Skip to content

Instantly share code, notes, and snippets.

View INeatFreak's full-sized avatar
🏳️
Abandoning Projects

Neat Freak INeatFreak

🏳️
Abandoning Projects
View GitHub Profile
@INeatFreak
INeatFreak / ScriptableObjectInitializationCallbacks.cs
Created July 12, 2022 11:22
Get callbacks in editor play mode changes to initialize ScriptableObjects.
using UnityEngine;
[CreateAssetMenu(fileName = "ExampleObject", menuName = "Examples/ExampleObject", order = 0)]
public class ExampleObject : ScriptableObject
{
#if UNITY_EDITOR
void OnEnable() {
EditorApplication.playModeStateChanged += OnPlayStateChange;
}
void OnDisable() {
@INeatFreak
INeatFreak / TruncateDecimals.cs
Last active October 1, 2022 18:06
Truncates the decimal places of given value to match desired decimal count.
/// <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;
@INeatFreak
INeatFreak / UnityEditor.Handles.DrawCameraOrientedSphere.cs
Created July 12, 2022 11:12
Draws camera oriented wire sphere using Handles.
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);
@INeatFreak
INeatFreak / RemapValueRange.cs
Last active December 19, 2023 18:59
Remaps the given value from one range to another.
/// <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;
}
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]
@INeatFreak
INeatFreak / UnityEditor.GetPropertyAttribute.cs
Last active January 26, 2024 02:37
Get [Space] Attribute from SerializedProperty
// 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;