AssetDatabase 是一个API,可用于访问项目中包含的资源。除此之外,它还提供了查找和加载资源的方法,以及创建、删除和修改资源的方法。Unity Editor 在内部使用 AssetDatabase 来跟踪资源文件并维护资源和引用它们的对象之间的链接关系。由于 Unity 需要跟踪项目文件夹的所有更改,因此如果要访问或修改资源数据,则应始终使用 AssetDatabase API 而不是文件系统。
AssetDatabase 接口仅在编辑器中可用,并且在构建的播放器中没有任何函数。与所有其他编辑器类一样,它仅适用于放置在 Editor 文件夹中的脚本(直接在项目的 Assets 主文件夹中创建名为“Editor”的文件夹,如果还没有的话)。
Unity 通常会在资源被拖入项目时自动导入资源,但也可以在脚本控制下导入资源。为此,可以使用 AssetDatabase.ImportAsset 方法,如以下示例所示。
using UnityEngine;
using UnityEditor;
public class ImportAsset {
[MenuItem ("AssetDatabase/ImportExample")]
static void ImportExample ()
{
AssetDatabase.ImportAsset("Assets/Textures/texture.jpg", ImportAssetOptions.Default);
}
}
还可以将 AssetDatabase.ImportAssetOptions 类型的额外参数传递给 AssetDatabase.ImportAsset 调用。脚本参考页面介绍了不同的选项及其对函数行为的影响。
编辑器仅在需要时加载资源,例如,是否将资源添加到场景或从 Inspector 面板中进行编辑。但是,可以使用 AssetDatabase.LoadAssetAtPath、AssetDatabase.LoadMainAssetAtPath、AssetDatabase.LoadAllAssetRepresentationsAtPath 和 AssetDatabase.LoadAllAssetsAtPath 从脚本加载和访问资源。有关更多详细信息,请参阅脚本文档。
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;
}
}
由于 Unity 保留有关资源文件的元数据,因此不应使用文件系统创建、移动或删除这些文件。相反,可以使用 AssetDatabase.Contains、AssetDatabase.CreateAsset、AssetDatabase.CreateFolder、AssetDatabase.RenameAsset、AssetDatabase.CopyAsset、AssetDatabase.MoveAsset、AssetDatabase.MoveAssetToTrash 和 AssetDatabase.DeleteAsset。
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
AssetDatabase.Refresh();
}
}
在完成资源的修改后,应调用 AssetDatabase.Refresh 来提交对数据库的更改并使其在项目中可见。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.