言語: 日本語
  • C#
  • JS
  • Boo

スクリプト言語

お好みのスクリプト言語を選択すると、サンプルコードがその言語で表示されます。

PrefabUtility.CreateEmptyPrefab

public static function CreateEmptyPrefab(path: string): Object;

Description

指定されたパスに空のプレハブを作成します。

パスにプレハブが既に存在する場合、それは削除されて空のプレハブに置き換えられます。 プレハブへレファレンスを返します。

	// Creates a prefab from the selected GameObjects.
	// if the prefab already exists it asks if you want to replace it
	
	@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 = PrefabUtility.CreateEmptyPrefab(localPath);
		PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab);
	}