EditorControl的写法:
复制代码using UnityEngine;using System.Collections;using UnityEditor;
[CustomEditor(typeof(Control))]public class EditorControl : Editor {
// Use this for initialization void Start (){ } // Update is called once per frame void Update (){ }
Control control; void OnEnable() { control = (Control)target; }
public override void OnInspectorGUI() { base.OnInspectorGUI(); if (GUILayout.Button("please",GUILayout.Width(50f))) { control.pref += 1; } }}
该脚本中有用的地方:
1.引用了UnityEditor命名空间;
2.定义的编辑器所编辑的脚本类型是Control类型 ( [CustomEditor(typeof(Control))] );
3.继承自Editor而不是Monobehaviour;
4.OnEnable方法,当物体激活的情况下执行;
5.OnInspectorGUI 这个虚方法需要重写,并且在这个方法下编写的GUI组件,将会呈现在Inspector视口下,从而达到我们要的效果。重点是base.OnInspectorGUI();这句,如果没有这句,Inspector中只会显示OnInspectorGUI()里的GUI元素,不会显示Control里的public变量;如果有这句,既会显示共有变量又会显示OnInspectorGUI()里的GUI元素。
这里我用了个简单的例子,Control的代码如下:
复制代码using UnityEngine;using System.Collections;
public class Control : MonoBehaviour {
public int pref=3;
private float pref2;
// Use this for initialization void Start () { }}
在Inspector下看到的效果如下: