// Loads a resource by GUID // Return null if it doesn't exist T LoadResourceByGUIDImpl <T>(string guid) where T : UnityEngine.Object { tk2dResource resource = Resources.Load(guidPrefix + guid, typeof(tk2dResource)) as tk2dResource; if (resource != null) { return(resource.objectReference as T); } else { return(null); } }
// Make this asset loadable at runtime, using the guid and the given name // The guid MUST match the GUID of the object. // The name is arbitrary and should be unique to make all assets findable using name, // but doesn't have to be. Name can be an empty string, but not null. public static bool MakeLoadableAsset(Object obj, string name) { string guid = GetObjectGUID(obj); // Check if it already is Loadable bool isLoadable = IsLoadableAsset(obj); if (isLoadable) { // Update name if it is different foreach (tk2dResourceTocEntry t in tk2dSystem.inst.Editor__Toc) { if (t.assetGUID == guid) { t.assetName = name; break; } } EditorUtility.SetDirty(tk2dSystem.inst); // Already loadable return(true); } // Create resource object string resourcePath = GetOrCreateResourcesDir() + "/" + "tk2d_" + guid + ".asset"; tk2dResource resource = ScriptableObject.CreateInstance <tk2dResource>(); resource.objectReference = obj; AssetDatabase.CreateAsset(resource, resourcePath); AssetDatabase.SaveAssets(); // Add index entry tk2dResourceTocEntry tocEntry = new tk2dResourceTocEntry(); tocEntry.resourceGUID = AssetDatabase.AssetPathToGUID(resourcePath); tocEntry.assetName = (name.Length == 0)?obj.name:name; tocEntry.assetGUID = guid; List <tk2dResourceTocEntry> toc = new List <tk2dResourceTocEntry>(tk2dSystem.inst.Editor__Toc); toc.Add(tocEntry); tk2dSystem.inst.Editor__Toc = toc.ToArray(); EditorUtility.SetDirty(tk2dSystem.inst); AssetDatabase.SaveAssets(); return(true); }