Unity EditorWindow API case

void OnEnable(){}//初始化
void OnGUI(){}//绘制
void OnDisable()//关闭

GetWindow<TWindow>(true, "Generate Window", true);//打开自定义窗口

è¿éåå¾çæè¿ °

è¿éåå¾çæè¿ °

   //利用构造函数来设置窗口名称
    MyFirstWindow()
    {
        this.titleContent = new GUIContent("Bug Reporter");
    }

GUIContent çé ¢ åå®¹ç ± »

è¿éåå¾çæè¿ °

è¿éåå¾çæè¿ °

//Toggle开关按钮、BeginToggleGroup开关区域
bool showBtn = true;
void OnGUI()
    {
        showBtn = EditorGUILayout.Toggle("Show Button",showBtn);
        if(showBtn){  //开关点开
            if(GUILayout.Button("Close")){ //绘制按钮
                this.Close(); //关闭面板
            }
        }
      }
private bool groupEnabled; //区域开关
void OnGUI(){
groupEnabled = EditorGUILayout.BeginToggleGroup("Optional Settings", groupEnabled);
---
EditorGUILayout.EndToggleGroup();}
//创建密码字段并可视化在密码字段有什么键入。
    string text = "Some text here";
    bool showBtn = true;
    void OnGUI() {
        text = EditorGUILayout.PasswordField("Password:",text);
        showBtn = EditorGUILayout.Toggle("Show Button", showBtn);
        if (showBtn)
        {
            EditorGUILayout.LabelField("mima:", text);
        }
    }
}
//整数字段 IntField :返回整数,由用户输入的值 
//浮点数字段 FloatField :返回小数,由用户输入的值 
int clones= 1;
    void OnGUI() {
        clones= EditorGUILayout.IntField("Number of clones:", clones);
}
//5.Slider 滑动条     
float scale = 0.0f; 
    void OnGUI()
    {
        scale = EditorGUILayout.Slider(scale,1,100);
    }
//随机放置选择的物体在最小最大滑动条之间
    float  minVal = -10.0f;
    float minLimit = -20.0f;
    float maxVal = 10.0f;
    float maxLimit = 20.0f;
    void OnGUI()
    {
        EditorGUILayout.LabelField("Min Val:", minVal.ToString());
        EditorGUILayout.LabelField("Max Val:", maxVal.ToString());
        EditorGUILayout.MinMaxSlider(ref minVal,ref  maxVal, minLimit, maxLimit);
 
    }
//Popup弹出选择菜单 
string[] options = { "Cube","Sphere","Plane"};
    int index = 0;
    void OnGUI()
    {
        index = EditorGUILayout.Popup(index, options);
    }
//EnumPopup 枚举弹出选择菜单(效果同上)
enum OPTIONS
{
    CUBE = 0,
    SPHERE = 1,
    PLANE = 2
}
public class myEditor3 : EditorWindow {
    OPTIONS op=OPTIONS.CUBE;
     [MenuItem("cayman/tempShow")]
    static void newWelcome()
    {
        EditorWindow.GetWindow(typeof(myEditor3), true, "Eam");
    }
    void OnGUI()
    {
       op = (OPTIONS)EditorGUILayout.EnumPopup("Primitive to create:", op)  ;
    }
    }
//Toolbar工具栏
int m_SelectedPage=0;
string[] m_ButtonStr=new string[4]{"Combine Animation","Check Part","Create RootMotion","CheckunUsedPrefab"};
void OnGUI(){
    m_SelectedPage=GUILayout.Toolbar(m_SelectedPage,m_ButtonStr,GUILayout.Height(25));
}
//ColorField 颜色字段  
Color matColor = Color.white;
    void OnGUI()
    {
        matColor = EditorGUILayout.ColorField("New Color", matColor);
 
    }
//Vector2Field 二维向量字段 Vector3Field 三维向量字段(略,同2维) 
float distance = 0;
    Vector2 p1, p2;
    void OnGUI()
    {
        p1 = EditorGUILayout.Vector2Field("Point 1:", p1);
        p2 = EditorGUILayout.Vector2Field("Point 2:", p2);
        EditorGUILayout.LabelField("Distance:", distance.ToString());
    }
    void OnInspectorUpdate() //面板刷新
    {
        distance = Vector2.Distance(p1, p2);
        this.Repaint();
    }
//TagField 标签字段 LayerField层字段 
string tagStr = "";
    int selectedLayer=0;
    void OnGUI()
    {  //为游戏物体设置
        tagStr = EditorGUILayout.TagField("Tag for Objects:", tagStr);
        if (GUILayout.Button("Set Tag!"))
            SetTags();
        if(GUILayout.Button("Set Layer!"))
            SetLayer();
    }
    void SetTags() {
        foreach(GameObject go in Selection.gameObjects)
            go.tag = tagStr;
    }
     void SetLayer() {
        foreach(GameObject go in Selection.gameObjects)
            go.laye = tagStr;
    }
