EditorUtility.ReplacePrefab 替换预置

static function ReplacePrefab (go : GameObject, targetPrefab : Object, options : ReplacePrefabOptions = ReplacePrefabOptions.Default) : GameObject

Description描述

Replaces the targetPrefab with a copy of the game object hierarchy go.

替换目标预置,带有游戏物体层级的拷贝。

Returns the prefab game object after it has been created. If connectToPrefab is enabled go will be made an instance of the created prefab.

在创建之后,返回预置游戏物体。如果connectToPrefab启用,将创建预置的实例。

// Creates a prefab from the selected GameObjects.
// if the prefab already exists it asks if you want to replace it
//从选择的游戏物体创建预置。
//如果预置已经存在,询问是否要替换
class CreatePrefabFromSelected extends ScriptableObject {

	@MenuItem("Examples/Create Prefab From Selected")
	static function CreatePrefab() {
		var objs = Selection.gameObjects;

		for (var go : GameObject in objs) {
			var localPath : String = "Assets/" + go.name + ".prefab";
			if (AssetDatabase.LoadAssetAtPath(localPath, GameObject)) {
				if (EditorUtility.DisplayDialog("Are you sure?",
					"The prefab already exists. Do you want to overwrite it?",
					"Yes",
					"No"))
					createNew(go, localPath);
			} else {
				createNew(go, localPath);
			}

		}
	}
	@MenuItem("Examples/Create Prefab From Selected", true)
	static function ValidateCreatePrefab() {
		return Selection.activeGameObject != null;
	}

	static function createNew(obj : GameObject, localPath : String) {
		var prefab : Object = EditorUtility.CreateEmptyPrefab(localPath);
		EditorUtility.ReplacePrefab(obj, prefab);
		AssetDatabase.Refresh();

		DestroyImmediate(obj);
		var clone : GameObject = EditorUtility.InstantiatePrefab(prefab) as GameObject;
	}
}
最后修改:2011年7月16日 Saturday 10:38

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。