XRTextEditor.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor.UI;
  5. using UnityEditor;
  6. using XRTool.Util;
  7. using TMPro;
  8. namespace XRTool.WorldUI
  9. {
  10. [InitializeOnLoad]
  11. [CustomEditor(typeof(XRText))]
  12. public class XRTextEditor : UnityEditor.UI.TextEditor
  13. {
  14. private SerializedProperty textSpacing;
  15. protected override void OnEnable()
  16. {
  17. base.OnEnable();
  18. textSpacing = serializedObject.FindProperty("textSpacing");
  19. }
  20. public override void OnInspectorGUI()
  21. {
  22. base.OnInspectorGUI();
  23. EditorGUILayout.PropertyField(textSpacing, new GUIContent("字间距"));
  24. serializedObject.ApplyModifiedProperties();
  25. }
  26. [MenuItem("GameObject/XRUI/XRText/XRText", priority = 10)]
  27. static void Init()
  28. {
  29. var obj = Instantiate(Resources.Load<XRText>(typeof(XRText).Name));
  30. obj.name = (typeof(XRText).Name);
  31. if (obj)
  32. {
  33. var parent = Selection.activeGameObject;
  34. if (!parent)
  35. {
  36. var canvas = GameObject.FindObjectsOfType(typeof(Canvas));
  37. if (canvas != null)
  38. {
  39. for (int i = 0; i < canvas.Length; i++)
  40. {
  41. if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace)
  42. {
  43. parent = (canvas[i] as Canvas).gameObject;
  44. break;
  45. }
  46. }
  47. }
  48. }
  49. UnityUtil.SetParent(parent ? parent.transform : null, obj.transform);
  50. obj.transform.localScale = Vector3.one * 0.5f;
  51. Selection.activeGameObject = obj.gameObject;
  52. }
  53. }
  54. [MenuItem("GameObject/XRUI/XRText/XRTextMesh", priority = 0)]
  55. static void InitTextMesh()
  56. {
  57. var obj = Instantiate(Resources.Load<TextMesh>(typeof(TextMesh).Name));
  58. obj.name = (typeof(TextMesh).Name);
  59. if (obj)
  60. {
  61. var parent = Selection.activeGameObject;
  62. if (!parent)
  63. {
  64. var canvas = GameObject.FindObjectsOfType(typeof(Canvas));
  65. if (canvas != null)
  66. {
  67. for (int i = 0; i < canvas.Length; i++)
  68. {
  69. if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace)
  70. {
  71. parent = (canvas[i] as Canvas).gameObject;
  72. break;
  73. }
  74. }
  75. }
  76. }
  77. UnityUtil.SetParent(parent ? parent.transform : null, obj.transform);
  78. obj.transform.localScale = Vector3.one * 5;
  79. Selection.activeGameObject = obj.gameObject;
  80. }
  81. }
  82. //[MenuItem("GameObject/XRUI/XRText/TextMeshPro", priority = 0)]
  83. //static void InitTextMeshPro()
  84. //{
  85. // var obj = Instantiate(Resources.Load<TextMeshPro>(typeof(TextMeshPro).Name));
  86. // obj.name = (typeof(TextMeshPro).Name);
  87. // if (obj)
  88. // {
  89. // var parent = Selection.activeGameObject;
  90. // if (!parent)
  91. // {
  92. // var canvas = GameObject.FindObjectsOfType(typeof(Canvas));
  93. // if (canvas != null)
  94. // {
  95. // for (int i = 0; i < canvas.Length; i++)
  96. // {
  97. // if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace)
  98. // {
  99. // parent = (canvas[i] as Canvas).gameObject;
  100. // break;
  101. // }
  102. // }
  103. // }
  104. // }
  105. // UnityUtil.SetParent(parent ? parent.transform : null, obj.transform);
  106. // Selection.activeGameObject = obj.gameObject;
  107. // }
  108. //}
  109. }
  110. }