Odin Attributes 大全之 Groups 系列 | (57)Toggle Group 特性

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

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

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

在这里插入图片描述

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

华丽的分割线

📂 Odin Attributes 大全 | 目录索引


标题1

💯 概述

Toggle Group 特性:用于任何字段,并创建一个可切换的选项组。 使用此特性可创建可启用或禁用的选项。


标题2

💯 使用方法

  1. 通过制定一个返回bool值得成员名来控制toggle的开关 ,默认以成员的名称作为标题显示

注意:此 bool 成员不能为 static

在这里插入图片描述

    [ToggleGroup("MyToggle")]
    public bool MyToggle;

    [ToggleGroup("MyToggle")]
    public float A;

    [ToggleGroup("MyToggle")]
    [HideLabel, Multiline]
    public string B;
  1. 可以以制定toggle group的标题,或者通过$特殊标识符引用一个成员的值作为标题
    在这里插入图片描述
    [ToggleGroup("EnableGroupOne", "$GroupOneTitle")]
    public bool EnableGroupOne = true;

    [ToggleGroup("EnableGroupOne")]
    public string GroupOneTitle = "One";

    [ToggleGroup("EnableGroupOne")]
    public float GroupOneA;

    [ToggleGroup("EnableGroupOne")]
    public float GroupOneB;
  1. 如果制定的toggle为class结构,需要添加Serializable特性,toggle标题默认为此类的名称,且继承关系的父类结构同样可以绘制在 Inspector 面板上
    在这里插入图片描述
    [Toggle("Enabled")]
    public MyToggleObject Three = new MyToggleObject();

    [Toggle("Enabled")]
    public MyToggleA Four = new MyToggleA();

    [Toggle("Enabled")]
    public MyToggleB Five = new MyToggleB();

    [Serializable]
    public class MyToggleObject
    {
        public  bool Enabled;

        [HideInInspector]
        public string Title;

        public int A;
        public int B;
    }

    [Serializable]
    public class MyToggleA : MyToggleObject
    {
        public float C;
        public float D;
        public float F;
    }

    [Serializable]
    public class MyToggleB : MyToggleObject
    {
        public string Text;
    }

标题3

💯 完整示例代码

using Sirenix.OdinInspector;
using System;
using UnityEngine;

public class ToggleGroupAttributeExample : MonoBehaviour
{
    [ToggleGroup("MyToggle")]
    public bool MyToggle;

    [ToggleGroup("MyToggle")]
    public float A;

    [ToggleGroup("MyToggle")]
    [HideLabel, Multiline]
    public string B;

    // Toggle for custom data.
    [ToggleGroup("EnableGroupOne", "$GroupOneTitle")]
    public bool EnableGroupOne = true;

    [ToggleGroup("EnableGroupOne")]
    public string GroupOneTitle = "One";

    [ToggleGroup("EnableGroupOne")]
    public float GroupOneA;

    [ToggleGroup("EnableGroupOne")]
    public float GroupOneB;

    // Toggle for individual objects.
    [Toggle("Enabled")]
    public MyToggleObject Three = new MyToggleObject();

    [Toggle("Enabled")]
    public MyToggleA Four = new MyToggleA();

    [Toggle("Enabled")]
    public MyToggleB Five = new MyToggleB();

    public MyToggleC[] ToggleList = new MyToggleC[]
    {
    new MyToggleC(){ Test = 2f, Enabled = true, },
    new MyToggleC(){ Test = 5f, },
    new MyToggleC(){ Test = 7f, },
    };

    [Serializable]
    public class MyToggleObject
    {
        public  bool Enabled;

        [HideInInspector]
        public string Title;

        public int A;
        public int B;
    }
    [Serializable]
    public class MyToggleA : MyToggleObject
    {
        public float C;
        public float D;
        public float F;
    }
    [Serializable]
    public class MyToggleB : MyToggleObject
    {
        public string Text;
    }

    [Serializable]
    public class MyToggleC
    {
        [ToggleGroup("Enabled", "$Label")]
        public bool Enabled;

        public string Label { get { return this.Test.ToString(); } }

        [ToggleGroup("Enabled")]
        public float Test;
    }
}


标题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、付费专栏及课程。

余额充值