EditorUtility.CreateEmptyPrefab 创建空预置

static function CreateEmptyPrefab (path : string) : Object

Description描述

Creates an empty prefab at given path.

在给定的路径创建一个空预置。

If a prefab at the path already exists it will be deleted and replaced with an empty prefab. Returns a reference to the prefab.

如果预置已经存在,它将删除并用一个空预置替换。返回一个预置的引用。

// 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;

	}

}
Page last updated: 2011-7-16