首页 > 其他分享 >编辑器二次开发

编辑器二次开发

时间:2022-11-19 12:00:58浏览次数:50  
标签:UnityEngine void GUILayout 编辑器 二次开发 data public gos

快速添加盒型碰撞器
批量设置字体
查询信息
记录历史记录,清除记录

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

public class UnityTools : EditorWindow {
    [MenuItem("Tools/快速添加组件小工具")]
    static void Init() {
        UnityTools abt = EditorWindow.GetWindow<UnityTools>();
        abt.Show();
    }

    /// <summary>
    /// 当前选择的字体
    /// </summary>
    public UnityEngine.Object font;
    /// <summary>
    /// 要查询的名字
    /// </summary>
    public string mytext;
    public bool basicWindow = false;
    /// <summary>
    /// 历史窗口缩进
    /// </summary>
    public bool historiesWindow = false;

    private void OnGUI() {
        //GUILayout.BeginArea(new Rect(Vector2.zero,new Vector2(300,300)));

        // basicWindow = EditorGUILayout.BeginToggleGroup("基本工具区间", basicWindow);
        basicWindow = EditorGUILayout.Foldout(basicWindow, "基本工具区间");
        if (basicWindow) {
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("添加Box Collider"))//按钮
            {
                AddBoxCollider();
            }
            if (GUILayout.Button("删除Box Collider"))//按钮
            {
                RemoveBoxColliders();
            }

            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();

            font = EditorGUILayout.ObjectField("要设置的字体", font, typeof(Font));
            if (GUILayout.Button("批量设置字体"))//按钮
            {
                SetLabelFont();
            }

            GUILayout.EndHorizontal();
            GUILayout.Label("要输入的查询信息");

            GUILayout.BeginHorizontal();

            mytext = GUILayout.TextField(mytext);///输入文本
            if (GUILayout.Button("查询"))//按钮
            {
                QueryByName();
            }
            GUILayout.EndHorizontal();

            foreach (var obj in checkObjects) {
                EditorGUILayout.ObjectField(obj.name, obj, typeof(UnityEngine.Object));
            }
        }

        historiesWindow = EditorGUILayout.Foldout(historiesWindow, "历史窗口");
        if (historiesWindow) {

            HorizontalArea(() => {
                if (GUILayout.Button("记录")) {
                    ///执行记录历史操作
                    RememberCommands();
                }
                if (GUILayout.Button("清除历史")) {
                    histories.Clear();
                }
            });


            foreach (var data in histories) {
                EditorGUILayout.ObjectField(data.Value.saveTime, data.Key, typeof(UnityEngine.Object));
            }
        }

        // EditorGUILayout.EndToggleGroup();

        //GUILayout.EndArea();
    }

    void HorizontalArea(Action callback = null) {
        GUILayout.BeginHorizontal();
        if (callback != null) {
            callback();
        }
        GUILayout.EndHorizontal();
    }

    /// <summary>
    /// 历史信息
    /// </summary>
    struct HistoryInfo {
        public UnityEngine.Object history;
        public string saveTime;

        public HistoryInfo(UnityEngine.Object history, string saveTime) {
            this.history = history;
            this.saveTime = saveTime;
        }
    }

    /// <summary>
    /// 操作历史
    /// </summary>
    Dictionary<UnityEngine.Object, HistoryInfo> histories = new Dictionary<UnityEngine.Object, HistoryInfo>();

    private void RememberCommands() {

        UnityEngine.Object[] gos = Selection.GetFiltered<UnityEngine.Object>(SelectionMode.Unfiltered);
        foreach (var data in gos) {
            if (!histories.ContainsKey(data)) {
                histories.Add(data, new HistoryInfo(data, DateTime.Now.ToString("yy-MM-dd hh-mm-ss")));
            }
        }

    }


    List<UnityEngine.Object> checkObjects = new List<UnityEngine.Object>();

    private void QueryByName() {
        checkObjects.Clear();
        GameObject[] gos = Selection.GetFiltered<GameObject>(SelectionMode.Deep);
        foreach (var data in gos) {
            if (data.name.Contains(mytext)) {
                checkObjects.Add(data);
            }
        }
    }

    private void SetLabelFont() {
        GameObject[] gos = Selection.GetFiltered<GameObject>(SelectionMode.Deep);
        foreach (var data in gos) {
            if (data.GetComponent<Text>() != null) {
                Debug.Log(data);
                data.GetComponent<Text>().font = (Font)font;
            }
        }
    }

    private void RemoveBoxColliders() {
        GameObject[] gos = Selection.GetFiltered<GameObject>(SelectionMode.Deep);
        foreach (var data in gos) {
            if (data.GetComponent<BoxCollider>() != null) {
                DestroyImmediate(data.GetComponent<BoxCollider>());
            }
        }
    }

    private void AddBoxCollider() {
        GameObject[] gos = Selection.GetFiltered<GameObject>(SelectionMode.Deep);
        foreach (var data in gos) {
            if (data.GetComponent<Renderer>() != null) {
                data.AddComponent<BoxCollider>();
            }
        }
    }
}

标签:UnityEngine,void,GUILayout,编辑器,二次开发,data,public,gos
From: https://www.cnblogs.com/wuzhongke/p/16905802.html

相关文章