XRImage3DEditor.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using UnityEngine;
  2. using UnityEditor.UI;
  3. using UnityEditor;
  4. using XRTool.Util;
  5. namespace XRTool.WorldUI
  6. {
  7. /// <summary>
  8. /// Image 3D
  9. /// </summary>
  10. [InitializeOnLoad]
  11. [CustomEditor(typeof(XRImage3D))]
  12. public class XRImage3DEditor : ImageEditor
  13. {
  14. private SerializedProperty isAutoScale;
  15. private SerializedProperty thickness;
  16. private SerializedProperty simple;
  17. private Mesh mesh;
  18. private Material mate;
  19. private SerializedProperty scale;
  20. private XRImage3D image3D;
  21. private Vector2 lastSize;
  22. private Vector3 axis;
  23. [MenuItem("GameObject/XRUI/XRUIModel/XRImage3D", priority = 0)]
  24. static void Init()
  25. {
  26. var obj = Instantiate(Resources.Load<XRImage3D>(typeof(XRImage3D).Name));
  27. obj.name = (typeof(XRImage3D).Name);
  28. if (obj)
  29. {
  30. var parent = Selection.activeGameObject;
  31. if (!parent)
  32. {
  33. var canvas = GameObject.FindObjectsOfType(typeof(Canvas));
  34. if (canvas != null)
  35. {
  36. for (int i = 0; i < canvas.Length; i++)
  37. {
  38. if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace)
  39. {
  40. parent = (canvas[i] as Canvas).gameObject;
  41. break;
  42. }
  43. }
  44. }
  45. }
  46. UnityUtil.SetParent(parent ? parent.transform : null, obj.transform);
  47. Selection.activeGameObject = obj.gameObject;
  48. }
  49. }
  50. protected override void OnEnable()
  51. {
  52. base.OnEnable();
  53. image3D = target as XRImage3D;
  54. isAutoScale = serializedObject.FindProperty("isAutoScale");
  55. thickness = serializedObject.FindProperty("thickness");
  56. scale = serializedObject.FindProperty("scale");
  57. simple = serializedObject.FindProperty("simple");
  58. lastSize = image3D.rectTransform.rect.size;
  59. image3D.AutoSetSize();
  60. }
  61. public override void OnInspectorGUI()
  62. {
  63. base.OnInspectorGUI();
  64. EditorGUILayout.PropertyField(isAutoScale, new GUIContent("等比缩放"));
  65. if (!image3D.isAutoScale)
  66. {
  67. EditorGUILayout.PropertyField(thickness, new GUIContent("厚度"));
  68. }
  69. mesh = (Mesh)EditorGUILayout.ObjectField("模型", mesh, typeof(Mesh));
  70. mate = (Material)EditorGUILayout.ObjectField("材质", mate, typeof(Material));
  71. EditorGUILayout.PropertyField(simple, new GUIContent("图片"));
  72. //icon3D.img = (Texture2D)EditorGUILayout.ObjectField("Icon", icon3D.img, typeof(Texture2D));
  73. //EditorGUILayout.PropertyField(mesh, new GUIContent("mesh"));
  74. EditorGUILayout.PropertyField(scale, new GUIContent("模型比例"));
  75. axis = EditorGUILayout.Vector3Field("模型偏转角(暂不支持)", axis);
  76. serializedObject.ApplyModifiedProperties();
  77. if (image3D.rectTransform.rect.size != lastSize)
  78. {
  79. lastSize = image3D.rectTransform.rect.size;
  80. image3D.AutoSetSize();
  81. }
  82. if (GUI.changed)
  83. {
  84. if (image3D)
  85. {
  86. if (!image3D.isAutoScale)
  87. {
  88. image3D.SetThickness();
  89. }
  90. image3D.SetRenderMesh(mesh);
  91. image3D.SetRenderMate(mate);
  92. image3D.AutoSetSize();
  93. image3D.SetSimple();
  94. }
  95. }
  96. }
  97. }
  98. }