• AssetDatabase文档翻译


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

  • 相关阅读:
    数据库数据实时采集Maxwell
    消息中间件之Kafka相关知识
    linux安装软件配置
    ETL工具Sqoop
    Hadoop 概述(三)
    全网最全的权限系统设计方案(图解)
    WebSocket 是什么原理?为什么可以实现持久连接
    封装 axios 拦截器实现用户无感刷新 access_token
    明明加了唯一索引,为什么还是产生了重复数据?
    动态组件和插槽
  • 原文地址:https://www.cnblogs.com/nafio/p/9137429.html
Copyright © 2020-2023  润新知