예제 #1
0
        public void DestroyObject(SavedInstance savedInstance, Saveable saveable)
        {
            if (spawnInfo.ContainsKey(savedInstance))
            {
                spawnInfo.Remove(savedInstance);
                loadedIDs.Remove(saveable.SaveIdentification);

                changesMade++;
            }
        }
예제 #2
0
        public SavedInstance SpawnObject(InstanceSource source, string filePath, string saveIdentification = "")
        {
            GameObject getResource = null;

            // Implement more spawn methods here.
            // Such as usage for Asset Bundles & Adressables
            switch (source)
            {
            case InstanceSource.Resources:

                getResource = Resources.Load(filePath) as GameObject;

                break;

            default:
                break;
            }

            if (getResource == null)
            {
                Debug.LogWarning(string.Format("Invalid resource path: {0}", filePath));
                return(null);
            }

            changesMade++;

            // We will temporarily set the resource to disabled. Because we don't want to enable any
            // of the components yet.
            bool resourceState = getResource.gameObject.activeSelf;

            getResource.gameObject.SetActive(false);

            GameObject instance = GameObject.Instantiate(getResource, getResource.transform.position, getResource.transform.rotation);

            SceneManager.MoveGameObjectToScene(instance.gameObject, this.gameObject.scene);

            // After instantiating we reset the resource back to it's original state.
            getResource.gameObject.SetActive(resourceState);

            Saveable saveable = instance.GetComponent <Saveable>();

            if (saveable == null)
            {
                Debug.LogWarning("Save Instance Manager: No saveable added to spawned object." +
                                 " Scanning for ISaveables during runtime is more costly.");
                saveable = instance.AddComponent <Saveable>();
                saveable.ScanAddSaveableComponents();
            }

            SavedInstance savedInstance = instance.AddComponent <SavedInstance>();

            savedInstance.Configure(saveable, this);

            // In case the object has no idenfication, which applies to all prefabs.
            // Then we give it a new identification, and we store it into our spawninfo array so we know to spawn it again.
            if (string.IsNullOrEmpty(saveIdentification))
            {
                saveable.SaveIdentification = string.Format("{0}-{1}-{2}", SceneID, saveable.name, spawnCountHistory);

                spawnInfo.Add(savedInstance, new SpawnInfo()
                {
                    filePath           = filePath,
                    saveIdentification = saveable.SaveIdentification,
                    source             = source
                });

                spawnCountHistory++;

                loadedIDs.Add(saveable.SaveIdentification);
            }
            else
            {
                saveable.SaveIdentification = saveIdentification;
                loadedIDs.Add(saveable.SaveIdentification);
            }

            instance.gameObject.SetActive(true);

            return(savedInstance);
        }