淘先锋技术网

首页 1 2 3 4 5 6 7

目录

官方文档 

常用EditorWindow,GUILayout,EditorGUILayout类

自定义编辑器

创建自定义编辑器三步骤:

GUILayout类: 

EditorGUILayout类

 



官方文档 

常用EditorWindow,GUILayout,EditorGUILayout

用的时候看看这几个类就好.


自定义编辑器

创建自定义编辑器三步骤:

  • 创建一个派生自EditorWindow的脚本
  • 使用代码触发窗口自行显示(EditorWindow.GetWindow)
  • 在OnGUI()中编写GUI代码(GUI,GUILayout,EditorGUI,EditorGUILayout)

Tips:与编辑器相关的脚本都应该放到Editor文件夹里,可以有多个Editor文件夹,然后创建一个继承自EditorWindow的类

GUILayout类: 

1.装饰类控件:

  • GUILayout.Label("label");  
  •  GUILayout.Space(100);

2.滑/滚动类控件:

  • scrollBarValue1 = GUILayout.HorizontalScrollbar(scrollBarValue1,0,0,100,new[]{GUILayout.Width(100)});   
  • scrollBarValue2 = GUILayout.VerticalScrollbar(scrollBarValue2,0,0,100,new[]{GUILayout.Height(100)});

3.按钮类控件:

  • bool a=GUILayout.Button("一个Button");
  • toolbarid=GUILayout.Toolbar(toolbarid, new[] {"1", "2", "3"});

4.Field类控件:

  • GUILayout.TextField("TextField只能一行");
  •  password = GUILayout.PasswordField(password, '*');

5.Toggle类控件:

  • isToggle = GUILayout.Toggle (isToggle,"Toggle");

6.区块布局类控件:

    //保证写的控件只在规定的范围内,这里设置到右下角

  • GUILayout.BeginArea(new Rect(position.width-250,position.height-150,250,150));

  • //...

  • GUILayout.EndArea();



EditorGUILayout类

1.Slider类控件

  • EditorGUILayout.MinMaxSlider("MinMaxSlider",ref sliderValue2,ref sliderValue3,10,20);//可以选择一个最大最小的范围的slider
  • intSliderValue=EditorGUILayout.IntSlider(intSliderValue,0,10);//int类型的slider
  • sliderValue=EditorGUILayout.Slider(sliderValue,0f,1);//和guilayout重复的

2.弹选框类控件

  • flagtype=(flagTestType)EditorGUILayout.EnumFlagsField(flagtype);//这个可以多选 需要枚举类型

3.按钮类控件:

//正常按钮在鼠标按键抬起MouseUp时返回true,他在MouseDown时就立即返回true
if (EditorGUILayout.DropdownButton(new GUIContent("DropdownButton"),FocusType.Keyboard))
{
    Debug.Log("鼠标按下时出现");
}

4.Field类控件:

i1=EditorGUILayout.IntField("IntField",i1,GUILayout.Width(100));
d1=EditorGUILayout.DoubleField("DoubleField",d1,GUILayout.Width(100));
			
//bounds类型输入框 反正就两个三维向量
boundsv=EditorGUILayout.BoundsField("BoundsField",boundsv);
boundintv=EditorGUILayout.BoundsIntField("BoundsIntField",boundintv);
			
//层级和标签\物体选择,就是unity中的各种layer  tag gameboject
EditorGUILayout.LayerField("LayerField", 1);
EditorGUILayout.TagField("TagField", "一个tag");
EditorGUILayout.ObjectField("ObjectField",GameObject.Find("Cube"), typeof(GameObject),true);
			
//显示序列化属性字段 比如之前的自定义特性里的
EditorGUILayout.PropertyField(p)
			
EditorGUILayout.RectField(new Rect());
EditorGUILayout.RectIntField(new RectInt());
			
//delay和一般的区别是只有按下回车或者失去焦点时,才会返回值,就是说你再输入时不会返回
//Note that the return value will not change until the user has pressed enter or focus is moved away from the text field.
delayint=EditorGUILayout.DelayedIntField("DelayedInt",delayint);
delayfloat=EditorGUILayout.DelayedFloatField("DelayedFloat",delayfloat);
delaydouble=EditorGUILayout.DelayedDoubleField("DelayedDouble",delaydouble);
delaystr=EditorGUILayout.DelayedTextField("DelayedTextField",delaystr);
		
colorv=EditorGUILayout.ColorField("颜色框", colorv);
acurev=EditorGUILayout.CurveField("曲线", acurev);
 
//还有这几个向量
//EditorGUILayout.Vector2Field();
//EditorGUILayout.Vector2IntField();
//EditorGUILayout.Vector3Field();
//EditorGUILayout.Vector3IntField();
//EditorGUILayout.Vector4Field()

