A small struct representing a range from min to max.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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