Skip to content

Instantly share code, notes, and snippets.

View mminer's full-sized avatar

Matthew Miner mminer

View GitHub Profile
@mminer
mminer / GetPropertiesWithAttribute.cs
Created September 28, 2021 23:16
Unity function to get all properties in a SerializedObject with an attribute.
// Example usage inside Editor.OnSceneGUI for Vector3 fields:
//
// foreach (var property in GetPropertiesWithAttribute<MyCustomAttribute>(serializedObject))
// {
// Handles.Label(property.vector3Value, property.name);
// }
static IEnumerable<SerializedProperty> GetPropertiesWithAttribute<TAttribute>(SerializedObject serializedObject)
{
var targetObjectType = serializedObject.targetObject.GetType();
@mminer
mminer / RandomPointOnUnitCircle.cs
Created September 16, 2021 00:23
Unity function to get a random point on a unit circle, similar to Random.onUnitSphere.
static Vector2 RandomPointOnUnitCircle()
{
var angle = Random.Range(0f, Mathf.PI * 2);
var x = Mathf.Sin(angle);
var y = Mathf.Cos(angle);
return new Vector2(x, y);
}
@mminer
mminer / StateMachine.cs
Last active February 3, 2023 04:57
Bare-bones C# state machine.
using System;
using System.Collections.Generic;
public class StateMachine
{
public interface IState
{
public void OnEnter();
public void OnExit();
}
@mminer
mminer / NavMeshPathGetLength.cs
Created September 8, 2021 19:55
Unity NavMeshPath extension function to calculate its length.
public static float GetLength(this NavMeshPath path)
{
if (path == null || path.status == NavMeshPathStatus.PathInvalid || path.corners.Length <= 1)
{
return Mathf.Infinity;
}
var length = 0f;
for (var i = 1; i < path.corners.Length; i++)
@mminer
mminer / ConditionalEnableAttribute.cs
Last active September 8, 2021 18:56
Unity property attribute to conditionally enable a property in the inspector.
using System;
using UnityEngine;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Struct)]
public class ConditionalEnableAttribute : PropertyAttribute
{
public readonly string ConditionalPropertyName;
public ConditionalEnableAttribute(string conditionalPropertyName)
{
@mminer
mminer / GetBounds.cs
Created July 29, 2021 22:08
Unity GameObject extension to calculate its bounds.
public static Bounds GetBounds(this GameObject gameObject)
{
var bounds = new Bounds(gameObject.transform.position, Vector3.zero);
var renderers = gameObject.GetComponentsInChildren<Renderer>();
foreach (var renderer in renderers)
{
bounds.Encapsulate(renderer.bounds);
}
@mminer
mminer / CinemachineBrainTimelineBinder.cs
Created July 22, 2021 01:09
Unity component to bind the main camera's Cinemachine brain to a timeline's Cinemachine tracks.
using System.Linq;
using Cinemachine;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
[ExecuteInEditMode]
public class CinemachineBrainTimelineBinder : MonoBehaviour
{
void Start()
@mminer
mminer / BezierControlPointMode.cs
Last active July 5, 2021 23:17
Unity spline / Bezier curves adapted from Catlike Coding's "Curves and Splines" tutorial.
/// <summary>
/// Modes that specify how the control points affect a Bezier curve.
/// </summary>
public enum BezierControlPointMode
{
Aligned,
Free,
Mirrored,
}
@mminer
mminer / AddressablesSizeAnalyzeRule.cs
Created June 24, 2021 21:59
Unity Addressables Analyze rule that reports the size of bundles after building them.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.AddressableAssets.Build;
using UnityEditor.AddressableAssets.Build.AnalyzeRules;
using UnityEditor.AddressableAssets.Settings;
using UnityEngine;
@mminer
mminer / OffsetGrabInteractable.cs
Created June 7, 2021 22:54
Unity XR component to move objects relative to their original position.
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class OffsetGrabInteractable : XRGrabInteractable
{
Vector3 interactorPosition;
Quaternion interactorRotation;
protected override void OnSelectEntered(XRBaseInteractor interactor)
{