public static void Add(IReferenceableAsset asset, string path) { if (cache == null) { Reload(); return; } if (!cache.ContainsKey(asset.GetGuid())) { cache.Add(asset.GetGuid(), path); } else { if (!cache.ContainsValue(path)) { Log($"Tried to add duplicate guid: {(asset as ScriptableObject).name}. Will create a new unique guid."); asset.GenerateNewGuid(); Add(asset, path); } } }
private static void LoadItems(bool testingInEditor) { int buildIndex = EditorSceneManager.GetActiveScene().buildIndex; bool canCreateAssetDatabase = (testingInEditor) ? true : (buildIndex == 0); // We only want to create this object at the first scene. if (canCreateAssetDatabase) { guidCache = new HashSet <string>(); if (BuildPipeline.isBuildingPlayer) { var scene = EditorSceneManager.GetActiveScene(); EditorSceneManager.MarkSceneDirty(scene); } GameObject assetDataBase = new GameObject("Scriptable Asset Database"); EditorUtility.SetDirty(assetDataBase); ScriptableAssetDatabase dataBase = assetDataBase.AddComponent <ScriptableAssetDatabase>(); EditorUtility.SetDirty(dataBase); string[] lookPath = new string[] { "Assets/ScriptableObjects", "Upload/Assets/ScriptableObjects" }; string[] itemGuidPaths = AssetDatabase.FindAssets("t:ScriptableObject", lookPath); for (int i = 0; i < itemGuidPaths.Length; i++) { string path = AssetDatabase.GUIDToAssetPath(itemGuidPaths[i]); IReferenceableAsset getItemData = AssetDatabase.LoadAssetAtPath <ScriptableObject>(path) as IReferenceableAsset; if (getItemData != null) { string guid = getItemData.GetGuid().ToString(); ScriptableObject asset = getItemData as ScriptableObject; bool isGuidDuplicate = guidCache.Contains(guid); // Check if guid is valid or duplicate, if so then we assign a new one for the asset. // And we ensure this gets saved within the editor. if (string.IsNullOrEmpty(getItemData.GetGuid()) || isGuidDuplicate) { getItemData.GenerateNewGuid(); guid = getItemData.GetGuid().ToString(); EditorUtility.SetDirty((ScriptableObject)getItemData); Debug.Log($"Found duplicate guid on {getItemData}"); } dataBase.values.Add(asset); dataBase.keys.Add(guid); // Fast way to do a lookup in case there is a duplicate guid guidCache.Add(guid); } } } }