增加GameObject后缀
实现功能:
(1)单选/多选 物体,填写后缀名,即可为其本身及子级添加后缀名
面板展示:
代码编写:
using UnityEngine;
using UnityEditor;
public class Add_Suffix : EditorWindow
{
static string addSuffix = null;//后缀
[MenuItem("GameObject/物体批量操作(单选|多选)/寻找物体所有子级并添加后缀", false, 0)]
static void FindAllSubsetsAndAddSuffixes()
{
if(Selection.gameObjects.Length > 0)
{
//窗口大小及呈现
Add_Suffix myWindow = GetWindow<Add_Suffix>("窗口:添加所有子级后缀");
Vector2 maxWinSize = new Vector2(300, 40);
myWindow.maxSize = maxWinSize;
myWindow.minSize = maxWinSize;
myWindow.Show();
//初始化静态变量
addSuffix = null;
}
}
//窗口GUI逻辑
private void OnGUI()
{
EditorGUILayout.BeginHorizontal();//垂直布局开始
GUILayout.Label("增加后缀:", GUILayout.Width(80));
addSuffix = GUILayout.TextField(addSuffix, GUILayout.Width(200));
EditorGUILayout.EndHorizontal();//垂直布局结束
EditorGUILayout.BeginHorizontal();//垂直布局开始
//GUILayout.FlexibleSpace();//灵活空白
GUILayout.Space(140);//自定义长度空白
if (GUILayout.Button("确定", GUILayout.Width(145)))
{
if (addSuffix != null && addSuffix.Trim() != "")
{
ChangeData();//修改子级信息
}
this.Close();
}
EditorGUILayout.EndHorizontal();//垂直布局结束
}
private void ChangeData()//修改子级信息
{
GameObject[] fathers = Selection.gameObjects;
for(int i=0;i<fathers.Length;i++)
{
Transform[] fatherTrans = fathers[i].GetComponentsInChildren<Transform>();
foreach (var child in fatherTrans)
{
child.gameObject.name = child.gameObject.name + addSuffix;
}
}
}
}
删减GameObject后缀
参考源:https://www.cnblogs.com/sntetwt/p/6060860.html
实现功能:
(1)单选/多选 物体,填写后缀名,即可为其本身及子级添加后缀名
面板展示:
代码编写:
using UnityEngine;
using UnityEditor;
using System;
public class Subtract_Suffix : EditorWindow
{
static string subtractSuffix = null;//后缀
static string subtractNum = null;//删减数量
[MenuItem("GameObject/物体批量操作(单选|多选)/寻找物体所有子级并删减后缀", false, 1)]
static void FindAllSubsetsAndAddSuffixes()
{
if (Selection.gameObjects.Length > 0)
{
//窗口大小及呈现
Subtract_Suffix myWindow = GetWindow<Subtract_Suffix>("窗口:删减所有子级后缀");
Vector2 maxWinSize = new Vector2(300, 40);
myWindow.maxSize = maxWinSize;
myWindow.minSize = maxWinSize;
myWindow.Show();
//初始化静态变量
subtractSuffix = null;
subtractNum = null;
}
}
//窗口GUI逻辑
private void OnGUI()
{
EditorGUILayout.BeginHorizontal();//垂直布局开始
GUILayout.Label("删减后缀:", GUILayout.Width(80));
subtractSuffix = GUILayout.TextField(subtractSuffix, GUILayout.Width(200));
EditorGUILayout.EndHorizontal();//垂直布局结束
EditorGUILayout.BeginHorizontal();//垂直布局开始
GUILayout.Label("删减数量:", GUILayout.Width(80));
subtractNum = GUILayout.TextField(subtractNum, GUILayout.Width(50));
if (GUILayout.Button("确定", GUILayout.Width(145)))
{
if (subtractSuffix != null && subtractSuffix.Trim() != "")
{
ChangeData();//修改子级信息
}
this.Close();
}
EditorGUILayout.EndHorizontal();//垂直布局结束
}
private void ChangeData()//修改子级信息
{
GameObject[] fathers = Selection.gameObjects;
for (int i = 0; i < fathers.Length; i++)
{
Transform[] fatherTrans = fathers[i].GetComponentsInChildren<Transform>();
foreach (var child in fatherTrans)
{
if(child.gameObject.name.EndsWith(subtractSuffix))
{
string childName = child.gameObject.name;
int subtractSuffixLength = subtractSuffix.Length;//输入后缀长度
//判断输入删减数是否为整数 | 输入是否为空 | 输入是否为空格
if(IsNumberic(subtractNum) && subtractNum != null && subtractNum.Trim() != "")
{
if(int.Parse(subtractNum) <= subtractSuffixLength)//删减长度不能大于输入的后缀长度
{
subtractSuffixLength = int.Parse(subtractNum);
}
}
childName = childName.Remove(childName.Length - subtractSuffixLength);
child.gameObject.name = childName;
}
}
}
}
public bool IsNumberic(string oText)//判断是否能转化为整数类型
{
try{
int intNum = Convert.ToInt32(oText);
return true;
}
catch
{
return false;
}
}
}