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

スクリプト言語

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

PrefabUtility.ReplacePrefab

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

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

Description

ゲームオブジェクトの階層にある go のコピーと targetPrefab を置き換えます。

それが作成された後にプレハブゲームオブジェクトを返します。 go が有効になっている場合、 go が作成されたプレハブのインスタンスを作ることになります。

	// Creates a prefab from the selected GameObjects.
	// 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);
	}