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

unity 使用PropertyDrawer 在Inspector 面板上自定义字段的显示方式

使用PropertyAttribute自定义特性,然后使用PropertyDrawer特性为其创造在Inspector 面板上的GUI,从而实现自定义Inspector 面板显示的效果

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;// 1. 创建自定义属性
public class MyRangeAttribute : PropertyAttribute
{public float min, max;public MyRangeAttribute(float min, float max) { this.min = min; this.max = max; }
}// 2. 创建对应的 PropertyDrawer
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(MyRangeAttribute))]
public class MyRangeDrawer : UnityEditor.PropertyDrawer
{public override void OnGUI(Rect position, SerializedProperty property, GUIContent label){// 绘制背景EditorGUI.DrawRect(position, new Color(0.8f, 0.8f, 1f, 0.2f)); // 淡蓝色背景//控制颜色GUI.color = Color.red;MyRangeAttribute range = (MyRangeAttribute)attribute;property.floatValue = EditorGUI.Slider(position, label, property.floatValue, range.min, range.max);EditorGUI.LinkButton(position, "测试的按钮Button");}
}
#endif// 3. 使用方式
public class Demo : MonoBehaviour
{[MyRange(0, 100)]public float value;
}

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

相关文章:

  • 天铭科技×蓝卓 | “1+2+N”打造AI驱动的汽车零部件行业智能工厂
  • RPG增容2.尝试使用MMC根据游戏难度自定义更改怪物的属性(二)
  • 本土化DevOps实践:Gitee为核心的协作工具链与高效落地指南
  • git中多仓库工作的常用命令
  • Mac安装Navicat步骤Navicat Premium for Mac v17.1.9【亲测】
  • 【腾讯云】EdgeOne网站安全防护的配置方法 防范盗刷流量 附恶意IP和UA黑名单
  • YOLOv11.pt 模型转换为 TFLite 和 NCNN 模型
  • npm : 无法加载文件 D:\Nodejs\node_global\npm.ps1,因为在此系统上禁止运行脚本
  • Kafka运维实战 17 - kafka 分区副本从 1 增加到 3【实战】
  • 图形界面应用程序技术栈大全
  • Java把word转HTML格式
  • python中的 @dataclass
  • It学习资源下载
  • LeetCode热题100——155. 最小栈
  • 【JVM】常见的 Java 垃圾回收算法以及常见的垃圾回收器介绍及选型
  • Docker网络技术深度研究与实战手册
  • DisplayPort 与 Display Port Alt模式两者区别解析
  • java导出pdf(使用html)
  • 【LeetCode 热题 100】(二)双指针
  • 【初识数据结构】CS61B中的基数排序
  • 纯血鸿蒙 AudioRenderer+AudioCapturer+RingBuffer 实现麦克风采集+发声
  • Leetcode-3152 特殊数组 II
  • 从字符串中“薅出”最长子串:LeetCode 340 Swift 解法全解析
  • B+树高效实现与优化技巧
  • 如何选择AI IDE?对比Cursor分析功能差异
  • echarts图表点击legend报错问题(折线图)
  • 8.项目起步(2)
  • 数据库02 网页html01 day44
  • 图像增强11种几何变换方法示例
  • 从单机架构到分布式:Redis为何成为架构升级的关键一环?