編寫 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();
    //后面繼續....
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM