当前位置: 首页 > 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.dtcms.com/a/305081.html

相关文章:

  • Android User版本默认用test-keys,如何改用release-keys
  • IDDR原语基本使用
  • 【三桥君】AI技术发展下,单智能体局限性凸显,如何通过MCP和A2A协议实现智能体团队协作转变?
  • Day 25:异常处理
  • GitLab的安装及使用
  • 嵌入式第十四课!!!指针在字符数组的应用与数组指针
  • 【Lua】题目小练3
  • 13、select_points_object_model_3d解析
  • Excel制作滑珠图、哑铃图
  • HCIP--MGRE综合实验
  • 从0到1学PHP(五):PHP 数组:高效存储与处理数据
  • C#_ArrayList动态数组
  • 【C#获取高精度时间】
  • 同一个局域网段,如何实现所有用户都可以访问本地mysql数据库?
  • 理解Transformer解码器
  • 【三桥君】企业级AI应用需要系统工程支撑,如何通过MCP大模型架构实现全链路实战解构?
  • 1 RAG三问
  • Javaweb————HTTP请求头属性讲解
  • ​第七篇:Python数据库编程与ORM实践
  • Python的‌列表推导式‌
  • 065_线程创建方式(继承Thread / 实现Runnable Callable)
  • Hyperchain账本数据存储机制详解
  • SpringCloud之Gateway
  • ORACLE的用户维护与权限操作
  • 车载刷写架构 --- 整车刷写中为何增加了ECU 队列刷写策略?
  • 激光雷达/相机一体机 时间同步和空间标定(1)
  • [leetcode] 电话号码的排列组合
  • elememtor 添加分页功能
  • GaussDB 约束的语法
  • 互联网前沿新技术