BatchReplaceNameTool.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. [ExecuteInEditMode]
  6. public class BatchReplaceNameTool : EditorWindow
  7. {
  8. [MenuItem("Tools/批量修改名称")]
  9. public static void ShowWindow()
  10. {
  11. GetWindow(typeof(BatchReplaceNameTool));
  12. }
  13. bool selectChild = false;
  14. string m_content = "";
  15. string m_replace = "";
  16. Object[] selects;
  17. List<GameObject> selectList = new List<GameObject>();
  18. Vector3 pos = Vector3.zero;
  19. Vector3 pos2 = Vector3.zero;
  20. void OnGUI()
  21. {
  22. titleContent.text = "批量修改名称";
  23. pos = GUILayout.BeginScrollView(pos);
  24. selectChild = EditorGUILayout.Toggle("包括选中子节点", selectChild);
  25. EditorGUILayout.LabelField("已选列表:");
  26. EditorGUI.indentLevel++;
  27. for (int i = 0; i < selectList.Count; i++)
  28. {
  29. EditorGUILayout.ObjectField(selectList[i], typeof(Object),true);
  30. }
  31. EditorGUI.indentLevel--;
  32. GUILayout.EndScrollView();
  33. EditorGUILayout.Space();
  34. EditorGUILayout.LabelField("预览:");
  35. EditorGUI.indentLevel++;
  36. pos2 = GUILayout.BeginScrollView(pos2);
  37. for (int i = 0; i < selectList.Count; i++)
  38. {
  39. string tmp = selectList[i].name;
  40. if (m_content != "")
  41. {
  42. tmp = tmp.Replace(m_content, m_replace);
  43. }
  44. EditorGUILayout.LabelField(tmp);
  45. }
  46. GUILayout.EndScrollView();
  47. EditorGUI.indentLevel--;
  48. m_content = EditorGUILayout.TextField("replace content:", m_content);
  49. m_replace = EditorGUILayout.TextField("replace to:", m_replace);
  50. EditorGUILayout.Space();
  51. if (GUILayout.Button("Repalce!"))
  52. {
  53. if (m_content != "")
  54. {
  55. ChangeName(selectList, m_content, m_replace);
  56. }
  57. }
  58. }
  59. private void Update()
  60. {
  61. selects = Selection.GetFiltered(typeof(GameObject), SelectionMode.Unfiltered);
  62. selectList.Clear();
  63. for (int i = 0; i < selects.Length; i++)
  64. {
  65. if(selectChild)
  66. {
  67. GameObject go = (GameObject)selects[i];
  68. AddChild(go);
  69. }
  70. else
  71. {
  72. selectList.Add((GameObject)selects[i]);
  73. }
  74. }
  75. Repaint();
  76. }
  77. void AddChild(GameObject go)
  78. {
  79. if(!selectList.Contains(go))
  80. {
  81. selectList.Add(go);
  82. foreach (Transform tf in go.transform)
  83. {
  84. AddChild(tf.gameObject);
  85. }
  86. }
  87. }
  88. void ChangeName(List<GameObject> list, string newName,string replaceTo)
  89. {
  90. Undo.RecordObjects(list.ToArray(), "ReplaceName->" + newName);
  91. for (int i = 0; i < list.Count; i++)
  92. {
  93. string tmp = list[i].name;
  94. tmp = tmp.Replace(newName, replaceTo);
  95. list[i].name = tmp;
  96. }
  97. }
  98. }