1、测试用例
首先新建一个新的C#游戏脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EditorTest : MonoBehaviour {
public Rect mRectValue;
public Texture texture;
}
为了能够动态编辑,需要在编辑器中增加对应的功能:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(EditorTest))]
[ExecuteInEditMode]
//需要继承Editor类
public class MyEditor : Editor {
//重载方法,来绘制控件
public override void OnInspectorGUI()
{
//获取EditorTest类的实例
EditorTest editorTest = (EditorTest)target;
//绘制一个窗口
editorTest.mRectValue = EditorGUILayout.RectField("窗口坐标", editorTest.mRectValue);
//绘制一个贴图槽
editorTest.texture = EditorGUILayout.ObjectField("增加一个贴图", editorTest.texture, typeof(Texture), true) as Texture;
}
}
2、为编辑器自行创建一个新的窗口,并且设置它的窗口布局
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class EditorWindowTest : EditorWindow {
[MenuItem("MyMenu/OpenWindow")]
static void OpenWindow()
{
//创建窗口
Rect wr = new Rect(0, 0, 500, 500);
EditorWindowTest window = (EditorWindowTest)EditorWindow.GetWindowWithRect(typeof(EditorWindowTest), wr, true, "测试创建窗口");
window.Show();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
//输入文字的内容
private string text;
//选择贴图的对象
private Texture texture;
public void Awake()
{
//在资源中读取一张贴图
texture = Resources.Load("1") as Texture;
}
//绘制窗口时调用
private void OnGUI()
{
//输入框控件
text = EditorGUILayout.TextField("输入文字:", text);
if (GUILayout.Button("打开通知", GUILayout.Width(200)))
{
//打开一个通知栏
this.ShowNotification(new GUIContent("This is a Notification"));
}
if (GUILayout.Button("关闭通知", GUILayout.Width(200)))
{
//关闭通知栏
this.RemoveNotification();
}
//文本框显示鼠标在窗口的位置
EditorGUILayout.LabelField("鼠标在窗口的位置", Event.current.mousePosition.ToString());
//选择贴图
texture = EditorGUILayout.ObjectField("添加贴图", texture, typeof(Texture), true) as Texture;
if (GUILayout.Button("关闭窗口", GUILayout.Width(200)))
{
this.Close();
}
}
private void OnFocus()
{
Debug.Log("当窗口获得焦点时调用一次");
}
private void OnLostFocus()
{
Debug.Log("当窗口丢失焦点时调用一次");
}
private void OnHierarchyChange()
{
Debug.Log("当Hierarchy视图中的任何对象发生改变时调用一次");
}
private void OnProjectChange()
{
Debug.Log("当Project视图中的资源发生改变时调用一次");
}
private void OnInspectorUpdate()
{
//窗口面板的更新,开启窗口的重绘
this.Repaint();
}
private void OnSelectionChange()
{
//当窗口处于开启状态,并且在Hierarchy视图中选择某游戏对象时调用
foreach(Transform t in Selection.transforms)
{
//可能多选,这里开启一个循环打印选中游戏对象的名称
Debug.Log("OnSelectinChage" + t.name);
}
}
private void OnDestroy()
{
Debug.Log("当窗口关闭时调用");
}
}
3、拓展场景Scene区域
创建一个空脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SceneTest : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
创建编辑脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(SceneTest))]
public class SceneEditor : Editor {
private void OnSceneGUI()
{
//得到test脚本的对象
SceneTest test = (SceneTest)target;
//绘制文本框
Handles.Label(test.transform.position + Vector3.up * 2, test.transform.name + ":"+test.transform.position.ToString());
//开始绘制GUI
Handles.BeginGUI();
//规定GUI显示区域
GUILayout.BeginArea(new Rect(100, 100, 100, 100));
//GUI绘制一个按钮
if(GUILayout.Button("这是一个按钮!"))
{
Debug.Log("SceneTest");
}
//GUI绘制文本框
GUILayout.Label("我在编辑Scene视图");
GUILayout.EndArea();
Handles.EndGUI();
}
}