Odin Attributes 大全之 Collections 系列 | (74)List Drawer Settings 特性

在这里插入图片描述

📂 Unity 开发资源汇总 | 插件 | 模型 | 源码

💓 欢迎访问 Unity 打怪升级大本营

Odin Attributes 为Unity开发者提供了更多的自定义选项,使得开发过程更加高效和愉悦。通过使用这些特性,开发者可以创建更加专业和用户友好的编辑器界面,从而提升整个开发团队的工作效率。

在这里插入图片描述

Odin Inspector and Serializer 最新版 (0积分)免费下载

华丽的分割线

📂 Odin Attributes 大全 | 目录索引


标题1

💯 概述

List Drawer Settings 特性:在 Inspector 中自定义列表和数组的绘制方式。


标题2

💯 使用方法

  • Odin重写对应的数组和列表的绘制
    在这里插入图片描述

        [Title("List Basics")]
        [InfoBox("现在可以拖动列表元素来重新排序并逐个删除它们,并且列表具有分页功能(尝试添加大量元素!)您仍然可以从项目视图一次将许多资产拖到列表中—只需将它们拖到列表本身,并将它们插入到您想要添加它们的地方.")]
        public List<float> FloatList;
    
  • 将[Range]属性应用于此列表,代替传统的float形式
    在这里插入图片描述

        [InfoBox("将[Range]属性应用于此列表,代替传统的float形式")]
        [Range(0, 1)]
        public float[] FloatRangeArray;
    
  • 不同方式的只读方式
    在这里插入图片描述

        [ListDrawerSettings(IsReadOnly = true)]
        public int[] ReadOnlyArray1 = new int[] { 1, 2, 3 };
        [ReadOnly]
        public int[] ReadOnlyArray2 = new int[] { 1, 2, 3 };
    
  • 负责数据结构的数组或列表
    在这里插入图片描述

        public SomeOtherStruct[] SomeStructList;
    
  • 自定义page每页的个数
    在这里插入图片描述

        [Title("Advanced List Customization")]
        [InfoBox("Using [ListDrawerSettings], lists can be customized in a wide variety of ways.")]
        [ListDrawerSettings(NumberOfItemsPerPage = 5)]
        public int[] FiveItemsPerPage;
    
  • 显示对应元素的索引和指定其元素的标签
    在这里插入图片描述

        [ListDrawerSettings(ShowIndexLabels = true, ListElementLabelName = "SomeString")]
        public SomeStruct[] IndexLabels;
    
  • 禁止拖拽item,禁止翻页,禁止显示item个数
    在这里插入图片描述

        [ListDrawerSettings(DraggableItems = false, Expanded = false, ShowIndexLabels = true, ShowPaging = false, ShowItemCount = false, HideRemoveButton = true)]
        public int[] MoreListSettings = new int[] { 1, 2, 3 };
    
  • 【OnBeginListElementGUI】【OnEndListElementGUI】在每个列表元素的前后调用一个函数,并传入对应元素的索引
    在这里插入图片描述

        [ListDrawerSettings(OnBeginListElementGUI = "BeginDrawListElement", OnEndListElementGUI = "EndDrawListElement")]
        public SomeStruct[] InjectListElementGUI;
        private void BeginDrawListElement(int index)
        {
            SirenixEditorGUI.BeginBox(this.InjectListElementGUI[index].SomeString);
        }
        private void EndDrawListElement(int index)
        {
            SirenixEditorGUI.EndBox();
        }
    
  • 使用它将自定义GUI注入到列表的标题栏中。
    在这里插入图片描述

        [ListDrawerSettings(OnTitleBarGUI = "DrawRefreshButton")]
        public List<int> CustomButtons;
        private void DrawRefreshButton()
        {
            if (SirenixEditorGUI.ToolbarButton(EditorIcons.Refresh))
            {
                Debug.Log(this.CustomButtons.Count.ToString());
            }
        }
    
  • 【CustomAddFunction 】覆盖将对象添加到列表的默认行为。如果引用的成员返回列表类型元素,则将对每个选定对象调用该元素一次。如果引用的方法返回void,那么不管选择了多少对象,它都只会被调用一次。
    在这里插入图片描述

        [ListDrawerSettings(CustomAddFunction = "CustomAddFunction")]
        public List<int> CustomAddBehaviour;
        private int CustomAddFunction()
        {
            return this.CustomAddBehaviour.Count+100;
        }
    

标题3

💯 完整示例代码

