public SO_UniqueIdentifierAsset GenerateUniqueAsset(string assetName, string information) { System.Guid guid = System.Guid.NewGuid(); string guidString = guid.ToString(); string assetFilename = string.IsNullOrEmpty(assetName) ? guidString : assetName + "Guid"; if (AssetDatabase.IsValidFolder("Assets/UniqueIdentifierAssets") == false) { AssetDatabase.CreateFolder("Assets", "UniqueIdentifierAssets"); } if (AssetDatabase.IsValidFolder("Assets/UniqueIdentifierAssets/IdentifierAssets") == false) { AssetDatabase.CreateFolder("Assets/UniqueIdentifierAssets", "IdentifierAssets"); } string uniqueAssetFilename = AssetDatabase.GenerateUniqueAssetPath("Assets/UniqueIdentifierAssets/IdentifierAssets/" + assetFilename + ".asset"); SO_UniqueIdentifierAsset uniqueAsset = ScriptableObject.CreateInstance <SO_UniqueIdentifierAsset>(); uniqueAsset.Populate(guidString, ""); AssetDatabase.CreateAsset(uniqueAsset, uniqueAssetFilename); AssetDatabase.SaveAssets(); return(uniqueAsset); }
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); }