XRInputFieldEditor.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using SC.XR.Unity;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEditor.UI;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using XRTool.Util;
  9. namespace XRTool.UI
  10. {
  11. [InitializeOnLoad]
  12. [CustomEditor(typeof(XRInputField))]
  13. public class XRInputFieldEditor : InputFieldEditor
  14. {
  15. [MenuItem("GameObject/XRUI/SCInputField", priority = 6)]
  16. static void Init()
  17. {
  18. var obj = Instantiate(Resources.Load<SCInputField>(typeof(SCInputField).Name));
  19. obj.name = (typeof(SCInputField).Name);
  20. if (obj)
  21. {
  22. var parent = Selection.activeGameObject;
  23. if (!parent)
  24. {
  25. var canvas = GameObject.FindObjectsOfType(typeof(Canvas));
  26. if (canvas != null)
  27. {
  28. for (int i = 0; i < canvas.Length; i++)
  29. {
  30. if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace)
  31. {
  32. parent = (canvas[i] as Canvas).gameObject;
  33. break;
  34. }
  35. }
  36. }
  37. }
  38. UnityUtil.SetParent(parent ? parent.transform : null, obj.transform);
  39. Selection.activeGameObject = obj.gameObject;
  40. if (!obj.textComponent)
  41. {
  42. obj.textComponent = UnityUtil.GetBreadthChild<Text>(obj.transform, "Text");
  43. }
  44. if (!obj.placeholder)
  45. {
  46. obj.placeholder = UnityUtil.GetBreadthChild<Graphic>(obj.transform, "Placeholder");
  47. }
  48. }
  49. }
  50. [MenuItem("GameObject/XRUI/XRInputField", priority = 6)]
  51. static void InitXRInputField()
  52. {
  53. var obj = Instantiate(Resources.Load<XRInputField>(typeof(XRInputField).Name));
  54. obj.name = (typeof(XRInputField).Name);
  55. if (obj)
  56. {
  57. var parent = Selection.activeGameObject;
  58. if (!parent)
  59. {
  60. var canvas = GameObject.FindObjectsOfType(typeof(Canvas));
  61. if (canvas != null)
  62. {
  63. for (int i = 0; i < canvas.Length; i++)
  64. {
  65. if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace)
  66. {
  67. parent = (canvas[i] as Canvas).gameObject;
  68. break;
  69. }
  70. }
  71. }
  72. }
  73. UnityUtil.SetParent(parent ? parent.transform : null, obj.transform);
  74. Selection.activeGameObject = obj.gameObject;
  75. if (!obj.textComponent)
  76. {
  77. obj.textComponent = UnityUtil.GetBreadthChild<Text>(obj.transform, "Text");
  78. }
  79. if (!obj.placeholder)
  80. {
  81. obj.placeholder = UnityUtil.GetBreadthChild<Graphic>(obj.transform, "Placeholder");
  82. }
  83. }
  84. }
  85. }
  86. }