Unity重写Inspector简化分组配置文件

2023-12-25 13:43:15

重写Inspector创建分组管理配置文件

创建

在这里插入图片描述
在这里插入图片描述

修改参数

在这里插入图片描述

参数对应类

using UnityEngine;

public class GameConfig : ScriptableObject {

    /// <summary>
    /// float参数
    /// </summary>
    public float test_float = 10f;

    /// <summary>
    /// vector3参数
    /// </summary>
    public Vector3 test_vector3 = Vector3.zero;

    /// <summary>
    /// bool参数
    /// </summary>
    public bool test_bool = false;

    /// <summary>
    /// int参数
    /// </summary>
    public int test_int = 1;

    /// <summary>
    /// slider参数
    /// </summary>
    public float test_slider = 0.1f;

    /// <summary>
    /// text参数
    /// </summary>
    public string test_text = "";

    /// <summary>
    /// toggle参数
    /// </summary>
    public bool test_toggle = false;
}

using System;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(GameConfig))]
public class GameConfigInspector : HKBaseEditor {

    /// <summary>
    /// 配置表
    /// </summary>
    private GameConfig config;

    private string[] MenuItems = new string[] { "Save" };

    /// <summary>
    /// 绘制
    /// </summary>
    public override void OnInspectorGUI()
    {
        //base.OnInspectorGUI(); //如果需要分组配置需将其注释

        config = (GameConfig)target;
        AddToolBar(MenuItems, OnEventSave);


        HKContentBlock.DrawContent("配置测试1", () =>
        {
            //配置1
            HKEditorHelper.DrawFloatFieldLayout("float参数", ref config.test_float);
            HKEditorHelper.DrawVector3Layout("float参数", ref config.test_vector3);
            HKEditorHelper.DrawSliderLayout("slider参数", ref config.test_slider);
            HKEditorHelper.DrawToggleLayout("toggle参数", ref config.test_toggle);
        });

        HKContentBlock.DrawContent("配置测试2", () =>
        {
            config.test_bool = HKEditorHelper.DrawBoolLayout("bool参数", config.test_bool);
            config.test_int = HKEditorHelper.DrawIntFieldLayout("int参数", config.test_int);
            config.test_text = HKEditorHelper.DrawTextFiledLayout("text参数", config.test_text);
        });

    }

    /// <summary>
    /// 保存
    /// </summary>
    private void OnEventSave()
    {
        EditorUtility.SetDirty(config);
        AssetDatabase.SaveAssets();
    }


    [MenuItem("ETools/CreateGameConfig")]
    public static void Create()
    {
        CreateAsset<GameConfig>("Assets/Resources/GameConfig.asset");
    }
}

工程在我的资源中名为CreateConfig,免费下载

文章来源:https://blog.csdn.net/qq_42194657/article/details/135186494
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。