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; | |
[CreateAssetMenu(fileName = "ExampleObject", menuName = "Examples/ExampleObject", order = 0)] | |
public class ExampleObject : ScriptableObject | |
{ | |
#if UNITY_EDITOR | |
void OnEnable() { | |
EditorApplication.playModeStateChanged += OnPlayStateChange; | |
} | |
void OnDisable() { |
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
/// <summary>Truncates the decimal places of <paramref name="value"/> to match <paramref name="decimalCount"/>.</summary> | |
/// <example> 1.14f.TruncateDecimals(1) --> 1.1f </example> | |
public static float TruncateDecimals(this float value, int decimalCount) | |
{ | |
var rate = Math.Pow(10, decimalCount); | |
return (float) (Math.Truncate(value * rate) / rate); | |
// This below uses the rounding option, which is not precise: | |
//float rounded = (float)Math.Round(value, decimalCount, MidpointRounding.AwayFromZero); | |
//return rounded; |
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
void DrawCameraOrientedWireSphere(Vector3 position, float radius, Color color) { | |
UnityEditor.Handles.matrix = Matrix4x4.TRS(Vector3.zero, transform.rotation, Vector3.one); | |
UnityEditor.Handles.color = color; | |
UnityEditor.Handles.DrawWireDisc(position, Vector3.right, radius); | |
UnityEditor.Handles.DrawWireDisc(position, Vector3.up, radius); | |
UnityEditor.Handles.DrawWireDisc(position, Vector3.forward, radius); | |
if(Camera.current.orthographic) { | |
Vector3 normal = position - UnityEditor.Handles.inverseMatrix.MultiplyVector(Camera.current.transform.forward); |
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
/// <summary>Remaps the given value from one range to another. Source: <a href="https://gist.github.com/INeatFreak/b30b9849793ccfd8c96b19131d756273">GitHub</a> </summary> | |
/// <param name="source">The value to remap.</param> | |
/// <param name="sourceMin">Minimum range of the source value.</param> | |
/// <param name="sourceMax">Maximum range of the source value.</param> | |
/// <param name="targetMin">Minimum range of the return value.</param> | |
/// <param name="targetMax">Maximum range of the return value.</param> | |
public float RemapValueRange(float source, float sourceMin, float sourceMax, float targetMin, float targetMax) { | |
source = Mathf.Clamp(source, sourceMin, sourceMax); // ensure that source value is in the given range or it can output higher/lower values than requested value range! | |
return (source - sourceMin) * (targetMax - targetMin) / (sourceMax - sourceMin) + targetMin; | |
} |
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 System; | |
using UnityEngine; | |
#if UNITY_2020_1_OR_NEWER | |
// good to go | |
#else | |
#error "Optional<T> plugin requires Unity 2020.1 or above for it to work. On below versions serializing fields of generic types was not possible." | |
#endif | |
[Serializable] |
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
// source: https://forum.unity.com/threads/serialiedproperty-check-if-it-has-a-propertyattribute.436103/ | |
private static T GetPropertyAttribute<T>(SerializedProperty prop, bool inherit) where T : PropertyAttribute | |
{ | |
if (prop == null) | |
return null; | |
Type t = prop.serializedObject.targetObject.GetType(); | |
FieldInfo f = null; |