U3D : Tools – search and replace gameObjects

U3D : Tools – search and replace gameObjects

一段簡單的 script 可以選取在 sceneview 中的的 GameObject 然後用其他 GameObject 取代它們原有的位置,角度,大小等等.
在場景有大量相同的東西需要轉換時的小工具.

using UnityEngine;
using UnityEditor;
using System.Collections;

public class SearchAndReplace : ScriptableWizard
{
	public GameObject prefab;
	public GameObject[] OldObjects;

	[Header("Options")]
	public bool copyParentTransform = true;
	public bool copyPosition = true;
	public bool copyRotation = true;
	public bool copyScale = true;

	[MenuItem("Kit/Replace GameObjects")]
	static void CreateWizard()
	{
		ScriptableWizard.DisplayWizard("Replace GameObjects", typeof(SearchAndReplace), "Replace");
	}

	void OnWizardCreate()
	{
		int undoIdx = Undo.GetCurrentGroup();
		foreach (GameObject go in OldObjects)
		{
			GameObject newObject;
			if (prefab.transform.root == null)
				newObject = (GameObject)PrefabUtility.InstantiatePrefab(prefab); // prefab
			else
				newObject = Instantiate(prefab); // scene object

			Undo.RegisterCreatedObjectUndo(newObject, "NewObject");
			if (copyParentTransform) newObject.transform.SetParent(go.transform.parent);
			if (copyPosition) newObject.transform.position = go.transform.position;
			if (copyRotation) newObject.transform.rotation = go.transform.rotation;
			if (copyScale) newObject.transform.localScale = go.transform.localScale;
			Undo.RecordObject(newObject, "NewObjectParams");
			Undo.DestroyObjectImmediate(go);
		}
		Undo.CollapseUndoOperations(undoIdx);
	}
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

*

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料