InventoryEngineTool.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine.UI;
  6. namespace SoftKitty.InventoryEngine
  7. {
  8. public class InventoryEngineTool : EditorWindow
  9. {
  10. private List<GameObject> _textComponents = new List<GameObject>();
  11. private UiStyle _styleScript;
  12. private bool _fold = false;
  13. private TMPro.TMP_FontAsset _font;
  14. Vector2 _scroll;
  15. void OnGUI()
  16. {
  17. GUIStyle box = GUI.skin.button;
  18. GUIStyle header = new GUIStyle();
  19. header.fontSize = 20;
  20. header.alignment = TextAnchor.MiddleCenter;
  21. GUILayout.BeginVertical();
  22. GUILayout.BeginHorizontal();
  23. GUI.color = Color.green;
  24. GUILayout.Label("TextMeshPro Converter");
  25. GUI.color = Color.white;
  26. GUILayout.EndHorizontal();
  27. EditorGUILayout.Separator();
  28. if (_styleScript == null && (Selection.activeGameObject == null || !Selection.activeGameObject.GetComponent<UiStyle>()))
  29. {
  30. GUI.color = Color.red;
  31. GUILayout.BeginHorizontal();
  32. GUILayout.Label("Please select the UI window object.");
  33. GUILayout.EndHorizontal();
  34. GUI.color = Color.white;
  35. }
  36. else
  37. {
  38. if (Selection.activeGameObject != null && Selection.activeGameObject.GetComponent<UiStyle>())
  39. {
  40. GUILayout.BeginHorizontal();
  41. GUI.color = Color.green;
  42. if (GUILayout.Button("Find all Texts (" + Selection.activeGameObject.name + ")"))
  43. {
  44. Text[] _texts = Selection.activeGameObject.GetComponentsInChildren<Text>();
  45. _styleScript = Selection.activeGameObject.GetComponent<UiStyle>();
  46. _textComponents.Clear();
  47. foreach (Text obj in _texts)
  48. {
  49. _textComponents.Add(obj.gameObject);
  50. }
  51. }
  52. GUILayout.Space(60);
  53. GUI.color = Color.white;
  54. GUILayout.EndHorizontal();
  55. }
  56. if (_styleScript != null)
  57. {
  58. GUILayout.BeginHorizontal();
  59. _fold = EditorGUILayout.Foldout(_fold, "Text List <" + _styleScript.gameObject.name + "> (" + _textComponents.Count + "):");
  60. if (GUILayout.Button("Clear List"))
  61. {
  62. _textComponents.Clear();
  63. }
  64. GUILayout.Space(60);
  65. GUILayout.EndHorizontal();
  66. GUILayout.BeginHorizontal();
  67. if (_textComponents.Count > 0)
  68. {
  69. GUI.color = _font != null ? Color.yellow : Color.gray;
  70. if (GUILayout.Button("Replace all texts with TMP"))
  71. {
  72. if (_font != null)
  73. {
  74. foreach (GameObject obj in _textComponents)
  75. {
  76. if (obj != null)
  77. {
  78. if (obj.GetComponent<Text>())
  79. {
  80. List<Vector2> _pos = new List<Vector2>();
  81. if (_styleScript != null)
  82. {
  83. for (int x = 0; x < _styleScript.References.Length; x++)
  84. {
  85. for (int y = 0; y < _styleScript.References[x].graphics.Length; y++)
  86. {
  87. if (_styleScript.References[x].graphics[y] == obj.GetComponent<MaskableGraphic>())
  88. {
  89. _pos.Add(new Vector2(x, y));
  90. }
  91. }
  92. }
  93. }
  94. string _text = obj.GetComponent<Text>().text;
  95. Color _color = obj.GetComponent<Text>().color;
  96. int _size = obj.GetComponent<Text>().fontSize;
  97. DestroyImmediate(obj.GetComponent<Text>());
  98. TMPro.TextMeshPro _tmp = obj.AddComponent<TMPro.TextMeshPro>();
  99. _tmp.text = _text;
  100. _tmp.color = _color;
  101. _tmp.fontSize = _size;
  102. _tmp.font = _font;
  103. foreach (var p in _pos)
  104. {
  105. _styleScript.References[Mathf.FloorToInt(p.x)].graphics[Mathf.FloorToInt(p.y)] = obj.GetComponent<MaskableGraphic>();
  106. }
  107. if (obj.GetComponent<Outline>()) DestroyImmediate(obj.GetComponent<Outline>());
  108. if (obj.GetComponent<Shadow>()) DestroyImmediate(obj.GetComponent<Shadow>());
  109. }
  110. }
  111. }
  112. }
  113. else
  114. {
  115. EditorUtility.DisplayDialog("Warning", "Please assign the TMP Font Asset first.", "OK");
  116. }
  117. }
  118. GUILayout.Space(10);
  119. GUI.color = Color.white;
  120. _font = (TMPro.TMP_FontAsset)EditorGUILayout.ObjectField(_font, typeof(TMPro.TMP_FontAsset), false);
  121. }
  122. GUILayout.Space(60);
  123. GUILayout.EndHorizontal();
  124. if (_fold)
  125. {
  126. _scroll = GUILayout.BeginScrollView(_scroll);
  127. foreach (GameObject obj in _textComponents)
  128. {
  129. if (obj != null)
  130. {
  131. GUILayout.BeginHorizontal();
  132. GUILayout.Space(30);
  133. GUI.color = obj.gameObject.GetComponent<TMPro.TMP_Text>() == null ? Color.gray : Color.green;
  134. if (GUILayout.Button(obj.transform.parent.name + "/" + obj.gameObject.name + (obj.gameObject.GetComponent<TMPro.TMP_Text>() == null ? " <Text>" : " <TMP>")))
  135. {
  136. Selection.activeGameObject = obj.gameObject;
  137. }
  138. GUI.color = Color.white;
  139. GUILayout.Space(50);
  140. GUILayout.EndHorizontal();
  141. }
  142. else
  143. {
  144. GUILayout.BeginHorizontal();
  145. GUILayout.Space(30);
  146. GUI.color = Color.red;
  147. GUILayout.Label("Missing");
  148. GUI.color = Color.white;
  149. GUILayout.Space(50);
  150. GUILayout.EndHorizontal();
  151. }
  152. }
  153. GUILayout.EndScrollView();
  154. }
  155. }
  156. }
  157. GUILayout.EndVertical();
  158. }
  159. }
  160. }