增加GameObject后缀

实现功能:

(1)单选/多选 物体,填写后缀名,即可为其本身及子级添加后缀名

面板展示:

image.png

代码编写:

  1. using UnityEngine;
  2. using UnityEditor;
  3. public class Add_Suffix : EditorWindow
  4. {
  5. static string addSuffix = null;//后缀
  6. [MenuItem("GameObject/物体批量操作(单选|多选)/寻找物体所有子级并添加后缀", false, 0)]
  7. static void FindAllSubsetsAndAddSuffixes()
  8. {
  9. if(Selection.gameObjects.Length > 0)
  10. {
  11. //窗口大小及呈现
  12. Add_Suffix myWindow = GetWindow<Add_Suffix>("窗口:添加所有子级后缀");
  13. Vector2 maxWinSize = new Vector2(300, 40);
  14. myWindow.maxSize = maxWinSize;
  15. myWindow.minSize = maxWinSize;
  16. myWindow.Show();
  17. //初始化静态变量
  18. addSuffix = null;
  19. }
  20. }
  21. //窗口GUI逻辑
  22. private void OnGUI()
  23. {
  24. EditorGUILayout.BeginHorizontal();//垂直布局开始
  25. GUILayout.Label("增加后缀:", GUILayout.Width(80));
  26. addSuffix = GUILayout.TextField(addSuffix, GUILayout.Width(200));
  27. EditorGUILayout.EndHorizontal();//垂直布局结束
  28. EditorGUILayout.BeginHorizontal();//垂直布局开始
  29. //GUILayout.FlexibleSpace();//灵活空白
  30. GUILayout.Space(140);//自定义长度空白
  31. if (GUILayout.Button("确定", GUILayout.Width(145)))
  32. {
  33. if (addSuffix != null && addSuffix.Trim() != "")
  34. {
  35. ChangeData();//修改子级信息
  36. }
  37. this.Close();
  38. }
  39. EditorGUILayout.EndHorizontal();//垂直布局结束
  40. }
  41. private void ChangeData()//修改子级信息
  42. {
  43. GameObject[] fathers = Selection.gameObjects;
  44. for(int i=0;i<fathers.Length;i++)
  45. {
  46. Transform[] fatherTrans = fathers[i].GetComponentsInChildren<Transform>();
  47. foreach (var child in fatherTrans)
  48. {
  49. child.gameObject.name = child.gameObject.name + addSuffix;
  50. }
  51. }
  52. }
  53. }

删减GameObject后缀

参考源:https://www.cnblogs.com/sntetwt/p/6060860.html

实现功能:

(1)单选/多选 物体,填写后缀名,即可为其本身及子级添加后缀名

面板展示:

image.png

代码编写:

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. public class Subtract_Suffix : EditorWindow
  5. {
  6. static string subtractSuffix = null;//后缀
  7. static string subtractNum = null;//删减数量
  8. [MenuItem("GameObject/物体批量操作(单选|多选)/寻找物体所有子级并删减后缀", false, 1)]
  9. static void FindAllSubsetsAndAddSuffixes()
  10. {
  11. if (Selection.gameObjects.Length > 0)
  12. {
  13. //窗口大小及呈现
  14. Subtract_Suffix myWindow = GetWindow<Subtract_Suffix>("窗口:删减所有子级后缀");
  15. Vector2 maxWinSize = new Vector2(300, 40);
  16. myWindow.maxSize = maxWinSize;
  17. myWindow.minSize = maxWinSize;
  18. myWindow.Show();
  19. //初始化静态变量
  20. subtractSuffix = null;
  21. subtractNum = null;
  22. }
  23. }
  24. //窗口GUI逻辑
  25. private void OnGUI()
  26. {
  27. EditorGUILayout.BeginHorizontal();//垂直布局开始
  28. GUILayout.Label("删减后缀:", GUILayout.Width(80));
  29. subtractSuffix = GUILayout.TextField(subtractSuffix, GUILayout.Width(200));
  30. EditorGUILayout.EndHorizontal();//垂直布局结束
  31. EditorGUILayout.BeginHorizontal();//垂直布局开始
  32. GUILayout.Label("删减数量:", GUILayout.Width(80));
  33. subtractNum = GUILayout.TextField(subtractNum, GUILayout.Width(50));
  34. if (GUILayout.Button("确定", GUILayout.Width(145)))
  35. {
  36. if (subtractSuffix != null && subtractSuffix.Trim() != "")
  37. {
  38. ChangeData();//修改子级信息
  39. }
  40. this.Close();
  41. }
  42. EditorGUILayout.EndHorizontal();//垂直布局结束
  43. }
  44. private void ChangeData()//修改子级信息
  45. {
  46. GameObject[] fathers = Selection.gameObjects;
  47. for (int i = 0; i < fathers.Length; i++)
  48. {
  49. Transform[] fatherTrans = fathers[i].GetComponentsInChildren<Transform>();
  50. foreach (var child in fatherTrans)
  51. {
  52. if(child.gameObject.name.EndsWith(subtractSuffix))
  53. {
  54. string childName = child.gameObject.name;
  55. int subtractSuffixLength = subtractSuffix.Length;//输入后缀长度
  56. //判断输入删减数是否为整数 | 输入是否为空 | 输入是否为空格
  57. if(IsNumberic(subtractNum) && subtractNum != null && subtractNum.Trim() != "")
  58. {
  59. if(int.Parse(subtractNum) <= subtractSuffixLength)//删减长度不能大于输入的后缀长度
  60. {
  61. subtractSuffixLength = int.Parse(subtractNum);
  62. }
  63. }
  64. childName = childName.Remove(childName.Length - subtractSuffixLength);
  65. child.gameObject.name = childName;
  66. }
  67. }
  68. }
  69. }
  70. public bool IsNumberic(string oText)//判断是否能转化为整数类型
  71. {
  72. try{
  73. int intNum = Convert.ToInt32(oText);
  74. return true;
  75. }
  76. catch
  77. {
  78. return false;
  79. }
  80. }
  81. }