using Adam; using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; //============================================================ //支持中文,文件使用UTF-8编码 //@author Adam //@create 20230417 //@company Adam // //@description: //============================================================ public class MyEditor : Editor { #if UNITY_EDITOR [MenuItem("MyTest/MyEditor")] #endif public static void OpenEditorWindow() { MyEditorWindow.ShowWindow(); } } public class MyEditorWindow : EditorWindow { public static MyEditorWindow instance; [SerializeField]//必须要加 protected List _assetLst = new List(); //序列化对象 protected SerializedObject _serializedObject; //序列化属性 protected SerializedProperty _assetLstProperty; //public GameObject flower1; //public GameObject flower2; //public GameObject plant1; //public GameObject plant2; public GameObject target; public float max; public float min; public static void ShowWindow() { instance = GetWindow(); instance.Init(); } public void Init() { SceneView.duringSceneGui += OnSceneGUI; //使用当前类初始化 _serializedObject = new SerializedObject(this); //获取当前类中可序列话的属性 _assetLstProperty = _serializedObject.FindProperty("_assetLst"); } private void OnGUI() { ////绘制对象 //GUILayout.Space(10); //flower1 = (GameObject)EditorGUILayout.ObjectField("Buggy Game Object", flower1, typeof(GameObject), true); //GUILayout.Space(10); //flower2 = (GameObject)EditorGUILayout.ObjectField("Buggy Game Object", flower2, typeof(GameObject), true); //GUILayout.Space(10); //min = (GameObject)EditorGUILayout.ObjectField("MinScale", plant2, typeof(GameObject), true); GUILayout.Space(10); target = (GameObject)EditorGUILayout.ObjectField("Buggy Game Object", target, typeof(GameObject), true); //更新 _serializedObject.Update(); //开始检查是否有修改 EditorGUI.BeginChangeCheck(); //显示属性 //第二个参数必须为true,否则无法显示子节点即List内容 EditorGUILayout.PropertyField(_assetLstProperty, true); //结束检查是否有修改 if (EditorGUI.EndChangeCheck()) {//提交修改 _serializedObject.ApplyModifiedProperties(); } } public void OnSceneGUI(SceneView sceneView) { //不能用input,因为不是运行时 if (Event.current.type == EventType.MouseDown && Event.current.button == 0) { Debug.Log("mouse left click!"); } else if (Event.current.type == EventType.MouseDown && Event.current.button == 1) { //使用HandleUtility来进行编辑器的操作 Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { if (hit.collider != null) { int index = Random.Range(0, _assetLst.Count - 1); GameObject obj = GameObject.Instantiate( (GameObject)_assetLst[index]); obj.transform.localPosition = hit.point; //obj.transform.LookAt(target.transform); obj.transform.localScale = new Vector3(Random.Range(1f, 1.5f), Random.Range(1f, 1.5f), Random.Range(1f, 1.5f)); obj.transform.localEulerAngles = new Vector3(0,Random.Range(0f, 360f),0); Debug.Log("mouse right click : " + hit.collider.gameObject.layer.ToString()); } } } } }