using Sirenix.OdinInspector;
using Sirenix.Utilities.Editor;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ListDrawerSettingsAttributeExample : MonoBehaviour
{
    [PropertyOrder(int.MinValue), OnInspectorGUI]
    private void DrawIntroInfoBox()
    {
        SirenixEditorGUI.InfoMessageBox("Odin开箱即用,无需检查,即可全面升级检查器中列表和数组的图形.");
    }

    [Title("List Basics")]
    [InfoBox("现在可以拖动列表元素来重新排序并逐个删除它们,并且列表具有分页功能(尝试添加大量元素!)您仍然可以从项目视图一次将许多资产拖到列表中—只需将它们拖到列表本身,并将它们插入到您想要添加它们的地方.")]
    public List<float> FloatList;

    [InfoBox("将[Range]属性应用于此列表,代替传统的float形式")]
    [Range(0, 1)]
    public float[] FloatRangeArray;

    [ListDrawerSettings(IsReadOnly = true)]
    public int[] ReadOnlyArray1 = new int[] { 1, 2, 3 };
    [ReadOnly]
    public int[] ReadOnlyArray2 = new int[] { 1, 2, 3 };

    public SomeOtherStruct[] SomeStructList;

    [Title("Advanced List Customization")]
    [InfoBox("Using [ListDrawerSettings], lists can be customized in a wide variety of ways.")]
    [ListDrawerSettings(NumberOfItemsPerPage = 5)]
    public int[] FiveItemsPerPage;

    [ListDrawerSettings(ShowIndexLabels = true, ListElementLabelName = "SomeString")]
    public SomeStruct[] IndexLabels;

    [ListDrawerSettings(DraggableItems = false, Expanded = false, ShowIndexLabels = true, ShowPaging = false, ShowItemCount = false, HideRemoveButton = true)]
    public int[] MoreListSettings = new int[] { 1, 2, 3 };

    [ListDrawerSettings(OnBeginListElementGUI = "BeginDrawListElement", OnEndListElementGUI = "EndDrawListElement")]
    public SomeStruct[] InjectListElementGUI;
    private void BeginDrawListElement(int index)
    {
        SirenixEditorGUI.BeginBox(this.InjectListElementGUI[index].SomeString);
    }
    private void EndDrawListElement(int index)
    {
        SirenixEditorGUI.EndBox();
    }

    [ListDrawerSettings(OnTitleBarGUI = "DrawRefreshButton")]
    public List<int> CustomButtons;
    private void DrawRefreshButton()
    {
        if (SirenixEditorGUI.ToolbarButton(EditorIcons.Refresh))
        {
            Debug.Log(this.CustomButtons.Count.ToString());
        }
    }

    [ListDrawerSettings(CustomAddFunction = "CustomAddFunction")]
    public List<int> CustomAddBehaviour;
    private int CustomAddFunction()
    {
        return this.CustomAddBehaviour.Count+100;
    }

    [Serializable]
    public struct SomeStruct
    {
        public string SomeString;
        public int One;
        public int Two;
        public int Three;
    }

    [Serializable]
    public struct SomeOtherStruct
    {
        [HorizontalGroup("Split", 55), PropertyOrder(-1)]
        [PreviewField(50, Sirenix.OdinInspector.ObjectFieldAlignment.Left), HideLabel]
        public UnityEngine.MonoBehaviour SomeObject;

        [FoldoutGroup("Split/$Name", false)]
        public int A, B, C;

        [FoldoutGroup("Split/$Name", false)]
        public int Two;

        [FoldoutGroup("Split/$Name", false)]
        public int Three;

        private string Name { get { return this.SomeObject ? this.SomeObject.name : "Null"; } }
    }
}

标题4

💯 Odin 下载地址


Odin Inspector and Serializer 最新版 (0积分)免费下载


TheEnd


跳跃

📂 Unity 开发资源汇总 | 插件 | 模型 | 源码

💓 欢迎访问 Unity 打怪升级大本营

🍉🍉🍉 如果觉得这篇文对你有帮助的话,请点个赞👍、收藏⭐️下吧,非常感谢! 💕💕💕
关注我

博主头像
【博主简介】:10年以上软件开发经验,精通 C语言C++C#Java 等开发语言,开发过大型 Android 项目,现主要自主开发经营 休闲益智类小游戏

【粉丝福利】:博主收藏了大量游戏开发资源和素材。这些资源经过博主多年整理沉淀,现筛选一批精品资源,分享给大家学习研究。

Unity打怪军团 诚邀天下勇士加入 Unity学习互助小组 有意进群的同学联系我,互3互推也请联系我…
联系我

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unity打怪升级

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值