Custom attributes: how does the attribute class relate to the custom type?
My goal is to create a custom attribute called MinMaxRange where I can specify the default minimum and default maximum.
[MinMaxRange(50.0f, 500.0f)]
public MinMaxRange radius;
In the Unity inspector, these should show as input boxes that can be changed to any other values.
So far, I have created this PropertyAttribute class:
using UnityEngine;
public class MinMaxRangeAttribute : PropertyAttribute
{
float defaultStart;
float defaultEnd;
public MinMaxRangeAttribute(float defaultStart, float defaultEnd)
{
this.defaultStart = defaultStart;
this.defaultEnd = defaultEnd;
}
}
And this custom property type:
using UnityEngine;
[System.Serializable]
public class MinMaxRange
{
public float rangeStart, rangeEnd;
public float GetRandomValue()
{
return Random.Range(rangeStart, rangeEnd);
}
public float Interpolate(float i)
{
float value = rangeStart + (rangeEnd - rangeStart) * i;
if (value < rangeStart || value > rangeEnd)
{
Debug.LogWarning("Interpolate with [0, 1] only.");
}
return Mathf.Clamp(value, rangeStart, rangeEnd);
}
}
I am not sure how to access defaultStart and defaultEnd from MinMaxRange. Any tips?
Comment
Answer by HenryStrattonFW · Feb 04, 2017 at 11:13 PM
to have your attribute effect the inspector for the custom type you need to define a PropertyDrawer for the attribute. https://docs.unity3d.com/ScriptReference/CustomPropertyDrawer.html