게임엔진/Unity

[Unity] Range 어트리뷰트를 단위로 설정할 수 있는 방법

ShovelingLife 2023. 2. 17. 11:39

아래는 어트리뷰트를 그려줄 클래스

using UnityEngine;
using UnityEditor;
using System;

[CustomPropertyDrawer(typeof(RangeExAttribute))]
internal sealed class RangeExDrawer : PropertyDrawer
{
    private int value;

    /**
     * Return exact precision of reel decimal
     * ex :
     * 0.01     = 2 digits
     * 0.02001  = 5 digits
     * 0.02000  = 2 digits
     */
    private int Precision(float value)
    {
        int _precision;
        if (value == .0f) return 0;
        _precision = value.ToString().Length - (((int)value).ToString().Length + 1);
        // Math.Round function get only precision between 0 to 15
        return Mathf.Clamp(_precision, 0, 15);
    }

    /**
     * Return new float value with step calcul (and step decimal precision)
     */
    private float Step(float value, float min, float step)
    {
        if (step == 0) return value;
        float newValue = min + Mathf.Round((value - min) / step) * step;
        return (float)Math.Round(newValue, Precision(step));
    }

    /**
     * Return new integer value with step calcul
     * (It's more simple ^^)
     */
    private int Step(int value, float step)
    {
        if (step == 0) return value;
        value -= (value % (int)Math.Round(step));
        return value;
    }

    //
    // Methods
    //
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        var rangeAttribute = (RangeExAttribute)base.attribute;

        if (rangeAttribute.label != "")
            label.text = rangeAttribute.label;

        switch (property.propertyType)
        {
            case SerializedPropertyType.Float:
                float _floatValue = EditorGUI.Slider(position, label, property.floatValue, rangeAttribute.min, rangeAttribute.max);
                property.floatValue = Step(_floatValue, rangeAttribute.min, rangeAttribute.step);
                break;
            case SerializedPropertyType.Integer:
                int _intValue = EditorGUI.IntSlider(position, label, property.intValue, (int)rangeAttribute.min, (int)rangeAttribute.max);
                property.intValue = Step(_intValue, rangeAttribute.step);
                break;
            default:
                EditorGUI.LabelField(position, label.text, "Use Range with float or int.");
                break;
        }
    }
}

아래는 어트리뷰트에 설정될 값

using System;
using UnityEngine;

// 범위 기반 어트리뷰트 [Range(최저, 최대)] 에서 n값에 나눠서 가능하게끔 함 / 예) 1~10에서 2단위로 설정한다. 2,4,6,8,10
// 사용 예시 [RangeEx(-10f, 10f, 0.5f, "간격이 0.5인 float 어트리뷰트")]
[AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
public sealed class RangeExAttribute : PropertyAttribute
{
    public readonly float min;
    public readonly float max;
    public readonly float step;
    public readonly string label = "";

    public RangeExAttribute(float min, float max, float step, string label)
    {
        this.min = min;
        this.max = max;
        this.step = step;
        this.label = label;
    }
}