using UnityEngine;
using UnityEditor.UI;
using UnityEditor;
using XRTool.Util;
namespace XRTool.WorldUI
{
///
/// Image 3D
///
[InitializeOnLoad]
[CustomEditor(typeof(XRImage3D))]
public class XRImage3DEditor : ImageEditor
{
private SerializedProperty isAutoScale;
private SerializedProperty thickness;
private SerializedProperty simple;
private Mesh mesh;
private Material mate;
private SerializedProperty scale;
private XRImage3D image3D;
private Vector2 lastSize;
private Vector3 axis;
[MenuItem("GameObject/XRUI/XRUIModel/XRImage3D", priority = 0)]
static void Init()
{
var obj = Instantiate(Resources.Load(typeof(XRImage3D).Name));
obj.name = (typeof(XRImage3D).Name);
if (obj)
{
var parent = Selection.activeGameObject;
if (!parent)
{
var canvas = GameObject.FindObjectsOfType(typeof(Canvas));
if (canvas != null)
{
for (int i = 0; i < canvas.Length; i++)
{
if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace)
{
parent = (canvas[i] as Canvas).gameObject;
break;
}
}
}
}
UnityUtil.SetParent(parent ? parent.transform : null, obj.transform);
Selection.activeGameObject = obj.gameObject;
}
}
protected override void OnEnable()
{
base.OnEnable();
image3D = target as XRImage3D;
isAutoScale = serializedObject.FindProperty("isAutoScale");
thickness = serializedObject.FindProperty("thickness");
scale = serializedObject.FindProperty("scale");
simple = serializedObject.FindProperty("simple");
lastSize = image3D.rectTransform.rect.size;
image3D.AutoSetSize();
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
EditorGUILayout.PropertyField(isAutoScale, new GUIContent("等比缩放"));
if (!image3D.isAutoScale)
{
EditorGUILayout.PropertyField(thickness, new GUIContent("厚度"));
}
mesh = (Mesh)EditorGUILayout.ObjectField("模型", mesh, typeof(Mesh));
mate = (Material)EditorGUILayout.ObjectField("材质", mate, typeof(Material));
EditorGUILayout.PropertyField(simple, new GUIContent("图片"));
//icon3D.img = (Texture2D)EditorGUILayout.ObjectField("Icon", icon3D.img, typeof(Texture2D));
//EditorGUILayout.PropertyField(mesh, new GUIContent("mesh"));
EditorGUILayout.PropertyField(scale, new GUIContent("模型比例"));
axis = EditorGUILayout.Vector3Field("模型偏转角(暂不支持)", axis);
serializedObject.ApplyModifiedProperties();
if (image3D.rectTransform.rect.size != lastSize)
{
lastSize = image3D.rectTransform.rect.size;
image3D.AutoSetSize();
}
if (GUI.changed)
{
if (image3D)
{
if (!image3D.isAutoScale)
{
image3D.SetThickness();
}
image3D.SetRenderMesh(mesh);
image3D.SetRenderMate(mate);
image3D.AutoSetSize();
image3D.SetSimple();
}
}
}
}
}