Unity AssetDataBase

本文深入讲解Unity中的AssetDatabase API,包括资源的导入、加载、创建、重命名、移动、复制及删除等操作,并通过代码示例展示如何在Unity编辑器环境中有效管理项目资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

一、导入资源

代码如下:

using UnityEngine;
using UnityEditor;
//Unity用脚本导入资源(没什么卵用)
public class ImportAsset
{
    [MenuItem("AssetDatabase/ImportExample")]
    static void ImportExample()
    {
        AssetDatabase.ImportAsset("Assets/UI/cube.png", ImportAssetOptions.Default);
    }
}

二、加载、卸载资源

代码如下:

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor; //在手机上上没有,需要使用条件编译
#endif
public class LoadAsset : MonoBehaviour {
    [MenuItem("AssetDatabase/LoadAssetExample")]
    static void LoadExample()
    {
#if UNITY_EDITOR
        //加载资源
        Texture2D t = AssetDatabase.LoadAssetAtPath("Assets/UI/cube.png", typeof(Texture2D)) as Texture2D;
        //使用资源
        print(t.name);
        //卸载资源: 注意,卸载的方法是在Resoutces类中,调用GC来清理垃圾,资源不是立刻就被清理掉的
        //所有在Unity控制台中还能输出语句
        print(AssetDatabase.GetAssetPath(t));
#endif
    }

}

运行效果:

三、创建资源

代码如下

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor; //在手机上上没有,需要使用条件编译
#endif

public class CreatAsset : MonoBehaviour {
    [MenuItem("AssetDatabase/CreatAssetExample")]
    static void CreatExample()
    {
        string str;
#if UNITY_EDITOR
        //创建
        Material material = new Material(Shader.Find("Specular"));
        AssetDatabase.CreateAsset(material, "Assets/Materials/myMaterials.mat");
        if(AssetDatabase.Contains(material))//如果存在
        print(material.name);
        //创建一个文件夹
        str = AssetDatabase.CreateFolder("Assets", "NewFolder");
        if (AssetDatabase.GUIDToAssetPath(str) != "")
            print("Folder Asset Created !");
        else
            print(str);

#endif
    }
}

运行前后对比

运行前:

运行后:

 

四、重命名

代码如下:

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor; //在手机上上没有,需要使用条件编译
#endif

public class Modification : MonoBehaviour {
    [MenuItem("AssetDatabase/ModAssetExample")]
    static void ModExample()
    {
        string str;
#if UNITY_EDITOR
        //重命名
        str = AssetDatabase.RenameAsset("Assets/Materials/myMaterials.mat", "mynewMaterials");
        if (str == "")
            print("Material asset rename to mynewMaterials");
        else
            print(str);
#endif
    }

}

运行结果:

五、移动

代码如下:

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor; //在手机上上没有,需要使用条件编译
#endif

public class Modification : MonoBehaviour {
    [MenuItem("AssetDatabase/ModAssetExample")]
    static void ModExample()
    {
        string str;
#if UNITY_EDITOR
        //移动
        str = AssetDatabase.MoveAsset("Assets/Materials/mynewMaterials.mat", "Assets/NewFolder");
        if (str == "")
            print("Material asset moved to NewFolder");
        else
            print(str);
#endif
    }

}

 

运行结果如下:

六、复制

代码如下:

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor; //在手机上上没有,需要使用条件编译
#endif

public class Modification : MonoBehaviour {
    [MenuItem("AssetDatabase/ModAssetExample")]
    static void ModExample()
    {
        string str;
#if UNITY_EDITOR
        //复制
        if (AssetDatabase.CopyAsset("Assets/NewFolder/mynewMaterials.mat", "Assets/Materials/myMaterials.mat"))
            print("Material asset copied as Asset/Material/myMaterials");
        else
            print("Couldn't copy the materal");
#endif
    }

}

运行结果:

生成了一个新的材质

 

七、移动到回收站并且删除

代码如下:

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor; //在手机上上没有,需要使用条件编译
#endif

public class Modification : MonoBehaviour {
    [MenuItem("AssetDatabase/ModAssetExample")]
    static void ModExample()
    {
        string str;
#if UNITY_EDITOR
        //加载并赋值给materialCopy
        Material materialCopy = AssetDatabase.LoadAssetAtPath("Assets/Materials/myMaterials.mat", typeof(Material)) as Material;
        //移动到回收站
        if (AssetDatabase.MoveAssetToTrash(AssetDatabase.GetAssetPath(materialCopy)))
            print("MaterialCopy asset moved to trash");
        //删除
        if (AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(materialCopy)))
            print("Material asset deletede");
        if (AssetDatabase.DeleteAsset("Assets/NewFolder"))
            print("NewFolder deleted");
        //刷新
        AssetDatabase.Refresh();
#endif
    }

}

运行结果对比:

运行前:

运行后:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值