Skip to content

Instantly share code, notes, and snippets.

@FlaShG
Last active May 30, 2023 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlaShG/6f5fb2d9aec3a0332c0791fb0947a77c to your computer and use it in GitHub Desktop.
Save FlaShG/6f5fb2d9aec3a0332c0791fb0947a77c to your computer and use it in GitHub Desktop.
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 InverseLerp(float t)
{
return Mathf.InverseLerp(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