예제 #1
0
        public SerializableSaveObject(SaveableObject saveableObject)
        {
            this.ID = saveableObject.ID;
            string lastPart = saveableObject.ID.Split('_').Last();

            this.associationID = lastPart.IsGuid() ? lastPart : this.ID;
            this.sceneName     = saveableObject.gameObject.scene.name;
        }
        /// <summary>
        /// Register a SaveableObject, if it's not already registered.
        /// This should happen on Awake for all SaveableObjects when its scene is loaded.
        /// </summary>
        /// <param name="saveableObject">SaveableObject to be registered</param>
        /// <returns>True if the object was registered, false otherwise</returns>
        public bool RegisterSaveableObject(SaveableObject saveableObject)
        {
            string id = saveableObject.ID;

            if (saveableObjects.ContainsKey(id))
            {
                if (saveableObjects[id] == null)
                {
                    saveableObjects[id] = saveableObject;
                    return(true);
                }
                Debug.LogWarning($"saveableObjects already contains key {id}: {saveableObjects[id]}");
                return(false);
            }

            saveableObjects.Add(id, saveableObject);
            return(true);
        }
        /// <summary>
        /// Will return a SaveableObject reference if this scene is active, or a SerializableSaveObject if the scene is inactive
        /// Will return null if the id is not found in its respective dictionary
        /// </summary>
        /// <param name="id">ID of the SaveableObject or SerializableSaveObject to be retrieved</param>
        /// <returns></returns>
        public Either <SaveableObject, SerializableSaveObject> GetSaveableObject(string id)
        {
            if (sceneIsLoaded)
            {
                if (saveableObjects.ContainsKey(id))
                {
                    if (saveableObjects[id] == null)
                    {
                        saveableObjects.Remove(id);
                        return(null);
                    }

                    return(saveableObjects[id]);
                }

                SaveableObject missingSaveableObject = LookForMissingSaveableObject(id);
                if (missingSaveableObject != null)
                {
                    missingSaveableObject.Register();
                    return(missingSaveableObject);
                }

                Debug.LogError($"No saveableObject found with id {id} in scene {sceneName}");
                return(null);
            }
            else
            {
                if (serializedSaveObjects.ContainsKey(id))
                {
                    return(serializedSaveObjects[id]);
                }

                Debug.LogError($"No serializedSaveObject found with id {id} in scene {sceneName}");
                return(null);
            }
        }