zoukankan      html  css  js  c++  java
  • AssetDatabase 资源数据库

    AssetDatabase is an API which allows you to access the assets contained in your project. Among other things, it provides methods to find and load assets and also to create, delete and modify them. The Unity Editor uses the AssetDatabase internally to keep track of asset files and maintain the linkage between assets and objects that reference them. Since Unity needs to keep track of all changes to the project folder, you should always use the AssetDatabase API rather than the filesystem if you want to access or modify asset data.

    AssetDatabase是一个API,它允许您访问您的项目中的资源。它提供了查找资源、加载资源、创建资源,删除资源和修改资源的方法。Unity编辑器内部使用AssetDatabase保持跟踪资源文件和保持资源与引用它们的对象之间的关联。由于Unity需要保持跟踪项目文件夹中的所有更改,如果你想访问或修改的资源数据,你应该始终使用AssetDatabase API,而不是文件系统。

    The AssetDatabase interface is only available in the editor and has no function in the built player. Like all other editor classes, it is only available to scripts placed in the Editor folder (just create a folder named Editor in the main Assets folder of your project if there isn't one already).

    AssetDatabase接口仅在编辑器中可用,在内置播放器没有这功能。像所有其他编辑器类,它是只适用于放置在Editor文件夹下的脚本(如果没有的话,在主资源项目文件夹下创建一个命名为Editor的文件夹。)。

    Importing an Asset 导入资源

    Unity normally imports assets automatically when they are dragged into the project but it is also possible to import them under script control. To do this you can use the AssetDatabase.ImportAsset method as in the example below.

    Unity的资源通常在拖动到项目时自动导入,但它也可以在脚本控制下导入。要做到这一点,你可以使用AssetDatabase.ImportAsset函数(方法)。就像下面的例子:

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

    You can also pass an extra parameter of type AssetDatabase.ImportAssetOptions to the AssetDatabase.ImportAsset call. The scripting reference page documents the different options and their effects on the function's behaviour.

    您还可以传递一个额外的AssetDatabase.ImportAssetOptions类型的参数,调用AssetDatabase.ImportAsset。脚本参考页记载着不同的选项以及不同选项对函数行为的作用。

    Loading an Asset 加载资源

    The editor loads assets only as needed, say if they are added to the scene or edited from the Inspector panel. However, you can load and access assets from a script using AssetDatabase.LoadAssetAtPathAssetDatabase.LoadMainAssetAtPath,AssetDatabase.LoadAllAssetRepresentationsAtPath and AssetDatabase.LoadAllAssetsAtPath. See the scripting documentation for further details.

    编辑器仅在需要时加载资源,如果资源被添加到场景或在检视面板编辑。但是,您可以用脚本来加载和访问资源,使用AssetDatabase.LoadAssetAtPath,AssetDatabase.LoadMainAssetAtPathAssetDatabase.LoadAllAssetRepresentationsAtPath and AssetDatabase.LoadAllAssetsAtPath。更多细节,请参阅脚本文档。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    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;
        }
    }

    File Operations using the AssetDatabase 文件操作使用AssetDatabase

    Since Unity keeps metadata about asset files, you should never create, move or delete them using the filesystem. Instead, you can useAssetDatabase.ContainsAssetDatabase.CreateAssetAssetDatabase.CreateFolderAssetDatabase.RenameAssetAssetDatabase.CopyAsset,AssetDatabase.MoveAssetAssetDatabase.MoveAssetToTrash and AssetDatabase.DeleteAsset.

    由于Unity保留有关资源文件的元数据,你不该使用文件系统创建,移动或删除它们。相反,你可以使用AssetDatabase.ContainsAssetDatabase.CreateAsset,AssetDatabase.CreateFolderAssetDatabase.RenameAssetAssetDatabase.CopyAssetAssetDatabase.MoveAsset,AssetDatabase.MoveAssetToTrash and AssetDatabase.DeleteAsset

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    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();
        }
    }

    Using AssetDatabase.Refresh 使用AssetDatabase.Refresh

    When you have finished modifying assets, you should call AssetDatabase.Refresh to commit your changes to the database and make them visible in the project.

    当你完成修改资源,你应该调用AssetDatabase.Refresh来提交更改到数据库,并使他们在项目可见。

    aliyun活动 https://www.aliyun.com/acts/limit-buy?userCode=re2o7acl
  • 相关阅读:
    JS小技巧总汇
    [转贴]聪明人如何拯救你的职业困
    Button按钮多行显示的实现方法
    事件和委托
    支持~
    关于递归
    Android 资源的国际化
    Android 文件的浏览(类似于FileDialog的功能)
    Android 开发TCP、UdP客户端
    Android 为什么现在google不让结束整个程序,只让结束单个Activity(转)
  • 原文地址:https://www.cnblogs.com/wangbin/p/2341795.html
Copyright © 2011-2022 走看看