AssetDatabase文档翻译
2024-09-05 21:54:34
AssetDatabase是一个能获取工程资源的API,它提供一些方法比如:查找、加载、创建、删除和修改。Unity需要了解工程文件夹里的所有改变,假如想要获取或修改资源文件,就使用 AssetDatabase的API而不是文件IO流。
导入资源
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.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;
}
}
用AssetDatabase做文件操作
由于unity采用元数据的文件方式,你需要对文件做创建、移动或者删除操作,可以用以下这些方法替代操作: AssetDatabase.Contains,AssetDatabase.CreateAsset, AssetDatabase.CreateFolder, AssetDatabase.RenameAsset, AssetDatabase.CopyAsset, AssetDatabase.MoveAsset,AssetDatabase.MoveAssetToTrash and AssetDatabase.DeleteAsset.
public class AssetDatabaseIOExample {
[MenuItem ("AssetDatabase/FileOperationsExample")]
static void Example ()
{
string ret;
// Create
Material material = new Material (Shader.Find("Specular"));
AssetDatabase.CreateAsset(material, "Assets/MyMaterial.mat");
if(AssetDatabase.Contains(material))
Debug.Log("Material asset created");
// Rename
ret = AssetDatabase.RenameAsset("Assets/MyMaterial.mat", "MyMaterialNew");
if(ret == "")
Debug.Log("Material asset renamed to MyMaterialNew");
else
Debug.Log(ret);
// Create a Folder
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");
// Move
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);
// Copy
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");
// Manually refresh the Database to inform of a change
AssetDatabase.Refresh();
Material MaterialCopy = AssetDatabase.LoadAssetAtPath("Assets/MyMaterialNew.mat", typeof(Material)) as Material;
// Move to Trash
if(AssetDatabase.MoveAssetToTrash(AssetDatabase.GetAssetPath(MaterialCopy)))
Debug.Log("MaterialCopy asset moved to trash");
// Delete
if(AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(material)))
Debug.Log("Material asset deleted");
if(AssetDatabase.DeleteAsset("Assets/NewFolder"))
Debug.Log("NewFolder deleted");
// Refresh the AssetDatabase after all the changes
AssetDatabase.Refresh();
}
}
当你完成资源的修改后,应该调用AssetDatabase.Refresh方法来确认你的改变。
最新文章
- Nginx高级使用
- 转 PresentViewController切换界面
- 线性回归、梯度下降(Linear Regression、Gradient Descent)
- 据说每个大牛、小牛都应该有自己的库——Event处理
- RAR暴破
- 自定义的dialog
- 自学php找工作【二】 PHP计算时间加一天
- 【C++基础】指针好难啊,一点点啃——基本概念
- SQL 存储过程(学生,课程表,选修表)
- css media
- 关于委托:异常{ 无法将 匿名方法 转换为类型“System.Delegate”,因为它不是委托类型 }
- 解决本地软件链接不上虚拟机mysql 的问题:grant all privileges on *.* to 'root'@'%' identified by 'nsfocus'
- odd number、 even number
- Unity3D合并着色器
- GitHub版本控制
- ios控件 UIControl
- 每天一个linux命令(44)--ss命令
- LinkedBlockingDeque
- GA:利用GA对一元函数进行优化过程,求x∈(0,10)中y的最大值——Jason niu
- python中时间的转换和使用datetime