unity 编辑器扩展 PropertyDrawer

本文介绍如何在Unity中创建自定义属性绘制器以增强编辑器体验。通过使用自定义属性绘制器,开发者可以在编辑器中更直观地调整变量范围,并提供了一种简单的方法来限制属性的值。此技术对于游戏开发中的调试和优化非常有用。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyTestAttribute : PropertyAttribute {

    public int max;
    public int min;
        
    public MyTestAttribute(int a, int b)
    {
        min = a;
        max = b;
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;


/// <summary>
/// 扩展属性绘制器,用来扩展属性的查看内容和方式,用于debug非常方便
/// 官方编程手册的内容很不错,可以用来参考
/// </summary>
[CustomPropertyDrawer(typeof(MyTestAttribute))]
public class MyTestDrawer : PropertyDrawer {

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        //base.OnGUI(position, property, label);

        MyTestAttribute attribute = (MyTestAttribute)base.attribute;
        property.intValue = Mathf.Min(Mathf.Max(EditorGUI.IntField(position, label.text, property.intValue), attribute.min), attribute.max);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Game : MonoBehaviour {

    [MyTestAttribute(0, 100)]
    public int intValue = 0;
}

转载于:https://www.cnblogs.com/yufenghou/p/7344284.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值