Last active
June 12, 2020 07:25
-
-
Save Nrjwolf/cd0b5b920f50a098db7959e21c6232be to your computer and use it in GitHub Desktop.
Custom Value to create constant/random value in range/random value in two ranges
This file contains hidden or 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 Random = UnityEngine.Random; | |
using Sirenix.OdinInspector; | |
[System.Serializable] | |
[InlineProperty(LabelWidth = 0)] | |
public class CustomValue | |
{ | |
[System.Serializable] | |
[HideLabel, LabelWidth(50)] | |
[InlineProperty(LabelWidth = 0)] | |
public struct RangeValue | |
{ | |
public RangeValue(float from, float to) | |
{ | |
From = from; | |
To = to; | |
} | |
[HorizontalGroup("Group 1")] public float From; | |
[HorizontalGroup("Group 1")] public float To; | |
public float GetRandom() => Random.Range(From, To); | |
} | |
public enum ValueType | |
{ | |
Constant, | |
RandomRange, | |
RandomRangeDouble, | |
} | |
[HideLabel, LabelWidth(50)] | |
public ValueType Type; | |
[ShowIf("Type", ValueType.Constant)] public float Value; | |
[ShowIf("Type", ValueType.RandomRange)] public RangeValue ValueRandomRange; | |
[ShowIf("Type", ValueType.RandomRangeDouble)] public RangeValue ValueRandomRangeDouble1; | |
[ShowIf("Type", ValueType.RandomRangeDouble)] public RangeValue ValueRandomRangeDouble2; | |
public CustomValue(float constant) | |
{ | |
Type = ValueType.Constant; | |
Value = constant; | |
} | |
public CustomValue(float valueRangeFrom, float valueRangeTo) | |
{ | |
Type = ValueType.RandomRange; | |
ValueRandomRange = new RangeValue(valueRangeFrom, valueRangeTo); | |
} | |
public CustomValue(float valueRangeFrom1, float valueRangeTo1, float valueRangeFrom2, float valueRangeTo2) | |
{ | |
Type = ValueType.RandomRangeDouble; | |
ValueRandomRangeDouble1 = new RangeValue(valueRangeFrom1, valueRangeTo1); | |
ValueRandomRangeDouble2 = new RangeValue(valueRangeFrom2, valueRangeTo2); | |
} | |
public float GetValue() | |
{ | |
if (Type == ValueType.Constant) return Value; | |
else if (Type == ValueType.RandomRange) return ValueRandomRange.GetRandom(); | |
else if (Type == ValueType.RandomRangeDouble) return Random.value > .5 ? ValueRandomRangeDouble1.GetRandom() : ValueRandomRangeDouble2.GetRandom(); | |
else return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Features
Uses odin inspector for a beautiful display in the inspector

Example