Unity3d 编写编辑器自定义插件(2)

来源:互联网 发布:88读书网软件 编辑:程序博客网 时间:2024/06/05 21:58

EditorWindow:编辑器窗口类需要继承的基类

public class EditorWindowExample : EditorWindow{  [MenuItem("Example/Open Window")]  static void Init()  {      var window = GetWindow<EditorWindowExample>();      window.position = new Rect(50, 50, 300, 300);      window.Show();  }  void OnGUI()  {      Event currentEvent = Event.current;      Rect contextRect = new Rect(10, 10, 200, 200);      EditorGUI.DrawRect(contextRect, Color.green);   }}

ScriptableWizard:自定义向导窗口需要继承的基类
官方例子,创建点光源的向导
例子:

public class WizardCreateLight : ScriptableWizard{  public float range = 500;  public Color color = Color.red;  [MenuItem("Example/Create Light Wizard")]  static void CreateWizard()  {      ScriptableWizard.DisplayWizard<WizardCreateLight>("Create Light", "Create", "Apply");  }  void OnWizardCreate()  {     GameObject go = new GameObject("New Light");     Light lt = go.AddComponent<Light>();     lt.range = range;     lt.color = color;  }  void OnWizardUpdate()  {     helpString = "Please set the color of the light!";  }  // When the user pressed the "Apply" button OnWizardOtherButton is called.  void OnWizardOtherButton()  {       if (Selection.activeTransform != null)       {           Light lt = Selection.activeTransform.GetComponent<Light>();           if (lt != null)           {               lt.color = Color.red;           }       }  }}

GenericMenu:用于创建一个右键菜单框和下拉选项框
使用AddItem来添加需要显示的项目,用DropDown来输出一个下拉框,或者用ShowAsContext来显示一个右键菜单框
例子:
GenericMenu menu = new GenericMenu();
//这里Callback是点击对应选项之后的回调函数
menu.AddItem(new GUIContent(“MenuItem1”), false, Callback, “item 1”);
menu.AddItem(new GUIContent(“MenuItem2”), false, Callback, “item 2”);
menu.AddSeparator(“”);
menu.AddItem(new GUIContent(“SubMenu/MenuItem3”), false, Callback, “item 3”);
menu.ShowAsContext();
//上面一行是作为右键菜单显示,下面一行是作为下拉选项显示
//menu.DropDown(contextRect);
currentEvent.Use();

UnityEngine.Event.current: 当前事件类,可以从这个实例中获取用户在编辑器中进行的各种键盘鼠标操作,如按键或者获取鼠标位置

UnityEngine.GL: 一个简单的图形库,用于绘图;
例子:

public class GLExample: MonoBehaviour{  public Material mat;  void OnPostRender()  {      if (!mat)      {        Debug.LogError("Please Assign a material on the inspector");        return;      }      GL.PushMatrix();      mat.SetPass(0);      GL.LoadOrtho();      GL.Color(Color.red);      GL.Begin(GL.LINES);      GL.Vertex3(0.25F, 0.1351F, 0);      GL.Vertex3(0.25F, 0.3F, 0);      GL.End();      GL.Color(Color.yellow);      GL.Begin(GL.TRIANGLES);      GL.Vertex3(0.5F, 0.25F, -1);      GL.Vertex3(0.5F, 0.1351F, -1);      GL.Vertex3(0.1F, 0.25F, -1);      GL.End();      GL.PopMatrix();  }}

上例中画了一条红色的线和一个黄色的三角形。需要注意的是,类中必须要有一个Material类型的参数,并且必须要有赋值,
选择要画的图形的类型之后,根据类型打点,点的个数要和类型相符。
实际测试GL.Color是否有效和Shader有关,还有一种方式是用手写的shader,用不同的pass通道来选择颜色,使用
mat.SetPass(passIndex);来改变当前绘图使用的pass通道,颜色在pass通道中设置
shader代码如下:

Shader "Hidden/Designer/Grid" {    SubShader {        Pass {            CGPROGRAM            #pragma vertex vert_img            #pragma fragment frag            #include "UnityCG.cginc"            fixed4 frag(v2f_img i) : Color {                return fixed4(0.21, 0.21, 0.21, 1);            }            ENDCG        }        Pass {            CGPROGRAM            #pragma vertex vert_img            #pragma fragment frag            #include "UnityCG.cginc"            fixed4 frag(v2f_img i) : Color {                return fixed4(0.5, 0.5, 0.5, 1);            }            ENDCG        }    }}

PropertyDrawerDecoratorDrawer:定制化监视面板

Handles:定制3D GUI控制和场景绘图(也包括编辑器中绘图)。可自动绘制一些特殊图形,如虚线,贝塞尔曲线,扇形,圆环,摄像机等等
好处是可以不用像UnityEngine.GL一样设置Material
例子:
Handles.BeginGUI();
Handles.DrawBezier(beginPoint.Position, endPoint.Position, startTangent, endTangent, color, null, 4f);
Handles.EndGUI();

HandleUtility:帮助函数,多用于转化3D空间到2D GUI,计算距离,转化等。

0 0