XRImageEditor.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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(XRImage))]
  11. public class XRImageEditor : ImageEditor
  12. {
  13. private SerializedProperty isNeedBg;
  14. private SerializedProperty thickness;
  15. private SerializedProperty isNeedFront;
  16. private SerializedProperty isAutoScale;
  17. private XRImage xRImage;
  18. private Vector2 size;
  19. public Material mate;
  20. [MenuItem("GameObject/XRUI/XRUIModel/XRImage", priority = 0)]
  21. static void Init()
  22. {
  23. var obj = Instantiate(Resources.Load<XRImage>(typeof(XRImage).Name));
  24. obj.name = (typeof(XRImage).Name);
  25. if (obj)
  26. {
  27. var parent = Selection.activeGameObject;
  28. if (!parent)
  29. {
  30. var canvas = GameObject.FindObjectsOfType(typeof(Canvas));
  31. if (canvas != null)
  32. {
  33. for (int i = 0; i < canvas.Length; i++)
  34. {
  35. if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace)
  36. {
  37. parent = (canvas[i] as Canvas).gameObject;
  38. break;
  39. }
  40. }
  41. }
  42. }
  43. UnityUtil.SetParent(parent ? parent.transform : null, obj.transform);
  44. Selection.activeGameObject = obj.gameObject;
  45. }
  46. }
  47. protected override void OnEnable()
  48. {
  49. base.OnEnable();
  50. xRImage = target as XRImage;
  51. xRImage.InitComponent();
  52. isNeedBg = serializedObject.FindProperty("isNeedBg");
  53. thickness = serializedObject.FindProperty("thickness");
  54. isNeedFront = serializedObject.FindProperty("isNeedFront");
  55. isAutoScale = serializedObject.FindProperty("isAutoScale");
  56. size = xRImage.rectTransform.rect.size * WorldDlg.UIScale;
  57. if (xRImage.isAutoScale)
  58. {
  59. xRImage.rectTransform.localScale = Vector3.one / WorldDlg.UIScale;
  60. }
  61. xRImage.UpdateSize(size/ WorldDlg.UIScale);
  62. xRImage.SetBoundThickness();
  63. }
  64. public override void OnInspectorGUI()
  65. {
  66. base.OnInspectorGUI();
  67. EditorGUILayout.PropertyField(isNeedFront, new GUIContent("世界面板"));
  68. EditorGUILayout.PropertyField(thickness, new GUIContent("面板厚度"));
  69. EditorGUILayout.PropertyField(isNeedBg, new GUIContent("背景板"));
  70. EditorGUILayout.PropertyField(isAutoScale, new GUIContent("自动缩放"));
  71. mate = (Material)EditorGUILayout.ObjectField("背景材质",mate,typeof(Material));
  72. if (xRImage.rectTransform.rect.size != size)
  73. {
  74. size=xRImage.rectTransform.rect.size;
  75. xRImage.UpdateSize(size);
  76. }
  77. serializedObject.ApplyModifiedProperties();
  78. if (GUI.changed)
  79. {
  80. if (xRImage)
  81. {
  82. xRImage.SetFrontActive();
  83. xRImage.SetBackActive();
  84. xRImage.SetBoundThickness();
  85. if (mate)
  86. {
  87. xRImage.SetMainMater(mate);
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }