Unity Editor基础篇

记录一下常用的编辑器类型,然后把它们集中在一个窗口,以便使用。

预览图

Unity Editor基础篇_第1张图片
20180712144030.png

代码部分

一. 建一个类

Editor目录下新建类 MyLayoutWindow ,继承 EditorWindow
编写一个静态方法,用来打开窗口。

public class MyLayoutWindow : EditorWindow {
    [MenuItem("Tools/布局窗口")]
    static public void ShowWindow() {
        EditorWindow window = EditorWindow.GetWindow();
        window.minSize = new Vector2(400, 450);
        window.maxSize = new Vector2(400, 450);
        window.Show();
    }

    public void OnGUI() {

    }
}

这样就可以愉快得在“Tools/布局窗口”下打开窗口了。

二. 开始写OnGUI里面的布局
  1. 坐标
    为了美观,把二维三维放在同一水平上,所以在前后各加上EditorGUILayout.BeginHorizontal();
    EditorGUILayout.EndHorizontal();
    即可,后面会频繁用到。
    EditorGUILayout.Space();
    只是单纯为了中间留点间隔。
    private Vector2 mVec2;
    private Vector3 mVec3;
    private Vector4 mVec4;

    public void OnGUI() {
        EditorGUILayout.BeginHorizontal();
        this.mVec2 = EditorGUILayout.Vector2Field("二维坐标", this.mVec2, GUILayout.MaxWidth(150));
        this.mVec3 = EditorGUILayout.Vector3Field("三维坐标", this.mVec3, GUILayout.MaxWidth(250));
        EditorGUILayout.EndHorizontal();
        this.mVec4 = EditorGUILayout.Vector4Field("四维坐标", this.mVec4);
    }
  1. 颜色
    private Color mColor;

    public void OnGUI() {
        this.mColor = EditorGUILayout.ColorField(this.mColor);
    }
  1. 滑块
    private float mSlider;

    public void OnGUI() {
        this.mSlider = EditorGUILayout.Slider(this.mSlider, 0, 10);
    }
  1. 下拉框
    这里需要先定义一个枚举类
[Serializable]
public enum ShowType{
    None = 0,
    IntType = 1,
    FloatType = 2,
    StrType = 3
}
    private int mPopupIndex, mLayer;
    private string[] mPopupStrArr = new[]{
            "第一",
            "第二",
            "第三"
        };
    private int[] mPopupIntArr = new[]{
            0,1,2
        };
    private string mTag;

    public void OnGUI() {
        EditorGUILayout.BeginHorizontal();
        this.mPopupIndex = EditorGUILayout.Popup(this.mPopupIndex, mPopupStrArr, GUILayout.MaxWidth(70));
        EditorGUILayout.LabelField(this.mPopupIndex.ToString(), GUILayout.MaxWidth(30));
        this.mPopupIndex = EditorGUILayout.IntPopup(this.mPopupIndex, mPopupStrArr, mPopupIntArr, GUILayout.MaxWidth(70));
        EditorGUILayout.LabelField(this.mPopupIndex.ToString(), GUILayout.MaxWidth(30));
        //自定义枚举下拉框
        this.mType = (ShowType)EditorGUILayout.EnumPopup(this.mType, GUILayout.MaxWidth(70));
        EditorGUILayout.LabelField(this.mType.ToString(), GUILayout.MaxWidth(70));
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.Space();
        EditorGUILayout.BeginHorizontal();
        //Tag标签下拉框
        EditorGUILayout.LabelField("TagSelect:", GUILayout.MaxWidth(70));
        this.mTag = EditorGUILayout.TagField(this.mTag, GUILayout.MaxWidth(70));
        //Layer层下拉框
        EditorGUILayout.LabelField("LayerSelect:", GUILayout.MaxWidth(80));
        this.mLayer = EditorGUILayout.LayerField(this.mLayer, GUILayout.MaxWidth(70));
        EditorGUILayout.EndHorizontal();
    }
  1. 提示框
    这里不浪费空间,加了个小按钮
    public void OnGUI() {
        if (GUILayout.Button("点我")) {
            EditorUtility.DisplayDialog("标题", "提示内容", "确定", "取消");
        }
    }
  1. 勾选+折叠
    private bool mIsSelectTog, mIsFoldout, mIsTogGroup;
    private bool[] mIsGroupTogs = new bool[2] { false, false};

    public void OnGUI() {
        this.mIsSelectTog = EditorGUILayout.Toggle("显示折叠", this.mIsSelectTog);
        if (this.mIsSelectTog) {
            this.mIsFoldout = EditorGUILayout.Foldout(this.mIsFoldout, "折叠");
            if (this.mIsFoldout) {
                this.mIsTogGroup = EditorGUILayout.BeginToggleGroup("选择:", this.mIsTogGroup);
                mIsGroupTogs[0] = EditorGUILayout.ToggleLeft("111", mIsGroupTogs[0]);
                mIsGroupTogs[1] = EditorGUILayout.ToggleLeft("222", mIsGroupTogs[1]);
                EditorGUILayout.EndToggleGroup();
            }
        }
    }
  1. 动画片段
    private AnimationCurve mCurve = new AnimationCurve();

    public void OnGUI() {
        this.mCurve = EditorGUILayout.CurveField("动画片段", this.mCurve);
    }
  1. 对象选择框
    private Object mObj;
    private Texture mTexture;

    public void OnGUI() {
        //type-Object
        this.mObj = EditorGUILayout.ObjectField("选择object", this.mObj, typeof(Object));
        //type-Texture
        this.mTexture = (Texture)EditorGUILayout.ObjectField("选择图片", this.mTexture, typeof(Texture), true);
    }
  1. 路径选择
    private string mSelectPath;

    public void OnGUI() {
        if (GUILayout.Button("选择", GUILayout.MaxWidth(80))) {
            this.mSelectPath = EditorUtility.OpenFilePanel("选择文件", "", "");
        }
        this.mSelectPath = EditorGUILayout.TextField(this.mSelectPath, GUILayout.MaxWidth(320), GUILayout.MaxHeight(20));
    }
  1. 提示
    public void OnGUI() {
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.HelpBox("oh my god!!!!!", MessageType.Error);
        EditorGUILayout.HelpBox("oh my god!!!!!", MessageType.Warning);
        EditorGUILayout.HelpBox("oh my god!!!!!", MessageType.Info);
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.HelpBox("oh my god!!!!!", MessageType.None);
    }

这些只是最最基础的界面显示有关的内容,利用这些,再根据项目的需求,编写工具,才是最终的目的。

你可能感兴趣的:(Unity Editor基础篇)