Unity重命名所有子物体

本文提供了一个Unity中的脚本,用于批量重命名场景内的游戏对象。通过递归遍历指定父对象的所有子对象,该脚本可以实现从名字中替换特定字符串或者在名字前添加特定前缀的功能。

直接上代码

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

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值