//IntPopup 整数弹出选择菜单 
int selectedSize = 1;
    string[] names = { "Normal","Double","Quadruple"};
    int[] sizes = { 1,2,4};
    void OnGUI()
    {
        selectedSize = EditorGUILayout.IntPopup("Resize Scale: ", selectedSize, names, sizes);
        if (GUILayout.Button("Scale"))
            ReScale();
    }
    void ReScale()
    {
        if (Selection.activeTransform)
            Selection.activeTransform.localScale =new Vector3(selectedSize, selectedSize, selectedSize);
        else Debug.LogError("No Object selected, please select an object to scale.");
    }
//打开保存位置文件夹
GUILayout.Label ("Save Path", EditorStyles.boldLabel);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.TextField(path,GUILayout.ExpandWidth(false));
if(GUILayout.Button("Browse",GUILayout.ExpandWidth(false)))
            path = EditorUtility.SaveFolderPanel("Path to Save Images",path,Application.dataPath);   //打开保存文件夹面板
EditorGUILayout.EndHorizontal();

bool Foldout(bool value, string label) folding label;

//滑动区域 BeginScrollView  选择网格 SelectionGrid
Vector2 v2 = new Vector2(0,0);
Int32 v = 0;
string[] str = { "Message1", "Message2", "Message3", "Message4" };
 
GUIStyle textStyle = new GUIStyle("textfield");
GUIStyle buttonStyle = new GUIStyle("button");
textStyle.active = buttonStyle.active;
textStyle.onNormal = buttonStyle.onNormal;
 
v2 = GUILayout.BeginScrollView(v2, true, true, GUILayout.Width(300), GUILayout.Height(100));
{
    v = GUILayout.SelectionGrid(v,str,1,textStyle);
}
GUILayout.EndScrollView();
//通过拖拽获取文件路径
string path;
Rect rect;
 void OnGUI()
    {
        EditorGUILayout.LabelField("路径");
        //获得一个长300的框  
        rect = EditorGUILayout.GetControlRect(GUILayout.Width(300));
        //将上面的框作为文本输入框  
        path = EditorGUI.TextField(rect, path);
 
        //如果鼠标正在拖拽中或拖拽结束时,并且鼠标所在位置在文本输入框内  
        if ((Event.current.type == EventType.DragUpdated
          || Event.current.type == EventType.DragExited)
          && rect.Contains(Event.current.mousePosition))
        {
            //改变鼠标的外表  
            DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
            if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0)
            {
                path = DragAndDrop.paths[0];
            }
        }
    }

Tips:
1/ Open a notification bar
this.ShowNotification(new GUIContent("This is a Notification"));

2/ Close the notification bar
this.RemoveNotification();

3/

//更新
    void Update()
    {
 
    }
 
    void OnFocus()
    {
        Debug.Log("当窗口获得焦点时调用一次");
    }
 
    void OnLostFocus()
    {
        Debug.Log("当窗口丢失焦点时调用一次");
    }
 
    void OnHierarchyChange()
    {
        Debug.Log("当Hierarchy视图中的任何对象发生改变时调用一次");
    }
 
    void OnProjectChange()
    {
        Debug.Log("当Project视图中的资源发生改变时调用一次");
    }
 
    void OnInspectorUpdate()
    {
       //Debug.Log("窗口面板的更新");
       //这里开启窗口的重绘,不然窗口信息不会刷新
       this.Repaint();
    }
 
    void OnSelectionChange()
    {
        //当窗口出去开启状态,并且在Hierarchy视图中选择某游戏对象时调用
        foreach(Transform t in Selection.transforms)
        {
            //有可能是多选,这里开启一个循环打印选中游戏对象的名称
            Debug.Log("OnSelectionChange" + t.name);
        }
    }
 
    void OnDestroy()
    {
        Debug.Log("当窗口关闭时调用");
    }

4/ Close the panel
this.Close();

5/ Many are the same as the previous article, such as:
EditorGUILayout.HelpBox("The default mode",MessageType.None);//Help information

6/ Some formats:
Label can use font: EditorStyles.boldLabel
button or something can limit the size:
GUILayout.MaxWidth(160),
GUILayout.MinHeight(60),
GUILayout.ExpandWidth(false)

//绘制描述文本区域
GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.Label("Description", GUILayout.MaxWidth(80));
description = EditorGUILayout.TextArea(description, GUILayout.MaxHeight(75));
GUILayout.EndHorizontal();
//绘制对象槽
GUILayout.Space(10);
buggyGameObject = (GameObject)EditorGUILayout.ObjectField("Buggy Game Object", buggyGameObject, typeof(GameObject), true);
//绘制文本TextField
GUILayout.Space(10);
bugReporterName = EditorGUILayout.TextField("Bug Name", bugReporterName);

Open folder panel selection

EditorUtility.OpenFolderPanel

Guess you like

Origin blog.csdn.net/Momo_Da/article/details/98659025