AssetDatabase是一个能获取工程资源的API,它提供一些方法比如:查找、加载、创建、删除和修改。Unity需要了解工程文件夹里的所有改变,假如想要获取或修改资源文件,就使用 AssetDatabase的API而不是文件IO流。

导入资源

Unity导入资源通常是用鼠标手动拖动到工程面板里,但是也可能需要脚本控制资源的导入,为了使用脚本导入可以使用AssetDatabase.ImportAsset方法,比如:

  1. using UnityEngine;
  2. using UnityEditor;
  3. public class ImportAsset {
  4. [MenuItem ("AssetDatabase/ImportExample")]
  5. static void ImportExample ()
  6. {
          AssetDatabase.ImportAsset("Assets/Textures/texture.jpg", ImportAssetOptions.Default);
  7. }
  8. }

加载资源

加载资源可以用这些方法:AssetDatabase.LoadAssetAtPath, AssetDatabase.LoadMainAssetAtPath, AssetDatabase.LoadAllAssetRepresentationsAtPath 和AssetDatabase.LoadAllAssetsAtPath.

  1. using UnityEngine;
  2. using UnityEditor;
  3. public class ImportAsset {
  4. [MenuItem ("AssetDatabase/LoadAssetExample")]
  5. static void ImportExample ()
  6. {
  7. Texture2D t = AssetDatabase.LoadAssetAtPath("Assets/Textures/texture.jpg", typeof(Texture2D)) as Texture2D;
  8. }
  9. }

用AssetDatabase做文件操作

由于unity采用元数据的文件方式,你需要对文件做创建、移动或者删除操作,可以用以下这些方法替代操作: AssetDatabase.Contains,AssetDatabase.CreateAsset, AssetDatabase.CreateFolder, AssetDatabase.RenameAsset, AssetDatabase.CopyAsset, AssetDatabase.MoveAsset,AssetDatabase.MoveAssetToTrash and AssetDatabase.DeleteAsset.

  1. public class AssetDatabaseIOExample {
  2. [MenuItem ("AssetDatabase/FileOperationsExample")]
  3. static void Example ()
  4. {
  5. string ret;
  6. // Create
  7. Material material = new Material (Shader.Find("Specular"));
  8. AssetDatabase.CreateAsset(material, "Assets/MyMaterial.mat");
  9. if(AssetDatabase.Contains(material))
  10. Debug.Log("Material asset created");
  11. // Rename
  12. ret = AssetDatabase.RenameAsset("Assets/MyMaterial.mat", "MyMaterialNew");
  13. if(ret == "")
  14. Debug.Log("Material asset renamed to MyMaterialNew");
  15. else
  16. Debug.Log(ret);
  17. // Create a Folder
  18. ret = AssetDatabase.CreateFolder("Assets", "NewFolder");
  19. if(AssetDatabase.GUIDToAssetPath(ret) != "")
  20. Debug.Log("Folder asset created");
  21. else
  22. Debug.Log("Couldn't find the GUID for the path");
  23. // Move
  24. ret = AssetDatabase.MoveAsset(AssetDatabase.GetAssetPath(material), "Assets/NewFolder/MyMaterialNew.mat");
  25. if(ret == "")
  26. Debug.Log("Material asset moved to NewFolder/MyMaterialNew.mat");
  27. else
  28. Debug.Log(ret);
  29. // Copy
  30. if(AssetDatabase.CopyAsset(AssetDatabase.GetAssetPath(material), "Assets/MyMaterialNew.mat"))
  31. Debug.Log("Material asset copied as Assets/MyMaterialNew.mat");
  32. else
  33. Debug.Log("Couldn't copy the material");
  34. // Manually refresh the Database to inform of a change
  35. AssetDatabase.Refresh();
  36. Material MaterialCopy = AssetDatabase.LoadAssetAtPath("Assets/MyMaterialNew.mat", typeof(Material)) as Material;
  37. // Move to Trash
  38. if(AssetDatabase.MoveAssetToTrash(AssetDatabase.GetAssetPath(MaterialCopy)))
  39. Debug.Log("MaterialCopy asset moved to trash");
  40. // Delete
  41. if(AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(material)))
  42. Debug.Log("Material asset deleted");
  43. if(AssetDatabase.DeleteAsset("Assets/NewFolder"))
  44. Debug.Log("NewFolder deleted");
  45. // Refresh the AssetDatabase after all the changes
  46. AssetDatabase.Refresh();
  47. }
  48. }

当你完成资源的修改后,应该调用AssetDatabase.Refresh方法来确认你的改变。

