本篇文章纪录和Asset DataBase相关,可以通过脚本方式控制。
Importing an Asset
1 2 3 4 5 6 7 8 9 10
| using UnityEngine; using UnityEditor;
public class ImportAsset { [MenuItem ("AssetDatabase/ImportExample")] static void ImportExample () { AssetDatabase.ImportAsset("Assets/Textures/texture.jpg", ImportAssetOptions.Default); } }
|
ImportAssetOptions选项:
Default 默认导入选项。
ForceUpdate 由用户发起的资源导入。
ForceSynchronousImport 同步导入所有资源。
ImportRecursive 导入文件夹时,也要导入其中的所有内容。
DontDownloadFromCacheServer 强制完全重新导入,但不从缓存服务器下载资源。
ForceUncompressedImport 强制资源以版本工具的未压缩形式导入。
Loading an Asset
1 2 3 4 5 6 7 8 9 10
| using UnityEngine; using UnityEditor;
public class ImportAsset { [MenuItem ("AssetDatabase/LoadAssetExample")] static void ImportExample () { Texture2D t = AssetDatabase.LoadAssetAtPath("Assets/Textures/texture.jpg", typeof(Texture2D)) as Texture2D; } }
|
有关AssetDataBase的文件操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| public class AssetDatabaseIOExample { [MenuItem ("AssetDatabase/FileOperationsExample")] static void Example () { string ret; Material material = new Material (Shader.Find("Specular")); AssetDatabase.CreateAsset(material, "Assets/MyMaterial.mat"); if(AssetDatabase.Contains(material)) Debug.Log("Material asset created"); ret = AssetDatabase.RenameAsset("Assets/MyMaterial.mat", "MyMaterialNew"); if(ret == "") Debug.Log("Material asset renamed to MyMaterialNew"); else Debug.Log(ret); ret = AssetDatabase.CreateFolder("Assets", "NewFolder"); if(AssetDatabase.GUIDToAssetPath(ret) != "") Debug.Log("Folder asset created"); else Debug.Log("Couldn't find the GUID for the path"); ret = AssetDatabase.MoveAsset(AssetDatabase.GetAssetPath(material), "Assets/NewFolder/MyMaterialNew.mat"); if(ret == "") Debug.Log("Material asset moved to NewFolder/MyMaterialNew.mat"); else Debug.Log(ret); if(AssetDatabase.CopyAsset(AssetDatabase.GetAssetPath(material), "Assets/MyMaterialNew.mat")) Debug.Log("Material asset copied as Assets/MyMaterialNew.mat"); else Debug.Log("Couldn't copy the material"); AssetDatabase.Refresh(); Material MaterialCopy = AssetDatabase.LoadAssetAtPath("Assets/MyMaterialNew.mat", typeof(Material)) as Material; if(AssetDatabase.MoveAssetToTrash(AssetDatabase.GetAssetPath(MaterialCopy))) Debug.Log("MaterialCopy asset moved to trash"); if(AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(material))) Debug.Log("Material asset deleted"); if(AssetDatabase.DeleteAsset("Assets/NewFolder")) Debug.Log("NewFolder deleted"); AssetDatabase.Refresh(); } }
|