Unity重命名所有子物体

為{幸葍}努か 提交于 2020-01-19 00:12:45

直接上代码

using System;
using UnityEngine;

public class Renamer : MonoBehaviour
{
    [ContextMenu("Rename")]
    private void Rename()
    {
        for (int i = 0; i < this.parent.childCount; i++)
        {
            if (this.parent.GetChild(i).name.Contains(this.fromName) || this.toAppend)
            {
                if (this.toAppend)
                {
                    this.parent.GetChild(i).name = this.toName + this.parent.GetChild(i).name;
                }
                else
                {
                    this.parent.GetChild(i).name = this.parent.GetChild(i).name.Replace(this.fromName, this.toName);
                }
                if (this.parent.GetChild(i).childCount > 0)
                {
                    this.CheckChilds(this.parent.GetChild(i));
                }
            }
        }
    }

    private void CheckChilds(Transform t)
    {
        for (int i = 0; i < t.childCount; i++)
        {
            if (t.GetChild(i).name.Contains(this.fromName) || this.toAppend)
            {
                if (this.toAppend)
                {
                    t.GetChild(i).name = this.toName + t.GetChild(i).name;
                }
                else
                {
                    t.GetChild(i).name = t.GetChild(i).name.Replace(this.fromName, this.toName);
                }
                this.CheckChilds(t.GetChild(i));
            }
        }
    }

    public bool toAppend;

    public string fromName;

    public string toName;

    public Transform parent;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!