Last active
August 29, 2015 14:13
-
-
Save HilariousCow/1d056da2e3324670a087 to your computer and use it in GitHub Desktop.
A Random float Range type: Adapted a bunch from http://www.grapefruitgames.com/blog/2013/11/a-min-max-range-for-unity/ ,When used, returns a random value between two ranges. IntRange coming soon...
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
//... | |
public class MyScript : MonoBehaviour | |
{ | |
[FloatRange(-1f,1f)] //using this will make the randomModulator appear with double handles. | |
public FloatRange randomModulator; | |
void Jitter() | |
{ | |
transform.position = transform.position + Vector3.right * randomModulator; | |
} | |
} | |
// Works anywhere you can allow the inspector to see variables, i.e. ScriptableObject | |
public class MyDataBlob : ScriptableObject | |
{ | |
[FloatRange(-0f,100f)] //using this will make the randomModulator appear with double handles. | |
public FloatRange chanceOfSuccess; | |
} |
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 System.Collections; | |
using System.Collections.Generic; | |
public class FloatRangeAttribute : PropertyAttribute | |
{ | |
public float MinLimit, MaxLimit; | |
public FloatRangeAttribute(float minLimit, float maxLimit) | |
{ | |
this.MinLimit = minLimit; | |
this.MaxLimit = maxLimit; | |
} | |
} | |
[System.Serializable] | |
public class FloatRange | |
{ | |
public float RangeStart, RangeEnd; | |
private float GetRandomValue() | |
{ | |
return Random.Range(RangeStart, RangeEnd); | |
} | |
public static implicit operator float(FloatRange d) // implicit digit to byte conversion operator | |
{ | |
return d.GetRandomValue(); | |
} | |
} |
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
//Place in an "Editor" Folder. Keep your folders tidy or I will get you. | |
//apologies for the magic values below | |
using UnityEngine; | |
using UnityEditor; | |
using System.Collections; | |
using System.Collections.Generic; | |
[CustomPropertyDrawer( typeof( FloatRangeAttribute ) )] | |
public class FloatRangeDrawer : PropertyDrawer | |
{ | |
public override float GetPropertyHeight( SerializedProperty property, GUIContent label ) | |
{ | |
return base.GetPropertyHeight( property, label ) + 16; | |
} | |
// Draw the property inside the given rect | |
public override void OnGUI( Rect position, SerializedProperty property, GUIContent label ) | |
{ | |
// Now draw the property as a Slider or an IntSlider based on whether it’s a float or integer. | |
if (property.type != typeof(FloatRange).ToString()) | |
Debug.LogWarning("Use only with IntRange type"); | |
else | |
{ | |
FloatRangeAttribute range = attribute as FloatRangeAttribute; | |
SerializedProperty minValue = property.FindPropertyRelative("RangeStart"); | |
SerializedProperty maxValue = property.FindPropertyRelative("RangeEnd"); | |
float newMin = minValue.floatValue; | |
float newMax = maxValue.floatValue; | |
float xDivision = position.width * 0.4f; | |
float xLabelDiv = xDivision * 0.125f; | |
float yDivision = position.height * 0.5f; | |
EditorGUI.LabelField(new Rect(position.x, position.y, xDivision, yDivision) | |
, label); | |
Rect mmRect = new Rect(position.x + xDivision + xLabelDiv, position.y, position.width - (xDivision + xLabelDiv * 2), yDivision); | |
EditorGUI.MinMaxSlider(mmRect, ref newMin, ref newMax, range.MinLimit, range.MaxLimit); | |
Rect minRangeRect = new Rect(position.x + xDivision, position.y, xLabelDiv, yDivision); | |
minRangeRect.x += xLabelDiv * 0.5f - 12; | |
minRangeRect.width = 24; | |
EditorGUI.LabelField(minRangeRect, range.MinLimit.ToString()); | |
Rect maxRangeRect = new Rect(minRangeRect); | |
maxRangeRect.x = mmRect.xMax + xLabelDiv * 0.5f - 12; | |
maxRangeRect.width = 24; | |
EditorGUI.LabelField(maxRangeRect, range.MaxLimit.ToString()); | |
Rect minLabelRect = new Rect(mmRect); | |
minLabelRect.x = minLabelRect.x + minLabelRect.width * (newMin / range.MaxLimit); | |
minLabelRect.x -= 12; | |
minLabelRect.y += yDivision; | |
minLabelRect.width = 24; | |
newMin = Mathf.Clamp(EditorGUI.FloatField(minLabelRect, newMin), range.MinLimit, newMax); | |
//EditorGUI.LabelField(minLabelRect, newMin.ToString()); | |
Rect maxLabelRect = new Rect(mmRect); | |
maxLabelRect.x = maxLabelRect.x + maxLabelRect.width * (newMax / range.MaxLimit); | |
maxLabelRect.x -= 12; | |
maxLabelRect.x = Mathf.Max(maxLabelRect.x, minLabelRect.xMax); | |
maxLabelRect.y += yDivision; | |
maxLabelRect.width = 24; | |
newMax = Mathf.Clamp(EditorGUI.FloatField(maxLabelRect, newMax), newMin, range.MaxLimit); | |
//EditorGUI.LabelField(maxLabelRect, newMax.ToString()); | |
minValue.floatValue = newMin; | |
maxValue.floatValue = newMax; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment