Skip to content

Instantly share code, notes, and snippets.

@FlaShG FlaShG/MinMaxFloat.cs
Last active Jul 4, 2018

Embed
What would you like to do?
A small struct representing a range from min to max.
using UnityEngine;
[System.Serializable]
public struct MinMaxFloat
{
public float min;
public float max;
public float range { get { return Mathf.Abs(max - min); } }
public MinMaxFloat(float min, float max)
{
this.min = min;
this.max = max;
}
public static MinMaxFloat FromTwoNumbers(float a, float b)
{
if (a <= b)
{
return new MinMaxFloat(a, b);
}
else
{
return new MinMaxFloat(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(MinMaxFloat))]
public class MinMaxFloatEditor : 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
You can’t perform that action at this time.