Unity 3D 拓展编辑器 拓展Scene视图

//--辅助元素

Unity 3D 拓展编辑器 拓展Scene视图_第1张图片

using UnityEngine;

public class DrawGizmosMyTools : MonoBehaviour
{
    //--挂在相机上
    //--依赖选择对象
    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawLine(transform.position, Vector3.one);
    }
    //--不依赖选择对象
    private void OnDrawGizmos()
    {
        Gizmos.DrawSphere(transform.position, 1);
    }
}

//--------------------------------------

//-辅助UI

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(Camera))]
public class EditorGUIMyTools : Editor
{
    private void OnSceneGUI()
    {
        Camera camera = target as Camera;
        if (camera)
        {
            Handles.color = Color.red;
            Handles.Label(camera.transform.position, camera.transform.position.ToString());

            Handles.BeginGUI();
            GUI.backgroundColor = Color.red;
            if (GUILayout.Button("click", GUILayout.Width(200.0f)))
            {
                Debug.Log("click = " + camera.name);
            }
            GUILayout.Label("点击红色按钮打印相机名字");
            Handles.EndGUI();
        }
    }
}

//---------------------------------------------

//--常驻辅助UI

Unity 3D 拓展编辑器 拓展Scene视图_第2张图片

using UnityEditor;
using UnityEngine;

public class EdotirGUIAllTimeMyTools
{
    [InitializeOnLoadMethod]
    static void InitializeOnLoadMethod()
    {
        SceneView.onSceneGUIDelegate = delegate (SceneView sceneView)
        {
            Handles.BeginGUI();
            GUI.Label(new Rect(0f, 0f, 150.0f, 15.0f), "常驻UI标题");
            if (GUI.Button(new Rect(0f, 20.0f, 150.0f, 50.0f), "常驻UI按钮"))
            {
                Debug.Log("常驻UI按钮");
            }
            Handles.EndGUI();
        };
    }
}

//-------------------------------------------

//-禁用选中对象

using UnityEditor;
using UnityEngine;

public class StopSelectionMyTools
{
    //--这样Scene场景点击鼠标就不能选中物体了
    [InitializeOnLoadMethod]
    static void InitializeOnLoadMethod()
    {
        SceneView.onSceneGUIDelegate = delegate (SceneView sceneView)
        {
            Event e = Event.current;
            if (e != null)
            {
                int controlID = GUIUtility.GetControlID(FocusType.Passive);
                if (e.type == EventType.Layout)
                {
                    HandleUtility.AddDefaultControl(controlID);
                }
            }
        };
    }
}

 也可在Layers 设置 ,以层为单位设置某个层无法选中(“小锁头”)。

//-------------------------------------

//--选中整个节点

using UnityEngine;

[SelectionBase]
public class SelectionRootMyTools : MonoBehaviour
{
    //--脚本挂在根结点,在Scene场景点击它的子节点物体还是会选中整个物体
}

 

你可能感兴趣的:(Unity,3D,游戏开发,第二版,学习笔记)