5.0自定义场景视图

2021-03-12  本文已影响0人  莫忘初心_倒霉熊

自定义场景视图,需要继承Editor,并放到Editor文件夹中,如图:


image.png

MySceneExample.cs代码如下:

using UnityEngine;

public class MySceneExample : MonoBehaviour
{
}

MySceneEditor.cs代码如下:

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;


[CustomEditor(typeof(MySceneExample))]
public class MySceneEditor : Editor
{
    public List<string> toolBars = new List<string>()
    {
        "ViewMode",
        "MoveMode",
        "RotateMode"
    };

    private int index = 0;
    public Sprite myTest;

    private void OnEnable()
    {
        Debug.Log("OnEnable");
        
        for (int i = 0; i < 10; i++)
        {
            GameObject go = new GameObject(i.ToString());
            if (i < 5)
            {   
                //在Hierarchy隐藏
                go.hideFlags = HideFlags.HideInHierarchy;
            }
            else
            {
                //Transform组件不可编辑
                go.transform.hideFlags = HideFlags.NotEditable;
            }
        }
    }

    public override void OnInspectorGUI()
    {
        myTest = (Sprite)EditorGUILayout.ObjectField("Test", myTest,typeof(Sprite), true);
        
        //绘制一个object自定义的Inspector
        //Editor.CreateEditor(myTest.sprite).OnInspectorGUI();
        //绘制一个object默认的Inspector
        Editor.CreateEditor(myTest).DrawDefaultInspector();
    }
    
    private void OnSceneGUI()
    {
        //scene中的控件绘制必须放到Handles.BeginGUI()与Handles.EndGUI()之间
        Handles.BeginGUI();
        //绘制的区域  GUILayout.ExpandHeight(true)垂直扩展
        GUILayout.BeginArea(new Rect(10f,10f,360f,100));
        //inspector查看器与Editor Window中控件都可以在Scene窗口绘制
        //绘制toolbar控件
        index = GUILayout.Toolbar(index, toolBars.ToArray(), GUILayout.ExpandHeight(true));
        //设置scene操作模式,对应Unity的q,w,e快捷键
        if (index == 0)
        {
            Tools.current = Tool.View;
        }
        else if(index == 1)
        {
            Tools.current = Tool.Move;
        }else if (index == 2)
        {
            Tools.current = Tool.Rotate;
        }
        if (GUILayout.Button("2D/3D Scene模式"))
        {
            //设置scene 为2D模式
            SceneView.currentDrawingSceneView.in2DMode = !SceneView.currentDrawingSceneView.in2DMode;
        }
        GUILayout.EndArea();
        Handles.EndGUI();
        
        //设置焦点类型,失去焦点时,依旧绘制控制
        HandleUtility.AddDefaultControl((GUIUtility.GetControlID(FocusType.Passive)));

        //获取scene场景的相机,输出Scene的宽高
        Camera camera = SceneView.currentDrawingSceneView.camera;
        Debug.Log($"Scene Weight:{camera.pixelWidth},Height:{camera.pixelHeight}");
        //获取鼠标当前位置
        Vector3 mousePos = Event.current.mousePosition;
        Debug.Log($"MousePos:{mousePos}");
        
        if (Event.current.type == EventType.MouseDown)
        {
            Debug.Log("鼠标按下");
        }
        if (Event.current.type == EventType.MouseDrag)
        {
            Debug.Log("鼠标拖动");
        }
        //位置 旋转  HandleCap尺寸大小   吸附尺寸(按住Ctrl拖动)  HandleCap类型
        Camera.main.transform.position = Handles.FreeMoveHandle(Camera.main.transform.position, Camera.main.transform.rotation, 10, Vector3.one*10, Handles.RectangleHandleCap);

        //重新绘制查看器
        Repaint();
    }

    private void OnDisable()
    {
        Debug.Log("OnEnable");
    }

    private void OnDestroy()
    {
        Debug.Log("OnDestroy");
    }
}

效果如图:


自定义场景按钮
DrawDefaultInspector绘制Sprite默认查看器
Transform组件不可编辑
GameObject在Hierarchy面板隐藏
上一篇下一篇

猜你喜欢

热点阅读