(Created page with "Containers are GameObjects that store a collection of inventory items that the player can retrieve. They are typically a per-scene object, but this script allows you to creat...")
Tag: Visual edit
 
Tag: Visual edit
 
Line 2: Line 2:
   
 
To use it:
 
To use it:
# Create a new C# script named SyncContainers.cs, and paste in the contents below
+
# Create a new C# script named DontDestroyMe.cs, and paste in the contents below
# Drop a new Container in the scene and add the new Sync Containers component
+
# Drop a new Container in your game's first scene and add the new Dont Destroy Me component
# Check '''Retain in prefab?''' in the Remember Container Inspector and make a new prefab from it
+
# Make sure there is a Remember Container component attached
# Place an instance of this prefab in each scene.
 
   
SyncContainers.cs:
+
DontDestroyMe.cs:
using UnityEngine;<br>
+
using UnityEngine;<br>using System.Collections;<br>using AC;<br>
  +
using System.Collections;<br>
 
 
public class DontDestroyMe : MonoBehaviour<br>{<br>
using AC;<br>
 
 
private void Start ()<br> {
<br>
 
  +
DontDestroyOnLoad (gameObject);
[RequireComponent (typeof(RememberContainer))]<br>
 
public class SyncContainers : MonoBehaviour<br>
 
{<br>
 
<br>
 
private int constantID = 0;<br>
 
private RememberContainer rememberContainer;<br>
 
<br>
 
<br>
 
private void OnEnable ()<br>
 
{<br>
 
rememberContainer = GetComponent <RememberContainer>();<br>
 
constantID = rememberContainer.constantID;<br>
 
<br>
 
EventManager.OnAfterChangeScene += OnAfterChangeScene;<br>
 
 
}<br>
 
}<br>
<br>
 
<br>
 
private void OnDisable ()<br>
 
{<br>
 
EventManager.OnAfterChangeScene -= OnAfterChangeScene;<br>
 
}<br>
 
<br>
 
<br>
 
private void OnAfterChangeScene (LoadingGame loadingGame)<br>
 
{<br>
 
if (constantID == 0)<br>
 
{<br>
 
return;<br>
 
}<br>
 
<br>
 
if (loadingGame == LoadingGame.No || loadingGame == LoadingGame.JustSwitchingPlayer)<br>
 
{<br>
 
SceneInfo previousSceneInfo = KickStarter.sceneChanger.GetPreviousSceneInfo (false);<br>
 
Debug.Log ("Load data for scene " + previousSceneInfo.name + " - " + previousSceneInfo.number + ", Searching for data ID: " + constantID);<br>
 
<br>
 
foreach (SingleLevelData levelData in KickStarter.levelStorage.allLevelData)<br>
 
{<br>
 
if (levelData.sceneNumber == previousSceneInfo.number)<br>
 
{<br>
 
foreach (ScriptData _scriptData in levelData.allScriptData)<br>
 
{<br>
 
if (_scriptData.data != null && _scriptData.data.Length > 0 && _scriptData.objectID == constantID)<br>
 
{<br>
 
Debug.Log ("Found save data in last scene, applying to self.", gameObject);<br>
 
rememberContainer.LoadData (_scriptData.data);<br>
 
}<br>
 
}<br>
 
}<br>
 
}<br>
 
}<br>
 
}<br>
 
 
<br>
 
 
}
 
}
 
[[Category:General]]
 
[[Category:General]]

Latest revision as of 11:03, 7 May 2020

Containers are GameObjects that store a collection of inventory items that the player can retrieve. They are typically a per-scene object, but this script allows you to create a Container whose contents are the same in all scenes.

To use it:

  1. Create a new C# script named DontDestroyMe.cs, and paste in the contents below
  2. Drop a new Container in your game's first scene and add the new Dont Destroy Me component
  3. Make sure there is a Remember Container component attached

DontDestroyMe.cs:

using UnityEngine;
using System.Collections;
using AC;
public class DontDestroyMe : MonoBehaviour
{
private void Start ()
{ DontDestroyOnLoad (gameObject); }
}
Community content is available under CC-BY-SA unless otherwise noted.