• 编写 Unity Editor 插件


    Editor Style Viewer

    在开发过程中,我喜欢编写一些辅助的Editor插件,方便在游戏开发过程进行调试。

    下面是摘自Asset Store的一个查看Unity 默认GUI样式的小工具

    插件链接:Editor Style Viewer https://www.assetstore.unity3d.com/en/#!/content/3282

    预览

    imageimage

    Editor Style Viewer源码

    原理:遍历所有的GUI.skin,并显示其样式

    using UnityEngine;
    using UnityEditor;
    
    /// <summary>
    /// 查看默认的gui skin样式
    /// </summary>
    public class EditorStyleView : EditorWindow
    {
        private Vector2 scrollPosition = Vector2.zero;
        private string search = string.Empty;
    
        [MenuItem("Tools/默认GUI样式查看器")]
        static void Init()
        {
            var window= EditorWindow.GetWindow<EditorStyleView>();
            window.title = "GUI样式查看器";
            window.Show();
            
        }
    
        void OnGUI()
        {
            GUILayout.BeginHorizontal("HelpBox");
            GUILayout.Label("单击左侧样式将复制其名到剪贴板", "label");
            GUILayout.FlexibleSpace();
            GUILayout.Label("查找:");
            search = EditorGUILayout.TextField(search);
            GUILayout.EndHorizontal();
    
            scrollPosition = GUILayout.BeginScrollView(scrollPosition);
    
            //foreach (GUIStyle style in GUI.skin.customStyles)
            foreach (GUIStyle style in GUI.skin)
            {
                //过滤
                if (style.name.ToLower().Contains(search.ToLower()))
                {
                    //设置奇偶行不同背景
                    GUILayout.BeginHorizontal("PopupCurveSwatchBackground");
                    GUILayout.Space(20);//左边留白20
                    if (GUILayout.Button(style.name, style))
                    {
                        //把名字存储在剪粘板 
                        EditorGUIUtility.systemCopyBuffer = style.name; // """ + style.name + """;
                    }
                    GUILayout.FlexibleSpace();
                    EditorGUILayout.SelectableLabel(""" + style.name + """);
                    GUILayout.EndHorizontal();
                    GUILayout.Space(20);//右边留白20
                }
            }
    
            GUILayout.EndScrollView();
        }
    }

    GM编辑器插件

    比如这样的GM小工具,辅助开发团队。

    image

    1、创建GMEditorWindow.cs,放在Editor目录下

    2、编写与游戏相关的逻辑功能

    GM编辑器插件源码

    [MenuItem("Game/GM指令")]
    static void Init()
    {
        var window = EditorWindow.GetWindow<GMEditorWindow>();
        window.title = "XX GM指令";
        window.Show();
    }
    
    private int newExp = 0, newMoney = 0, newVip = 0, newVp = 0, newCoin = 0, newSpirts = 0;
    private int maxHp = 0, maxVp = 0,maxHurt=0;
    
    private int nMapId = 0;
    
    public void OnGUI()
    {
        EditorGUILayout.LabelField("== 加数值 指令 ==");
    
        GUILayout.BeginHorizontal();
        GUILayout.Label("经验:");
        newExp = EditorGUILayout.IntField(newExp, GUILayout.ExpandWidth(true), GUILayout.MinHeight(20));
        if (GUILayout.Button("加经验", GUILayout.MinWidth(100), GUILayout.MaxHeight(20)))
        {
                AddExp(newExp);
        }
        //-------
        GUILayout.Label("VIP钱:");
        newVip = EditorGUILayout.IntField(newVip, GUILayout.ExpandWidth(true), GUILayout.MinHeight(20));
        if (GUILayout.Button("加VIP", GUILayout.MinWidth(100), GUILayout.MaxHeight(20)))
        {
                AddVip(newVip);
        }
        GUILayout.EndHorizontal();
    
        GUILayout.BeginHorizontal();
        GUILayout.Label("金币:");
        newCoin = EditorGUILayout.IntField(newCoin);
        if (GUILayout.Button("加金币", GUILayout.MinWidth(100), GUILayout.MaxHeight(20)))
        {
            AddCoin(newCoin);
        }
        //-------
        GUILayout.Label("元宝");
        newMoney = EditorGUILayout.IntField(newMoney);
        if (GUILayout.Button("加元宝", GUILayout.MinWidth(100), GUILayout.MaxHeight(20)))
        {
            AddMoney(newMoney);
        }
        GUILayout.EndHorizontal();
        //后面继续....
    }
  • 相关阅读:
    【ybtoj】【Hash】回文子串
    Design Pattern:状态模式
    Design Pattern:装饰者模式
    Design Pattern:外观模式
    Design Pattern:适配器模式
    Design Pattern:模板方法模式
    Design Pattern:命令模式
    Design Pattern:观察者模式
    Design Pattern:复合模式
    Design Pattern:迭代器模式
  • 原文地址:https://www.cnblogs.com/zhaoqingqing/p/5147872.html
Copyright © 2020-2023  润新知