LittleTools之批量替换工具

身为程序员,有很多事情都可以交给机器来做,这样可以提高工作效率。

在此先写个批量替换工具,用来将某些对象统一替换为另一对象。

比方说场景中摆了一堆树,位置、比例、旋转都已经调好了,但是对树的样式不太满意,想要替换掉。

using UnityEngine;
using UnityEditor;
/// <summary>
/// 替换场景中的对象
/// </summary>
public class Replace :EditorWindow {
static GameObject obj_Profeb;
[MenuItem ("Custom/Replace")]
static void ReplaceObj() {
EditorWindow.GetWindow<Replace> (true, "Replace");
}

void ReplaceEnd() {
Transform[] selection = Selection.GetTransforms(SelectionMode.Editable | SelectionMode.ExcludePrefab);//注意:所选中的对象必须只有一个父或者其父的父无缩放!!!
GameObject _Obj=obj_Profeb;//不能直接使用obj_Profeb,需要用_Obj代替
for (int i=0; i<selection.Length; i++) {
GameObject tempObj=(GameObject)Instantiate(_Obj,selection[i].position,selection[i].rotation);

tempObj.transform.localScale=selection[i].localScale;
tempObj.name=selection[i].name;
tempObj.transform.parent=selection[i].parent;

DestroyImmediate(selection[i].gameObject);
}
Debug.Log ("成功!");

}
void OnGUI(){
// str_Name=EditorGUILayout.TextField("输入文字:",str_Name);
obj_Profeb = (GameObject)EditorGUILayout.ObjectField ("OBJ",obj_Profeb,typeof(GameObject),true);
if (GUILayout.Button ("Replace")) {
ReplaceEnd ();
}
}
}

这是一个很简单的代码,但是有的时候能节省很多的时间。

在遇到手动解决起来比较繁琐的问题时,冷静一下,想想看能不能用程序员的方式解决。

 

posted @ 2015-12-21 18:06  团谋  阅读(508)  评论(0编辑  收藏  举报