zoukankan      html  css  js  c++  java
  • PropertyDrawer 自定义属性绘图

    public class PlayerAttributeExample : MonoBehaviour
    {
        //无滑块的属性
        public int VIPLevel = 0;
    
        //特性限定,有滑块
        [Range(0, 10)]
        public int SliderVIPLevel = 0;
    }

    Range特性的方法实现:

    using UnityEngine;
    using System.Collections;
    
    //特性的定义要继承自PropertyAttribute
    public class MyRangeAttribute : PropertyAttribute
    {
        public float Min;//最小值
        public float Max;//最大值
    
        public MyRangeAttribute(float min, float max)
        {
            this.Min = min;
            this.Max = max;
        }
    }
    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    
    //继承PropertyDrawer, 必须放入Editor文件夹下
    [CustomPropertyDrawer(typeof(MyRangeAttribute))]
    public class MyRangeAttributeDrawer : PropertyDrawer
    {
        //重载OnGUI方法
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            MyRangeAttribute myRange = attribute as MyRangeAttribute;
    
            if (property.propertyType == SerializedPropertyType.Integer)
            {
                EditorGUI.IntSlider(position, property, (int)myRange.Min, (int)myRange.Max, label);
            }
            else if (property.propertyType == SerializedPropertyType.Float)
            {
                EditorGUI.Slider(position, property, myRange.Min, myRange.Max, label);
            }
            else
            {
    
            }
        }
    
    }

    2.绘制多选

        public enum SomeFood
        {
            汉堡 = 0,
            鸡肉卷 = 1,
            薯条 = 3,
        }
        //只能单选
        public SomeFood MyLoveFood;
    
        //多选特性
        [EnumListAttribute]
        public SomeFood MyLoveFoodList;
    using UnityEngine;
    using System.Collections;
    
    public class EnumListAttribute : PropertyAttribute
    {
    
    }
    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    
    [CustomPropertyDrawer(typeof(EnumListAttribute))]
    public class EnumListAttributeDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            property.intValue = EditorGUI.MaskField(position, label, property.intValue, property.enumNames);
        }
    }
  • 相关阅读:
    浅谈树状数组与线段树
    BZOJ1367:[Baltic2004]sequence
    浅谈左偏树
    BZOJ4003:[JLOI2015]城池攻占
    BZOJ2809:[APIO2012]dispatching
    BZOJ1455:罗马游戏
    模拟ssh远程执行命令
    基于TCP协议的socket套接字编程
    计算机网络基础知识
    元类( 控制对象产生和控制类产生)模板
  • 原文地址:https://www.cnblogs.com/martianzone/p/4867982.html
Copyright © 2011-2022 走看看