Skip to content

Instantly share code, notes, and snippets.

@shanecelis
Last active April 25, 2018 23:34
Show Gist options
  • Save shanecelis/c12a8f0ef72ab1a05ca2c1b454103ed0 to your computer and use it in GitHub Desktop.
Save shanecelis/c12a8f0ef72ab1a05ca2c1b454103ed0 to your computer and use it in GitHub Desktop.
Adds a frequency or period custom drawer to Unity. Because I can never remember which one I've used.
/*
Adds a frequency or period custom drawer to Unity because I can never
remember which one I've used. See here for a quick demonstration:
https://twitter.com/shanecelis/status/989234269020180480
Its licensed under the MIT License.
* * *
Copyright 2018 Shane Celis
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
[System.Serializable]
public class FrequencyOrPeriod {
public enum Kind {
Frequency,
Period
}
public Kind kind = Kind.Frequency;
[SerializeField]
private float _frequency = 1f;
public float frequency {
get { return _frequency; }
set { _frequency = value; }
}
public float period {
get { return 1f / _frequency; }
set { _frequency = 1f / value; }
}
public float Value {
get { return kind == Kind.Frequency ? frequency : period; }
set {
if (kind == Kind.Frequency)
frequency = value;
else
period = value;
}
}
public static FrequencyOrPeriod Frequency(float f) {
return new FrequencyOrPeriod { _frequency = f, kind = Kind.Frequency };
}
public static FrequencyOrPeriod Period(float p) {
return new FrequencyOrPeriod { _frequency = 1f / p, kind = Kind.Period };
}
public bool isFrequency {
get { return kind == Kind.Frequency; }
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer (typeof (FrequencyOrPeriod))]
class FrequencyOrPeriodDrawer : PropertyDrawer {
// Draw the property inside the given rect
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
// Using BeginProperty / EndProperty on the parent property means that
// prefab override logic works on the entire property.
EditorGUI.BeginProperty (position, label, property);
float frequency = property.FindPropertyRelative("_frequency").floatValue;
var kind = (FrequencyOrPeriod.Kind) property.FindPropertyRelative("kind").intValue;
// label = new GUIContent(label);
var freq = new FrequencyOrPeriod { frequency = frequency, kind = kind };
float amount = freq.Value;
// Draw label
position = EditorGUI.PrefixLabel (position, GUIUtility.GetControlID (FocusType.Passive), label);
// Don't make child fields be indented
var indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
GUIContent suffixContent;
if (! Mathf.Approximately(amount, 1f))
suffixContent = new GUIContent(freq.isFrequency ? " cycles per second" : " seconds per cycle");
else
suffixContent = new GUIContent(freq.isFrequency ? " cycle per second" : " second per cycle");
Vector2 suffixSize = GUI.skin.label.CalcSize(suffixContent);
var buttonWidth = GUI.skin.button.CalcSize(new GUIContent("Frequency")).x;
// Calculate rects
var padding = 2f;
var width = (position.width - suffixSize.x - buttonWidth - 2 * padding);
Rect buttonRect = new Rect (position.x, position.y, buttonWidth, position.height);
Rect amountRect = new Rect (position.x + buttonWidth + padding, position.y, width, position.height);
Rect suffixRect = new Rect (position.x + buttonWidth + width + 2 * padding, position.y, suffixSize.x, position.height);
var floatStyle = new GUIStyle(GUI.skin.textField);
floatStyle.alignment = TextAnchor.MiddleRight;
float newAmount = EditorGUI.FloatField(amountRect, amount, floatStyle);
if (newAmount != amount) {
switch (kind) {
case FrequencyOrPeriod.Kind.Frequency:
property.FindPropertyRelative("_frequency").floatValue = newAmount;
break;
case FrequencyOrPeriod.Kind.Period:
property.FindPropertyRelative("_frequency").floatValue = 1f / newAmount;
break;
default:
throw new System.Exception("Unexpected value for kind " + kind);
}
}
EditorGUI.LabelField(suffixRect, suffixContent);
var newKind = (FrequencyOrPeriod.Kind) EditorGUI.EnumPopup(buttonRect, kind);
if (newKind != kind) {
property.FindPropertyRelative("kind").intValue = (int) newKind;
}
// Set indent back to what it was
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty ();
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment