public void Add(AssetLibraryAsset lib, List <int> instanceIDs, List <int> persistentIDs) { try { LibToIDs.Add(lib, new IDLists(instanceIDs, persistentIDs)); } catch (ArgumentException) { Debug.LogWarningFormat("Asset library {0} ordinal {1} already added.", lib.name, lib.Ordinal); throw; } }
private static void SaveAssetLibrary(AssetLibraryAsset asset, string folderName, string assetLibraryName, int index) { string dir = RTSLPath.UserRoot; string dataPath = Application.dataPath; if (!Directory.Exists(dataPath + dir)) { Directory.CreateDirectory(dataPath + dir); } if (!Directory.Exists(dataPath + dir + "/" + RTSLPath.LibrariesFolder)) { AssetDatabase.CreateFolder("Assets" + dir, RTSLPath.LibrariesFolder); } dir = dir + "/" + RTSLPath.LibrariesFolder; if (!Directory.Exists(dataPath + dir + "/Resources")) { AssetDatabase.CreateFolder("Assets" + dir, "Resources"); } dir = dir + "/Resources"; string[] folderNameParts = folderName.Split('/'); for (int i = 0; i < folderNameParts.Length; ++i) { string folderNamePart = folderNameParts[i]; if (!Directory.Exists(dataPath + dir + "/" + folderNamePart)) { AssetDatabase.CreateFolder("Assets" + dir, folderNamePart); } dir = dir + "/" + folderNamePart; } if (string.IsNullOrEmpty(AssetDatabase.GetAssetPath(asset))) { if (index == 0) { AssetDatabase.CreateAsset(asset, "Assets" + dir + "/" + assetLibraryName + ".asset"); } else { AssetDatabase.CreateAsset(asset, "Assets" + dir + "/" + assetLibraryName + (index + 1) + ".asset"); } } EditorUtility.SetDirty(asset); AssetDatabase.SaveAssets(); }
private static void CreateBuiltInAssetLibrary(int index, AssetLibraryAsset asset, AssetFolderInfo folder, HashSet <UnityObject> hs) { Dictionary <string, Type> builtInExtra = new Dictionary <string, Type> { { "Default-Line.mat", typeof(Material) }, { "Default-Material.mat", typeof(Material) }, { "Default-Particle.mat", typeof(Material) }, { "Default-Skybox.mat", typeof(Material) }, { "Sprites-Default.mat", typeof(Material) }, { "Sprites-Mask.mat", typeof(Material) }, { "UI/Skin/Background.psd", typeof(Sprite) }, { "UI/Skin/Checkmark.psd", typeof(Sprite) }, { "UI/Skin/DropdownArrow.psd", typeof(Sprite) }, { "UI/Skin/InputFieldBackground.psd", typeof(Sprite) }, { "UI/Skin/Knob.psd", typeof(Sprite) }, { "UI/Skin/UIMask.psd", typeof(Sprite) }, { "UI/Skin/UISprite.psd", typeof(Sprite) }, }; Dictionary <string, Type> builtIn = new Dictionary <string, Type> { { "New-Sphere.fbx", typeof(Mesh) }, { "New-Capsule.fbx", typeof(Mesh) }, { "New-Cylinder.fbx", typeof(Mesh) }, { "Cube.fbx", typeof(Mesh) }, { "New-Plane.fbx", typeof(Mesh) }, { "Quad.fbx", typeof(Mesh) }, { "Arial.ttf", typeof(Font) } }; List <object> builtInAssets = new List <object>(); foreach (KeyValuePair <string, Type> kvp in builtInExtra) { UnityObject obj = AssetDatabase.GetBuiltinExtraResource(kvp.Value, kvp.Key); if (obj != null) { builtInAssets.Add(obj); } } foreach (KeyValuePair <string, Type> kvp in builtIn) { UnityObject obj = Resources.GetBuiltinResource(kvp.Value, kvp.Key); if (obj != null) { builtInAssets.Add(obj); } } CreateAssetLibrary(builtInAssets.ToArray(), "BuiltInAssets", "BuiltInAssetLibrary", index, asset, folder, hs); }
public void LoadLibrary(string assetLibrary, int ordinal, bool loadIIDtoPID, bool loadPIDtoObj, Action <bool> callback) { if (m_ordinalToLib.ContainsKey(ordinal)) { Debug.LogWarningFormat("Asset Library {0} with this same ordinal {1} already loaded", m_ordinalToLib[ordinal].name, ordinal); callback(false); return; } ResourceRequest request = Resources.LoadAsync <AssetLibraryAsset>(assetLibrary); Action <AsyncOperation> completed = null; completed = ao => { AssetLibraryAsset assetLib = (AssetLibraryAsset)request.asset; if (assetLib == null) { if (IsBuiltinLibrary(ordinal)) { if (ordinal - AssetLibraryInfo.BUILTIN_FIRST == 0) { Debug.LogWarningFormat("Asset Library was not found : {0}. Click Tools->Runtime SaveLoad2->Libraries->Create Built-in asset library.", assetLibrary); } } else if (IsSceneLibrary(ordinal)) { if (ordinal - AssetLibraryInfo.SCENELIB_FIRST == 0) { Debug.LogWarningFormat("Asset Library was not found : {0}. Click Tools->Runtime SaveLoad2->Libraries->Collect Scene Dependencies.", assetLibrary); } } else { Debug.LogWarningFormat("Asset Library was not found : {0}", assetLibrary); } callback(false); return; } AddLibrary(assetLib, ordinal, loadIIDtoPID, loadPIDtoObj); callback(true); request.completed -= completed; }; request.completed += completed; }
private static AssetLibrariesListAsset Create() { AssetLibrariesListAsset asset = ScriptableObject.CreateInstance <AssetLibrariesListAsset>(); asset.List = new List <AssetLibraryListEntry>(); string[] assetLibraries = AssetDatabase.FindAssets("t:AssetLibraryAsset"); for (int i = 0; i < assetLibraries.Length; ++i) { string assetLib = assetLibraries[i]; assetLib = AssetDatabase.GUIDToAssetPath(assetLib); if (assetLib.StartsWith("Assets" + RTSLPath.UserRoot + "/" + RTSLPath.LibrariesFolder + "/Resources/Scenes/")) { continue; } if (assetLib.StartsWith("Assets" + RTSLPath.UserRoot + "/" + RTSLPath.LibrariesFolder + "/Resources/BuiltInAssets")) { continue; } if (!assetLib.Contains("/Resources/")) { Debug.LogWarning("Move " + assetLib + " to Resources folder"); continue; } AssetLibraryAsset assetLibAsset = AssetDatabase.LoadAssetAtPath <AssetLibraryAsset>(assetLib); int index = assetLib.IndexOf("/Resources/"); assetLib = assetLib.Remove(0, index + 11); asset.List.Add(new AssetLibraryListEntry { Library = assetLib, Ordinal = assetLibAsset.Ordinal }); } return(asset); }
private static void CreateAssetLibrary() { AssetLibraryAsset asset = ScriptableObject.CreateInstance <AssetLibraryAsset>(); int identity = AssetLibrariesListGen.GetIdentity(); asset.Ordinal = identity; string path = AssetDatabase.GetAssetPath(Selection.activeObject); string name = "/AssetLibrary" + ((identity == 0) ? "" : identity.ToString()); string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + name + ".asset"); AssetDatabase.CreateAsset(asset, assetPathAndName); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); //Selection.activeObject = asset; AssetLibrariesListGen.UpdateList(identity + 1); }
private static void CreateAssetLibraryForScene(Scene scene, int index, AssetLibraryAsset asset, AssetFolderInfo folder, HashSet <UnityObject> hs) { TypeMap typeMap = new TypeMap(); AssetDB assetDB = new AssetDB(); IOC.Register <ITypeMap>(typeMap); IOC.Register <IAssetDB>(assetDB); PersistentRuntimeScene rtScene = new PersistentRuntimeScene(); GetDepsFromContext ctx = new GetDepsFromContext(); rtScene.GetDepsFrom(scene, ctx); IOC.Unregister <ITypeMap>(typeMap); IOC.Unregister <IAssetDB>(assetDB); CreateAssetLibrary(ctx.Dependencies.ToArray(), "Scenes/" + scene.name, "SceneAssetLibrary", index, asset, folder, hs); }
public bool AddLibrary(AssetLibraryAsset assetLib, int ordinal, bool IIDtoObj, bool PIDtoObj) { if (m_ordinalToLib.ContainsKey(ordinal)) { Debug.LogWarningFormat("Asset Library with ordinal {0} already loadeded", assetLib.Ordinal); return(false); } if (m_loadedLibraries.Contains(assetLib)) { Debug.LogWarning("Asset Library already added"); return(false); } assetLib.Ordinal = ordinal; m_loadedLibraries.Add(assetLib); m_ordinalToLib.Add(ordinal, assetLib); LoadMapping(ordinal, IIDtoObj, PIDtoObj); return(true); }
private static HashSet <UnityObject> ReadFromBuiltInAssetLibraries(out int index, out AssetLibraryAsset asset, out AssetFolderInfo folder) { if (!Directory.Exists(Path.GetFullPath(RTSLPath.UserRoot + "/" + RTSLPath.LibrariesFolder + "/Resources/BuiltInAssets"))) { return(ReadFromAssetLibraries(new string[0], out index, out asset, out folder)); } string[] guids = AssetDatabase.FindAssets("", new[] { RTSLPath.UserRoot + "/" + RTSLPath.LibrariesFolder + "/Resources/BuiltInAssets" }); return(ReadFromAssetLibraries(guids, out index, out asset, out folder)); }
private static void CreateAssetLibraryForScene(Scene scene, int index, AssetLibraryAsset asset, AssetFolderInfo folder, HashSet <UnityObject> hs) { TypeMap <long> typeMap = new TypeMap <long>(); AssetDB assetDB = new AssetDB(); RuntimeShaderUtil shaderUtil = new RuntimeShaderUtil(); IOC.Register <ITypeMap>(typeMap); IOC.Register <IAssetDB>(assetDB); IOC.Register <IAssetDB <long> >(assetDB); IOC.Register <IRuntimeShaderUtil>(shaderUtil); PersistentRuntimeScene <long> rtScene = new PersistentRuntimeScene <long>(); GetDepsFromContext ctx = new GetDepsFromContext(); rtScene.GetDepsFrom(scene, ctx); Queue <UnityObject> depsQueue = new Queue <UnityObject>(ctx.Dependencies.OfType <UnityObject>()); GetDepsFromContext getDepsCtx = new GetDepsFromContext(); while (depsQueue.Count > 0) { UnityObject uo = depsQueue.Dequeue(); if (!uo) { continue; } Type persistentType = typeMap.ToPersistentType(uo.GetType()); if (persistentType != null) { getDepsCtx.Clear(); try { IPersistentSurrogate persistentObject = (IPersistentSurrogate)Activator.CreateInstance(persistentType); persistentObject.GetDepsFrom(uo, getDepsCtx); } catch (Exception e) { Debug.LogError(e); } foreach (UnityObject dep in getDepsCtx.Dependencies) { if (!ctx.Dependencies.Contains(dep)) { ctx.Dependencies.Add(dep); depsQueue.Enqueue(dep); } } } } IOC.Unregister <IRuntimeShaderUtil>(shaderUtil); IOC.Unregister <ITypeMap>(typeMap); IOC.Unregister <IAssetDB>(assetDB); IOC.Unregister <IAssetDB <long> >(assetDB); CreateAssetLibrary(ctx.Dependencies.ToArray(), "Scenes/" + scene.name, "SceneAssetLibrary", index, asset, folder, hs); }
private static void CreateAssetLibrary(object[] objects, string folderName, string assetLibraryName, int index, AssetLibraryAsset asset, AssetFolderInfo folder, HashSet <UnityObject> hs) { int identity = asset.AssetLibrary.Identity; foreach (object o in objects) { UnityObject obj = o as UnityObject; if (!obj) { if (o != null) { Debug.Log(o.GetType() + " is not a UnityEngine.Object"); } continue; } if (hs.Contains(obj)) { continue; } if (!AssetDatabase.Contains(obj)) { continue; } if (obj is GameObject) { GameObject go = (GameObject)obj; AssetInfo assetInfo = new AssetInfo(go.name, 0, identity); assetInfo.Object = go; hs.Add(go); identity++; List <PrefabPartInfo> prefabParts = new List <PrefabPartInfo>(); AssetLibraryAssetsGUI.CreatePefabParts(go, ref identity, prefabParts); for (int i = prefabParts.Count - 1; i >= 0; --i) { PrefabPartInfo prefabPart = prefabParts[i]; if (hs.Contains(prefabPart.Object)) { prefabParts.Remove(prefabPart); } else { hs.Add(prefabPart.Object); } } if (prefabParts.Count >= AssetLibraryInfo.MAX_ASSETS - AssetLibraryInfo.INITIAL_ID) { EditorUtility.DisplayDialog("Unable Create AssetLibrary", string.Format("Max 'Indentity' value reached. 'Identity' == {0}", AssetLibraryInfo.MAX_ASSETS), "OK"); return; } if (identity >= AssetLibraryInfo.MAX_ASSETS) { SaveAssetLibrary(asset, folderName, assetLibraryName, index); index++; asset = ScriptableObject.CreateInstance <AssetLibraryAsset>(); folder = asset.AssetLibrary.Folders.Where(f => f.depth == 0).First(); if (folder.Assets == null) { folder.Assets = new List <AssetInfo>(); } identity = asset.AssetLibrary.Identity; } assetInfo.PrefabParts = prefabParts; asset.AssetLibrary.Identity = identity; folder.Assets.Add(assetInfo); assetInfo.Folder = folder; } else { AssetInfo assetInfo = new AssetInfo(obj.name, 0, identity); assetInfo.Object = obj; identity++; if (identity >= AssetLibraryInfo.MAX_ASSETS) { SaveAssetLibrary(asset, folderName, assetLibraryName, index); index++; asset = ScriptableObject.CreateInstance <AssetLibraryAsset>(); folder = asset.AssetLibrary.Folders.Where(f => f.depth == 0).First(); if (folder.Assets == null) { folder.Assets = new List <AssetInfo>(); } identity = asset.AssetLibrary.Identity; } asset.AssetLibrary.Identity = identity; folder.Assets.Add(assetInfo); assetInfo.Folder = folder; } } SaveAssetLibrary(asset, folderName, assetLibraryName, index); index++; //Selection.activeObject = asset; //EditorGUIUtility.PingObject(asset); }
private static HashSet <UnityObject> ReadFromAssetLibraries(string[] guids, out int index, out AssetLibraryAsset asset, out AssetFolderInfo folder) { HashSet <UnityObject> hs = new HashSet <UnityObject>(); List <AssetLibraryAsset> assetLibraries = new List <AssetLibraryAsset>(); foreach (string guid in guids) { string path = AssetDatabase.GUIDToAssetPath(guid); AssetLibraryAsset assetLibrary = AssetDatabase.LoadAssetAtPath <AssetLibraryAsset>(path); if (assetLibrary != null) { assetLibrary.Foreach(assetInfo => { if (assetInfo.Object != null) { if (!hs.Contains(assetInfo.Object)) { hs.Add(assetInfo.Object); } if (assetInfo.PrefabParts != null) { foreach (PrefabPartInfo prefabPart in assetInfo.PrefabParts) { if (prefabPart.Object != null) { if (!hs.Contains(prefabPart.Object)) { hs.Add(prefabPart.Object); } } } } } return(true); }); assetLibraries.Add(assetLibrary); } } if (assetLibraries.Count == 0) { asset = ScriptableObject.CreateInstance <AssetLibraryAsset>(); index = 0; } else { asset = assetLibraries.OrderBy(a => a.AssetLibrary.Identity).FirstOrDefault(); index = assetLibraries.Count - 1; } folder = asset.AssetLibrary.Folders.Where(f => f.depth == 0).First(); if (folder.Assets == null) { folder.Assets = new List <AssetInfo>(); } return(hs); }
private static HashSet <UnityObject> ReadFromSceneAssetLibraries(Scene scene, out int index, out AssetLibraryAsset asset, out AssetFolderInfo folder) { if (!Directory.Exists(Application.dataPath + RTSLPath.UserRoot + "/" + RTSLPath.LibrariesFolder + "/Resources/Scenes/" + scene.name)) { return(ReadFromAssetLibraries(new string[0], out index, out asset, out folder)); } string[] guids = AssetDatabase.FindAssets("", new[] { "Assets" + RTSLPath.UserRoot + "/" + RTSLPath.LibrariesFolder + "/Resources/Scenes/" + scene.name }); return(ReadFromAssetLibraries(guids, out index, out asset, out folder)); }
public void SetTreeAsset(AssetLibraryAsset asset) { m_asset = asset; m_Initialized = false; }