Skip to content

Instantly share code, notes, and snippets.

@fuzzblob
Created October 20, 2017 05:21
Show Gist options
  • Save fuzzblob/35bd37a986b6677563be1b367c88ffa8 to your computer and use it in GitHub Desktop.
Save fuzzblob/35bd37a986b6677563be1b367c88ffa8 to your computer and use it in GitHub Desktop.
simple Unity inpector GUI element for a value and a random offset in a single range slider
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
public class EditorHelper
{
public static GUILayoutOption[] __buttonOptions = new GUILayoutOption[] { GUILayout.Width(120f), GUILayout.ExpandWidth(false) };
public static void RandomRangeSlider(string propertyName, ref float val, ref float rand, ref bool randomEnabled, float min, float max, float resetValue)
{
EditorGUILayout.BeginHorizontal();
#region VALUE
if (GUILayout.Button(propertyName + val.ToString("0.00"), __buttonOptions))
{
val = resetValue;
}
val = EditorGUILayout.Slider(val, min, max, GUILayout.ExpandWidth(false));
#endregion
GUILayout.Space(30f);
#region RANDOM OFFSET
if (randomEnabled == false)
GUI.enabled = false;
if (GUILayout.Button("Rand." + rand.ToString("0.00"), __buttonOptions))
{
rand = resetValue;
}
rand = EditorGUILayout.Slider(rand, -80f, 0f, GUILayout.ExpandWidth(false));
GUI.enabled = true;
#endregion
EditorGUILayout.EndHorizontal();
GUILayout.Space(5f);
EditorGUILayout.BeginHorizontal();
#region RANGE SLIDER
// Random Toggle
randomEnabled = EditorGUILayout.Toggle(randomEnabled, GUILayout.MaxWidth(50f));
// make the random setting relative to the main value
rand += val;
EditorGUILayout.MinMaxSlider(ref rand, ref val, min, max);
rand -= val;
#endregion
EditorGUILayout.EndHorizontal();
}
}
#endif
@fuzzblob
Copy link
Author

@mildmojo
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment