EditorAuxiliary.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using UnityEditor;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using System.IO;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. [CustomEditor(typeof(Auxiliary))]
  8. public class EditorAuxiliary : Editor
  9. {
  10. #region 变量
  11. private Auxiliary Script;
  12. #endregion
  13. private void OnEnable()
  14. {
  15. Script = (Auxiliary) target;
  16. }
  17. public override void OnInspectorGUI()
  18. {
  19. base.OnInspectorGUI();
  20. if (GUILayout.Button("ChangeFont"))
  21. {
  22. ChangeFont();
  23. }
  24. if (GUILayout.Button("PrintBounds"))
  25. {
  26. PrintBounds();
  27. }
  28. if (GUILayout.Button("ChangePivot"))
  29. {
  30. ChangePivot();
  31. }
  32. if (GUILayout.Button("AddSuffix"))
  33. {
  34. AddSuffix();
  35. }
  36. if (GUILayout.Button("AddPrefix"))
  37. {
  38. AddPrefix();
  39. }
  40. if (GUILayout.Button("ChangePrefix"))
  41. {
  42. ChangePrefix();
  43. }
  44. if (GUILayout.Button("Reset"))
  45. {
  46. //ResetAll();
  47. }
  48. }
  49. private void ResetAll()
  50. {
  51. for (int i = 0; i < Script.GoList.Count; i++)
  52. {
  53. Text[] texts = Script.GoList[i].GetComponentsInChildren<Text>(true); ;
  54. Graphic[] graphics = Script.GoList[i].GetComponentsInChildren<Graphic>(true);
  55. for (int j = 0; j < texts.Length; j++)
  56. {
  57. texts[j].supportRichText = false;
  58. texts[j].verticalOverflow = VerticalWrapMode.Truncate;
  59. texts[j].horizontalOverflow = HorizontalWrapMode.Wrap;
  60. }
  61. for (int j = 0; j < graphics.Length; j++)
  62. {
  63. graphics[j].raycastTarget = true;
  64. }
  65. }
  66. }
  67. private void ChangeFont()
  68. {
  69. for (int j = 0; j < Script.GoList.Count; j++)
  70. {
  71. Transform[] transforms = Script.GoList[j].GetComponentsInChildren<Transform>(true);
  72. for (int i = 0; i < transforms.Length; i++)
  73. {
  74. Text text = transforms[i].GetComponent<Text>();
  75. TextMesh textMesh = transforms[i].GetComponent<TextMesh>();
  76. if (text != null)
  77. {
  78. text.font = Script.Font;
  79. text.fontStyle = FontStyle.Normal;
  80. }
  81. if (textMesh != null)
  82. {
  83. textMesh.font = Script.Font;
  84. textMesh.fontStyle = FontStyle.Normal;
  85. }
  86. }
  87. }
  88. }
  89. private void PrintBounds()
  90. {
  91. Bounds bounds = Script.Go.GetComponent<Renderer>().bounds;
  92. Debug.Log(string.Format("x : {0:0.00} y : {1:0.00} z : {2:0.00}", bounds.extents.x, bounds.extents.y, bounds.extents.z));
  93. }
  94. private void ChangePivot()
  95. {
  96. for (int i = 0; i < Script.SrList.Count; i++)
  97. {
  98. string path = AssetDatabase.GetAssetPath(Script.SrList[i].sprite);
  99. TextureImporter textureImporter = (TextureImporter)AssetImporter.GetAtPath(path);
  100. float offsetX = -Script.SrList[i].transform.position.x * 100 / Script.SrList[i].sprite.texture.width;
  101. float offsetY = -Script.SrList[i].transform.position.y * 100 / Script.SrList[i].sprite.texture.height;
  102. textureImporter.spritePivot = new Vector2(textureImporter.spritePivot.x + offsetX, textureImporter.spritePivot.y + offsetY);
  103. AssetDatabase.ImportAsset(path);
  104. }
  105. }
  106. private void AddSuffix()
  107. {
  108. Transform[] transforms = Script.Go.GetComponentsInChildren<Transform>(true);
  109. for (int i = 0; i < transforms.Length; i++)
  110. {
  111. transforms[i].name = transforms[i].name + Script.String;
  112. }
  113. }
  114. private void AddPrefix()
  115. {
  116. Transform[] transforms = Script.Go.GetComponentsInChildren<Transform>(true);
  117. for (int i = 0; i < transforms.Length; i++)
  118. {
  119. transforms[i].name = Script.String + transforms[i].name;
  120. }
  121. }
  122. private void ChangePrefix()
  123. {
  124. Transform[] transforms = Script.Go.GetComponentsInChildren<Transform>(true);
  125. for (int i = 0; i < transforms.Length; i++)
  126. {
  127. int index = transforms[i].name.IndexOf('_');
  128. transforms[i].name = Script.String + transforms[i].name.Remove(0, index + 1);
  129. }
  130. }
  131. }