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

posted @ 2012-08-08 18:13  Marble  阅读(2706)  评论(0编辑  收藏