(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 |
No edit summary Tag: Visual edit |
||
Line 2: | Line 2: | ||
To use it: |
To use it: |
||
− | # Create a new C# script named |
+ | # Create a new C# script named DontDestroyMe.cs, and paste in the contents below |
− | # Drop a new Container in |
+ | # Drop a new Container in your game's first scene and add the new Dont Destroy Me component |
− | # |
+ | # Make sure there is a Remember Container component attached |
− | # Place an instance of this prefab in each scene. |
||
− | + | DontDestroyMe.cs: |
|
− | using UnityEngine;<br> |
+ | using UnityEngine;<br>using System.Collections;<br>using AC;<br> |
+ | |||
− | using System.Collections;<br> |
||
⚫ | |||
− | using AC;<br> |
||
⚫ | |||
− | <br> |
||
+ | DontDestroyOnLoad (gameObject); |
||
− | [RequireComponent (typeof(RememberContainer))]<br> |
||
⚫ | |||
− | {<br> |
||
− | <br> |
||
− | private int constantID = 0;<br> |
||
− | private RememberContainer rememberContainer;<br> |
||
− | <br> |
||
− | <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:
- Create a new C# script named DontDestroyMe.cs, and paste in the contents below
- Drop a new Container in your game's first scene and add the new Dont Destroy Me component
- 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); }
}