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方法来确认你的改变。

最新文章

  1. Nginx高级使用
  2. 转 PresentViewController切换界面
  3. 线性回归、梯度下降(Linear Regression、Gradient Descent)
  4. 据说每个大牛、小牛都应该有自己的库——Event处理
  5. RAR暴破
  6. 自定义的dialog
  7. 自学php找工作【二】 PHP计算时间加一天
  8. 【C++基础】指针好难啊,一点点啃——基本概念
  9. SQL 存储过程(学生,课程表,选修表)
  10. css media
  11. 关于委托:异常{ 无法将 匿名方法 转换为类型“System.Delegate”,因为它不是委托类型 }
  12. 解决本地软件链接不上虚拟机mysql 的问题:grant all privileges on *.* to 'root'@'%' identified by 'nsfocus'
  13. odd number、 even number
  14. Unity3D合并着色器
  15. GitHub版本控制
  16. ios控件 UIControl
  17. 每天一个linux命令(44)--ss命令
  18. LinkedBlockingDeque
  19. GA:利用GA对一元函数进行优化过程,求x∈(0,10)中y的最大值——Jason niu
  20. python中时间的转换和使用datetime

热门文章

  1. 使用FMDB多线程訪问数据库,及database is locked的问题
  2. webrtc初探
  3. NDK编译STL
  4. 未来 5 年八大热门 IT 职业
  5. TinyXML的使用
  6. Makefile:1: *** 多个目标匹配。 停止。
  7. Oracle:热备测试
  8. 【Java】DateUtil(1)
  9. java -- 虚拟机和内存
  10. BZOJ1453: [WC2005]Dface双面棋盘