public void DestroyRuntimeInstance(StorableRuntimeInstance instance, Storable storable) { if (_spawnedInstances.ContainsKey(instance)) { _spawnedInstances.Remove(instance); _loadedRuntimeInstances.Remove(storable.Guid.GetGuid); } }
public StorableRuntimeInstance SpawnRuntimeInstance(RuntimeSource source, string path, string guid = null, string information = null) { // Retrieve the source GameObject resource = null; switch (source) { case RuntimeSource.Resource: resource = Resources.Load(path) as GameObject; break; default: break; } if (resource == null) { Debug.LogWarning("Invalid resource path : " + path); return(null); } // Instantiate the gameobject and ensure to disable it first, to prevent any component from starting / awakening bool resourceState = resource.gameObject.activeSelf; resource.gameObject.SetActive(false); GameObject instance = GameObject.Instantiate(resource, resource.transform.position, resource.transform.rotation); SceneManager.MoveGameObjectToScene(instance.gameObject, this.gameObject.scene); resource.gameObject.SetActive(resourceState); // Ensure that our runtime prefab has a Storable component, if not create one and attach it Storable storable = instance.GetComponent <Storable>(); if (storable == null) { storable = instance.AddComponent <Storable>(); storable.RefreshStorables(); } // Add a StorableRuntimeInstance component to the instance // This component will ensure to destroy properly the object from the game and from the save StorableRuntimeInstance storableRuntimeInstance = instance.AddComponent <StorableRuntimeInstance>(); storableRuntimeInstance.Init(this, storable); // If there is no GUID attach to Storable component (which apply to all prefab) we need to assign one // We need to reassign them from the persistence string identifierGuid = null; string identifierInformation = null; if (string.IsNullOrEmpty(guid)) { if (string.IsNullOrEmpty(storable.Guid.GetGuid) == false) { Debug.LogError("You want to spawn a prefab instance which shouldn't have a GUID but it actually has one."); return(null); } identifierGuid = _guid.GetGuid + StorageConstants.STORAGE_SEPARATOR_GUID + _countHistory; identifierInformation = _guid.GetInformation + StorageConstants.STORAGE_SEPARATOR_INFORMATION; _spawnedInstances.Add(storableRuntimeInstance, new RuntimeInstance(source, path, identifierInformation, identifierGuid)); } else { identifierGuid = guid; identifierInformation = information; } _loadedRuntimeInstances.Add(identifierGuid); SO_UniqueIdentifierAsset assetIdentification = ScriptableObject.CreateInstance <SO_UniqueIdentifierAsset>(); assetIdentification.Populate(identifierGuid, identifierInformation); storable.Guid = new Scylla.CommonModules.Identification.Guid(assetIdentification); instance.gameObject.SetActive(true); return(storableRuntimeInstance); }
public void Init(StorageScene sceneContainer, Storable storable) { _sceneContainer = sceneContainer; _storable = storable; }