Exemple #1
0
        public void AddListener(Saveable instance, SaveablePrefab scriptablePrefab, string identification)
        {
            SaveablePrefabData prefabData;

            // Is there no data yet for this scriptable prefab? Create it.
            if (!saveReferences.TryGetValue(scriptablePrefab, out prefabData))
            {
                prefabData = new SaveablePrefabData();

                prefabData.prefabGUID  = scriptablePrefab.GetGuid();
                prefabData.trimmedguid = $"{this.gameObject.scene.name}{prefabData.prefabGUID.Substring(0, 4)}";

                saveReferences.Add(scriptablePrefab, prefabData);
            }

            // Add identification keys for each saveable object.
            if (!prefabData.saveableGUIDS.ContainsKey(instance))
            {
                string saveableGUID = (string.IsNullOrEmpty(identification) ? $"I{prefabData.trimmedguid}{prefabData.saveableGUIDS.Count}" : identification);

                instance.saveIdentification.UseConstant   = true;
                instance.saveIdentification.ConstantValue = saveableGUID;

                isSaveable = true;

                prefabData.saveableGUIDS.Add(instance, saveableGUID);
            }
        }
Exemple #2
0
        public void RemoveListener(Saveable instance, SaveablePrefab scriptablePrefab)
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                return;
            }
#endif
            // We only remove the saved state of a prefab when it is destroyed by code.
            // Not while the scene is being destroyed or when the game is quitting.
            if (destroyingScene || quittingGame)
            {
                return;
            }

            SaveablePrefabData data;

            if (saveReferences.TryGetValue(scriptablePrefab, out data))
            {
                instance.RemoveFromSaveData();

                if (data.saveableGUIDS.Remove(instance))
                {
                    isSaveable = true;
                }
                else
                {
                    Debug.Log("Tried to remove listener that was never added.");
                }
            }
        }
Exemple #3
0
        public T Retrieve <T>(string identification = "") where T : UnityEngine.Object
        {
            GameObject prefabInstance = GameObject.Instantiate(prefab.gameObject);

            Saveable getSaveable = prefabInstance.GetComponent <Saveable>();

            if (getSaveable != null)
            {
                if (instanceManager == null)
                {
                    GameObject getManagerObject = instanceManagerReference?.Reference;
                    if (getManagerObject != null)
                    {
                        instanceManager = getManagerObject.GetComponent <SaveablePrefabInstanceManager>();
                    }
                    else
                    {
                        Debug.Log("No instance manager found within this scene. This means that a prefab will not save.");
                    }
                }

                if (instanceManager != null)
                {
                    SaveableInstance instanceController = prefabInstance.AddComponent <SaveableInstance>();
                    instanceController.SetSaveablePrefabInstanceManager(instanceManager, getSaveable, this);

                    instanceManager.AddListener(getSaveable, this, identification);
                }
            }

            if (getSaveable != null && typeof(T) == typeof(Saveable))
            {
                return(getSaveable as T);
            }

            if (typeof(T) == typeof(GameObject))
            {
                return(prefabInstance as T);
            }

            return(prefabInstance.GetComponent <T>());
        }
Exemple #4
0
        public void OnLoad(string data)
        {
            if (string.IsNullOrEmpty(data))
            {
                return;
            }

            currentSaveGame = SaveUtility.LoadSave(currentSaveSlot.Value);

            if (currentSaveGame == null)
            {
                Debug.Log("Could not find current save");
                return;
            }
            SaveData saveData = JsonUtility.FromJson <SaveData>(data);

            if (saveData.data != null && saveData.data.Count > 0)
            {
                for (int i = 0; i < saveData.data.Count; i++)
                {
                    SaveablePrefab saveablePrefab = ScriptableAssetDatabase.GetAsset(saveData.data[i].prefabGUID) as SaveablePrefab;

                    if (saveablePrefab == null)
                    {
                        //Debug.Log(saveData.data[i]);
                        Debug.Log($"Could not find reference in ScriptableAssetDatabase for Saveable Prefab : {saveData.data[i].prefabGUID}");
                        continue;
                    }

                    for (int i2 = 0; i2 < saveData.data[i].saveableGUIDs.Count; i2++)
                    {
                        Saveable getSaveable = saveablePrefab.Retrieve <Saveable>(saveData.data[i].saveableGUIDs[i2]);
                        getSaveable.OnLoadRequest(currentSaveGame);

#if UNITY_EDITOR
                        getSaveable.transform.SetParent(this.transform, true);
#endif
                    }
                }
            }
        }
Exemple #5
0
 private void OnEnable()
 {
     targetComponent = target as Saveable;
 }
Exemple #6
0
 public void SetSaveablePrefabInstanceManager(SaveablePrefabInstanceManager reference, Saveable saveable, SaveablePrefab saveablePrefab)
 {
     prefabInstanceManager = reference;
     this.saveable         = saveable;
     this.saveablePrefab   = saveablePrefab;
 }