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 ();
    }
    }
    }

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

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

  • 相关阅读:
    jieba库的使用和词云
    类和正则表达
    数据库实践
    自己的第一个网页
    第一个爬虫和测试
    Linux 知识总结
    Python argparse模块基本用法
    Python面向对象编程
    linux Nginx发布基于PHP的WEB
    Linux nginx发布基于python的WEB环境
  • 原文地址:https://www.cnblogs.com/chimo523/p/5064204.html
Copyright © 2011-2022 走看看