5.Toggle类控件:

EditorGUILayout.Toggle("toggle",false);
EditorGUILayout.ToggleLeft("ToggleLeft",true);

6.区块布局类控件:

    //保证写的控件只在规定的范围内,这里设置到右下角

  • GUILayout.BeginArea(new Rect(position.width-250,position.height-150,250,150));

  • //...

  • GUILayout.EndArea();

6.折叠功能:

#region 折叠类
//折叠的相关控件 返回bool类型表示开、关
foldout = EditorGUILayout.Foldout(foldout, "折叠Label");
if (foldout)
{
   val1=EditorGUILayout.IntField("111", val1);
   val2=EditorGUILayout.IntField("222", val2);
}
foldout2=EditorGUILayout.InspectorTitlebar(foldout2,GameObject.Find("Cube"));
if (foldout2)
{
   val3=EditorGUILayout.IntField("333", val3);
   val4=EditorGUILayout.IntField("444", val4);
}
#endregion

7.其他控件:

knob=EditorGUILayout.Knob(new Vector2(100, 100), knob, 1, 10, "斤", Color.black, Color.blue, true);

一些七七八八的测试代码:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class XluaDocEditorWindow : EditorWindow
{
    private List<DocModel> docList;
    private float scale = 0.0f;
    private float scrollBarValue1;

    private int gridId;
    private bool isToggle;
    private string url = "www.baidu.com";
    private int intSliderValue;


    [MenuItem("Xlua/Xlua文档")]
    public static void ShowWindow()
    {
        XluaDocEditorWindow myWindow = (XluaDocEditorWindow)EditorWindow.GetWindow(typeof(XluaDocEditorWindow));
        myWindow.minSize = new Vector2(100,100);
        myWindow.Show();
    }

    private void OnEnable()
    {
        docList = new List<DocModel>();
        string docDirPath = Application.dataPath + "/MEComponent/cd_宁波港/Editor";
        string[] docFiles = Directory.GetFiles(docDirPath,"*.doc");//获取,doc文件
        for (int i = 0; i < docFiles.Length; i++)
        {
            string docFile = docFiles[i];
            FileInfo fi = new FileInfo(docFile);//提供用于创建、复制、删除、移动和打开文件的属性和实例方法  "D:/C05/02_AEM/Assets/MEComponent/cd_宁波港/Editor\\XLua_API.doc"
            DocModel docModel = new DocModel();//自己定义的一个类
            docModel.FileName = fi.Name.Split(new char[] {'.'})[0];
            docModel.FilePath = docFile;
            docList.Add(docModel);
        }
    }
    private void OnGUI()
    {
        //实际窗口代码
        //标题
        GUIStyle titleStyle = new GUIStyle();
        titleStyle.fontSize = 25;
        titleStyle.alignment = TextAnchor.MiddleCenter;
        titleStyle.normal.textColor = Color.yellow;
        //titleStyle.
        GUILayout.Label("好好学习,天天向上!", titleStyle);

        //EditorGUILayout测试
        GUILayout.BeginArea(new Rect(position.width-300,0,300,500));
        scale = EditorGUILayout.Slider(scale, -5,5);
        scrollBarValue1 = GUILayout.HorizontalScrollbar(scrollBarValue1,10,0,100);
        GUILayout.Button("可爱的按钮");
        gridId = GUILayout.SelectionGrid(gridId,new string[] { docList[0].FileName,docList[1].FileName },2);
        GUILayout.TextField("TextField只能一行");
        GUILayout.TextArea("TextField可以多行\n 比如这样");
        isToggle=GUILayout.Toggle(isToggle,"Toggle");
        GUILayout.EndArea();

        if (GUILayout.Button("打开百度"))
        {
            System.Diagnostics.Process.Start("IExplore.exe", "https://www.baidu,com/");
        }
        if (GUILayout.Button("打开百度"))
        {
            System.Diagnostics.Process.Start("msedge.exe", "https://www.baidu,com/");
        }

        intSliderValue= EditorGUILayout.IntSlider(intSliderValue,0,10);

        //GUILayout.BeginHorizontal();//水平布局
        GUILayout.BeginVertical();
        //文档列表
        int index = 0;
        for (int i = 0; i < docList.Count/2; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                if (index<docList.Count)
                {
                    DocModel model = docList[index++];
                    if (GUILayout.Button(model.FileName,GUILayout.Width(194),GUILayout.Height(50)))
                    {
                        Application.OpenURL(model.FilePath);
                    }
                }
            }
        }
        GUILayout.EndVertical();
       // GUILayout.EndHorizontal();
    }
}

internal class DocModel
{
    public string FilePath { get; set;}
    public string FileName { get; set;}
}