zoukankan      html  css  js  c++  java
  • 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 ();
    }
    }
    }

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

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

  • 相关阅读:
    将Python的Django框架与认证系统整合的方法
    将Python的Django框架与认证系统整合的方法
    Python的Asyncore异步Socket模块及实现端口转发的例子
    每天一个linux命令(3):du命令
    每天一个linux命令(2):file 命令
    Ubantu 使用extundelete恢复数据
    ubantu 单用户模式进入系统
    GDB 调试解析
    服务器搭建5 Samba实现文件共享
    服务器搭建4 安装其它库
  • 原文地址:https://www.cnblogs.com/chimo523/p/5064204.html
Copyright © 2011-2022 走看看