直接上代码
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;
}
本文提供了一个Unity中的脚本,用于批量重命名场景内的游戏对象。通过递归遍历指定父对象的所有子对象,该脚本可以实现从名字中替换特定字符串或者在名字前添加特定前缀的功能。
2155

被折叠的 条评论
为什么被折叠?



