Skip to content

Instantly share code, notes, and snippets.

@FlaShG

FlaShG/FloatRange.cs

Last active Jun 24, 2020
Embed
What would you like to do?
A small struct representing a range from min to max.
using UnityEngine;
[System.Serializable]
public struct FloatRange
{
public float min;
public float max;
public float range { get { return Mathf.Abs(max - min); } }
public FloatRange(float min, float max)
{
this.min = min;
this.max = max;
}
public static FloatRange FromTwoNumbers(float a, float b)
{
if (a <= b)
{
return new FloatRange(a, b);
}
else
{
return new FloatRange(b, a);
}
}
public float Lerp(float t)
{
return Mathf.Lerp(min, max, t);
}
public float Clamp(float t)
{
return Mathf.Clamp(min, max, t);
}
public float GetRandom()
{
return Random.Range(min, max);
}
}
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(FloatRange))]
public class FloatRangeEditor : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
var indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
var leftPosition = new Rect(position.x, position.y, position.width / 2, position.height);
var rightPosition = new Rect(position.x + position.width / 2, position.y, position.width / 2, position.height);
EditorGUI.PropertyField(leftPosition, property.FindPropertyRelative("min"), GUIContent.none);
EditorGUI.PropertyField(rightPosition, property.FindPropertyRelative("max"), GUIContent.none);
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment