资源数据库 AssetDatabase

本文介绍了Unity中的AssetDatabase API,详细讲解了如何使用该API进行资源的导入、加载、创建、移动、复制、重命名和删除等操作。同时,还介绍了如何使用AssetDatabase.Refresh方法来提交更改并使它们在项目中可见。

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函数(方法)。就像下面的例子:

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.LoadMainAssetAtPathAssetDatabase.LoadAllAssetRepresentationsAtPath andAssetDatabase.LoadAllAssetsAtPath. See the scripting documentation for further details.

编辑器仅在需要时加载资源,如果资源被添加到场景或在检视面板编辑。但是,您可以用脚本来加载和访问资源,使用AssetDatabase.LoadAssetAtPath,AssetDatabase.LoadMainAssetAtPathAssetDatabase.LoadAllAssetRepresentationsAtPath and 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;
	}
}

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.MoveAssetAssetDatabase.MoveAssetToTrash andAssetDatabase.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();
	}
}

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来提交更改到数据库,并使他们在项目可见。

原文地址:http://www.ceeger.com/Manual/AssetDatabase.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值