AssetDatabase.AddObjectToAsset 添加对象到资源

static function AddObjectToAsset (objectToAdd : Object, assetPath : string) : void

Description描述

Adds objectToAdd to an existing asset at path.

将objectToAdd这个对象添加到指定路径上的现有资源。

Please note that you should only add assets to '.asset' assets, imported models or texture assets for example will lose their data. All paths are relative to the project folder. Like: "Assets/MyTextures/hello.png"

请注意,你应该添加资源到”.assets”目录下的资源中,例如导入的模型或纹理将会丢失它们地数据。所有的路径都是相对于工程目录文件。 例如” Assets/MyTextures/hello.png”。

@MenuItem("GameObject/Create Material")
static function CreateMaterial () {
	// Create a simple material asset
	//新建一个简单的材质资源
	var material = new Material (Shader.Find("Specular"));
	AssetDatabase.CreateAsset(material, "Assets/MyMaterial.mat");

	// Add an animation clip to it
	//添加一个动画剪辑到材质上
	var animationClip = new AnimationClip ();
	animationClip.name = "My Clip";
	AssetDatabase.AddObjectToAsset(animationClip, material);

	// Reimport the asset after adding an object.
	//在新建一个对象后重新导入资源
	// Otherwise the change only shows up when saving the project
	//否则这个更改只会在保存工程时才显示
	AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(animationClip));

	// Print the path of the created asset
	//打印新建的资源
	Debug.Log(AssetDatabase.GetAssetPath(material));
}

• static function AddObjectToAsset (objectToAdd : Object, assetObject : Object) : void

Description描述

Adds objectToAdd to an existing asset identified by assetObject.

将objectToAdd这个对象添加到assertObject定义的资源上。

Please note that you should only add assets to '.asset' files, imported models or texture assets for example will lose their data when reimporting or quitting Unity.

请注意,你只能添加资源到”.asset”文件中,例如导入模型或纹理在重新导入或退出Unity时将会丢失数据。

@MenuItem("GameObject/Create Material")
static function CreateMaterial () {
	// Create a simple material asset
	//新建一个新的材质资源
	var material = new Material (Shader.Find("Specular"));
	AssetDatabase.CreateAsset(material, "Assets/MyMaterial.mat");

	// Add an animation clip to it
	//添加一个动画剪辑到材质上
	var animationClip = new AnimationClip ();
	animationClip.name = "My Clip";
	AssetDatabase.AddObjectToAsset(animationClip, material);

	// Reimport the asset after adding an object.
	//添加对象后重新导入资源
	// Otherwise the change only shows up when saving the project
	//否则这个更改只会在保存工程时才显示
	AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(animationClip));

	// Print the path of the created asset
	//打印新建的资源
	Debug.Log(AssetDatabase.GetAssetPath(material));
}
Page last updated: 2011-5-31