AssetDatabase文档翻译的更多相关文章

  1. 【Unity3D技术文档翻译】第1.8篇 AssetBundles 问题及解决方法

    上一章:[Unity3D技术文档翻译]第1.7篇 AssetBundles 补丁更新 本章原文所在章节:[Unity Manual]→[Working in Unity]→[Advanced Deve ...

  2. 我是如何进行Spring MVC文档翻译项目的环境搭建、项目管理及自动化构建工作的

    感兴趣的同学可以关注这个翻译项目 . 我的博客原文 和 我的Github 前段时间翻译的Spring MVC官方文档完成了第一稿,相关的文章和仓库可以点击以下链接.这篇文章,主要是总结一下这个翻译项目 ...

  3. Hibernate 3.3.2 文档翻译 Day01

    Hibernate 3.3.2 文档翻译 翻译人:微冷的雨 第一次书写:2015年11月29日 本人呕心沥血之作,请细心阅读领悟! Day01-1.1 项目描述 微冷的雨翻译:例如,我们将要建立一个可 ...

  4. Flume官方文档翻译——Flume 1.7.0 User Guide (unreleased version)中一些知识点

    Flume官方文档翻译--Flume 1.7.0 User Guide (unreleased version)(一) Flume官方文档翻译--Flume 1.7.0 User Guide (unr ...

  5. Flume官方文档翻译——Flume 1.7.0 User Guide (unreleased version)(二)

    Flume官方文档翻译--Flume 1.7.0 User Guide (unreleased version)(一) Logging raw data(记录原始数据) Logging the raw ...

  6. SQLAlchemy 中文文档翻译计划

    SQLAlchemy 中文文档翻译计划已启动. Python 文档协作翻译小组人手紧缺,有兴趣的朋友可以加入我们,完全公益性质.交流群:467338606. 希望大家能够勇敢地去翻译和改进翻译.虽然我 ...

  7. Laravel 5.3 中文文档翻译完成

    经过一个多月的紧张翻译和校对,翻译完成.以下是参与人员: Laravel 5.3 中文文档翻译完成 稿源:七星互联www . qixoo.com 文档地址在此:https://laravel-chin ...

  8. 蓝牙4.0——Android BLE开发官方文档翻译

    ble4.0开发整理资料_百度文库 http://wenku.baidu.com/link?url=ZYix8_obOT37JUQyFv-t9Y0Sv7SPCIfmc5QwjW-aifxA8WJ4iW ...

  9. Linux内核文档翻译之Squashfs文件系统

    转载:http://blog.csdn.net/gqb_driver/article/details/12946629 对于使用openwrt的嵌入式系统来说,因为硬件绝大多数采用Flash,因此一般 ...

随机推荐

  1. electron 开发拆坑总结

    electron 总结 前言 有一个web项目需要用客户端来包装一下 项目的主要业务都在服务器上 所以项目的大多数功能都用url 地址来访问: 客户端登陆界面在本地 打包客户端的本地登陆界面 做为登陆 ...

  2. socket 网络编程高速入门(一)教你编写基于UDP/TCP的服务(client)通信

    由于UNIX和Win的socket大同小异,为了方便和大众化,这里先介绍Winsock编程. socket 网络编程的难点在入门的时候就是对基本函数的了解和使用,由于这些函数的结构往往比較复杂,參数大 ...

  3. 走入asp.net mvc不归路:[1]项目文件结构

    先来了解一下一个asp.net mvc项目的文件结构. 1 项目文件结构一览 2 mvc,顾名思义,一个项目中最重要的就是这三个东西:M(Model,模型),V(View,视图),C(Controll ...

  4. C#语言 语句

    //有一组函数: //y=x(x<1) //y=2x-1(1<=x<10) //y=3x-11(x>=10) //括号内是x的满足条件 //实现功能,随意输出x /*Conso ...

  5. Webservice WCF WebApi 前端数据可视化 前端数据可视化 C# asp.net PhoneGap html5 C# Where 网站分布式开发简介 EntityFramework Core依赖注入上下文方式不同造成内存泄漏了解一下? SQL Server之深入理解STUFF 你必须知道的EntityFramework 6.x和EntityFramework Cor

    Webservice WCF WebApi   注明:改编加组合 在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下, ...

  6. PHP中include路径修改

    1.__FILE__ __FILE__ always equals to the real path of a php script regardless whether it's included. ...

  7. System V 信号量使用相关函数

    System V 信号量 在提到Posix 信号量时,指的是二值信号量或计数信号量,而System V信号量指的是入了计数信号量集 二值信号量:其值为0或1,类似于互斥锁,资源被锁住时为0,资源可用为 ...

  8. Android(Linux)模拟按键、触摸屏等事件

    前提: 在我们应用程序或者在写Android自己主动化測试时候经常会须要模拟实体按键,来给我们做測试用.这也是我要整理的目的. 基本的涉及的是Linux Input Event事件. 下面的样例基于A ...

  9. TiDB 是否支持其他存储引擎?

    https://www.pingcap.com/docs-cn/FAQ/ 1.1.11 TiDB 是否支持其他存储引擎? 是的,除了 TiKV 之外,TiDB 还支持一些流行的单机存储引擎,比如 Go ...

  10. Hadoop spark mongo复制集

    启动hadoop cd /usr/local/hadoop/hadoop $hadoop namenode -format # 启动前格式化namenode $./sbin/start-all.sh ...