[ InitializeOnLoadMethod ]
作用及描述:允许在 Unity 加载时初始化编辑器类方法,无需用户操作。(初始化时调用)
=>的作用:https://blog.csdn.net/qq_40544338/article/details/103599590
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class TestScene : MonoBehaviour
{
//使用SceneView类
[InitializeOnLoadMethod]
static void IntiallizeOnLoad()
{
UnityEditor.SceneView.duringSceneGui += (sceneView) =>
{
if (Event.current != null && Event.current.button == 1 && Event.current.type == EventType.MouseUp)
{
Debug.Log("鼠标抬起");
Rect position = new Rect(Event.current.mousePosition.x,Event.current.mousePosition.y-100, 100, 100);
GUIContent[] contents = new GUIContent[] { new GUIContent("Test1"), new GUIContent("Test2") };
EditorUtility.DisplayCustomMenu(position, contents,-1,(data,opt,select) => {
Debug.LogFormat("data:{0},opt:{1},select:{2},value:{3}", data, opt, select, opt[select]);
},null);
Event.current.Use();
}
};
}
}
Scene视图Gizoms
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestSceneGizmos : MonoBehaviour
{
//在Editor一般不编译,所以需要将文件转移到其他地方
//实现场景中绘制内容
public Color color;
#if UNITY_EDITOR //只能在Editor编译
private void OnDrawGizmosSelected()//仅选中绘制
{
Gizmos.color = color;
Gizmos.DrawLine(transform.position, transform.position + Vector3.up);
Gizmos.DrawSphere(transform.position, 1);
}
private void OnDrawGizmos()//不选中也绘制
{
Gizmos.DrawIcon(transform.position, "Assets/Texture/logo.png");
}
#endif
}
Scene视图添加UI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(TestSceneGizmos))]
public class TestSceneGUI : Editor
{
private void OnSceneGUI()
{
Handles.BeginGUI();
if (GUILayout.Button("Click Here",GUILayout.Width(100) ))
{
Debug.Log(target.name);
}
GUILayout.Label(target.name);
Handles.EndGUI();
}
}
Scene常驻UI
[InitializeOnLoadMethod]
static void InitializeOnLoad()
{
SceneView.duringSceneGui += (sceneView) =>
{
Handles.BeginGUI();
if(GUILayout.Button("test2",GUILayout.Width(200))){
Debug.Log("test2");
}
GUILayout.Label("固定UI");
Handles.EndGUI();
};
}