XRButtonEditor.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor.UI;
  5. using UnityEditor;
  6. using XRTool.Util;
  7. namespace XRTool.WorldUI
  8. {
  9. [InitializeOnLoad]
  10. [CustomEditor(typeof(XRButton))]
  11. public class XRButtonEditor : ButtonEditor
  12. {
  13. private SerializedProperty thickness;
  14. private SerializedProperty pressDis;
  15. private SerializedProperty isShowBack;
  16. private SerializedProperty isShowBox;
  17. private SerializedProperty pressTime;
  18. private SerializedProperty isTweenOnClick;
  19. private Vector2 size;
  20. private XRButton button;
  21. [MenuItem("GameObject/XRUI/XRButton", priority = 0)]
  22. static void Init()
  23. {
  24. var obj = Instantiate(Resources.Load<XRButton>(typeof(XRButton).Name));
  25. obj.name = (typeof(XRButton).Name);
  26. if (obj)
  27. {
  28. var parent = Selection.activeGameObject;
  29. if (!parent)
  30. {
  31. var canvas = GameObject.FindObjectsOfType(typeof(Canvas));
  32. if (canvas != null)
  33. {
  34. for (int i = 0; i < canvas.Length; i++)
  35. {
  36. if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace)
  37. {
  38. parent = (canvas[i] as Canvas).gameObject;
  39. break;
  40. }
  41. }
  42. }
  43. }
  44. UnityUtil.SetParent(parent ? parent.transform : null, obj.transform);
  45. Selection.activeGameObject = obj.gameObject;
  46. }
  47. }
  48. protected override void OnEnable()
  49. {
  50. base.OnEnable();
  51. thickness = serializedObject.FindProperty("thickness");
  52. pressDis = serializedObject.FindProperty("pressDis");
  53. isShowBack = serializedObject.FindProperty("isShowBack");
  54. isShowBox = serializedObject.FindProperty("isShowBox");
  55. pressTime = serializedObject.FindProperty("pressTime");
  56. isTweenOnClick = serializedObject.FindProperty("isTweenOnClick");
  57. button = target as XRButton;
  58. if (button)
  59. {
  60. size = button.Body.rect.size;
  61. }
  62. }
  63. public override void OnInspectorGUI()
  64. {
  65. base.OnInspectorGUI();
  66. EditorGUILayout.PropertyField(thickness, new GUIContent("按钮厚度"));
  67. EditorGUILayout.PropertyField(pressDis, new GUIContent("压缩比例"));
  68. EditorGUILayout.PropertyField(pressTime, new GUIContent("压缩时间比例"));
  69. EditorGUILayout.PropertyField(isShowBack, new GUIContent("显示背景"));
  70. EditorGUILayout.PropertyField(isShowBox, new GUIContent("显示Box"));
  71. EditorGUILayout.PropertyField(isTweenOnClick, new GUIContent("是否移动"));
  72. if (button.Body.rect.size != size)
  73. {
  74. size = button.Body.rect.size;
  75. button.AutoSetSize();
  76. //AutoSetScale(button.transform,size);
  77. }
  78. serializedObject.ApplyModifiedProperties();
  79. if (GUI.changed)
  80. {
  81. button.Back.gameObject.SetActive(button.isShowBack);
  82. button.Box.gameObject.SetActive(button.isShowBox);
  83. button.UpdateThickness();
  84. }
  85. }
  86. }
  87. }