当前位置: 首页 > news >正文

给你的Unity编辑器添加实现类似 Odin 的 条件显示字段 (ShowIf/HideIf) 功能

自己常用的一个预制功能模块,用到这个开关显示的功能,但又不想依赖Odin。
一个是自己穷,用不起Odin,二是如果我的包给别人用,依赖的太多而且还是收费的,会不会被人炸掉祖坟。

一、效果演示

请添加图片描述
在脚本的观察面板上,通过一些变量开关,显示和隐藏某些字段。

目前Unity没有这个Attribute,所以要么你用诸如Odin,要么自己用AI搓一个

二、需求来源

自己常用的一个预制功能模块,用到这个开关显示的功能,但又不想依赖Odin。
一个是自己穷,用不起Odin,二是如果我的包给别人用,依赖的太多而且还是收费的,会不会被人炸掉祖坟。
请添加图片描述

三、功能脚本组成

脚本1,Editor下:ConditionalFieldDrawer.cs,注意,必须放在Editor下
脚本2,Runtime下:ConditionalFieldAttribute.cs,不能放在Editor下
脚本3,测试脚本:Demo.cs
在这里插入图片描述
挂载后到物体并测试:
在这里插入图片描述

四、源码

1、ConditionalFieldDrawer.cs 清单

using UnityEditor;
using UnityEngine;[CustomPropertyDrawer(typeof(ConditionalFieldAttribute))]
public class ConditionalFieldDrawer : PropertyDrawer
{public override void OnGUI(Rect position, SerializedProperty property, GUIContent label){ConditionalFieldAttribute condHAtt = (ConditionalFieldAttribute)attribute;// 找到比较的字段SerializedProperty comparedField = property.serializedObject.FindProperty(condHAtt.ComparedPropertyName);if (comparedField != null && comparedField.propertyType == SerializedPropertyType.Boolean){bool enabled = comparedField.boolValue == condHAtt.ExpectedValue;if (enabled){EditorGUI.PropertyField(position, property, label, true);}}else{// 如果字段没找到,正常显示EditorGUI.PropertyField(position, property, label, true);}}public override float GetPropertyHeight(SerializedProperty property, GUIContent label){ConditionalFieldAttribute condHAtt = (ConditionalFieldAttribute)attribute;SerializedProperty comparedField = property.serializedObject.FindProperty(condHAtt.ComparedPropertyName);if (comparedField != null && comparedField.propertyType == SerializedPropertyType.Boolean){bool enabled = comparedField.boolValue == condHAtt.ExpectedValue;return enabled ? EditorGUI.GetPropertyHeight(property, label, true) : 0;}return EditorGUI.GetPropertyHeight(property, label, true);}
}

2、ConditionalFieldAttribute.cs 清单

using System;
using UnityEngine;[AttributeUsage(AttributeTargets.Field, AllowMultiple = true, Inherited = true)]
public class ConditionalFieldAttribute : PropertyAttribute
{public string ComparedPropertyName { get; private set; }public bool ExpectedValue { get; private set; }public ConditionalFieldAttribute(string comparedPropertyName, bool expectedValue = true){this.ComparedPropertyName = comparedPropertyName;this.ExpectedValue = expectedValue;}
}

3、Demo.cs 清单

using UnityEngine;public class ConditionalFieldTest : MonoBehaviour
{[Header("总开关")]public bool masterSwitch;[Header("子选项,只在 masterSwitch == true 时显示")][ConditionalField(nameof(masterSwitch), true)]public bool optionA;[ConditionalField(nameof(masterSwitch), true)]public bool optionB;[Header("反向测试:只有当 masterSwitch == false 时才显示")][ConditionalField(nameof(masterSwitch), false)]public string hiddenMessage = "当 masterSwitch == false 时才看得见";[Header("多层嵌套测试")]public bool subSwitch;[ConditionalField(nameof(subSwitch), true)]public int subValue;
}

五、致谢:

感谢GPT,从前的搜索时代是信息平权,现在的GPT时代是知识平权。
网友说:三天不学习,赶不上GPT。

http://www.lryc.cn/news/626571.html

相关文章:

  • 用好 Elasticsearch Ruby 传输层elastic-transport
  • 二维码跳转支付宝、微信小程序二码合一(直接关联、中间页识别跳转)方案
  • python-使用鼠标对图片进行涂抹自定义绘图
  • 自己微调的大模型如何用ollama运行
  • Android 开发问题:android:marginTop=“20px“ 属性不生效
  • 数字化图书管理系统设计实践(java)
  • Redis 复制功能是如何工作的
  • Linux I/O 多路复用实战:Select/Poll 编程指南
  • iOS 应用上架常见问题与解决方案,多工具组合的实战经验
  • Redis--day9--黑马点评--分布式锁(二)
  • C++ 数据结构 和 STL
  • 大数据毕业设计选题推荐-基于大数据的1688商品类目关系分析与可视化系统-Hadoop-Spark-数据可视化-BigData
  • wpf之ComboBox
  • 软件测试面试题真题分享
  • 数据挖掘笔记:点到线段的距离计算
  • 百度Q2财报:总营收327亿 AI新业务收入首次超100亿
  • PDF如何在Adobe Acrobat 中用OCR光学识别文档并保存可编辑文档
  • 【鸿蒙心迹】7×24小时极限求生:当Origin_null遇上鸿蒙,我如何用100杯咖啡换一条跨域活路?
  • 环境搭建:centos7+docker+Oracle
  • 【Day 30】Linux-SQL语句
  • 【爬虫实战-IP代理的重要性二】 以Selenium为例
  • 高通 XR 系列芯介绍
  • 发那科机器人程序调整功能
  • 搭建一个SpringBoot项目的流程
  • 05-网关服务开发指南
  • 使用Spring Retry组件优雅地实现重试
  • 【网络】使用 DNAT 进行负载均衡时,若未配置配套的 SNAT,回包失败
  • 数字IC后端设计实现常见后端问题解析
  • 面试官视角分析与提问点
  • SpringBoot3 Ruoyi芋道管理后台vben5.0