Skip to content

Instantly share code, notes, and snippets.

View mminer's full-sized avatar

Matthew Miner mminer

View GitHub Profile
@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)
{
@mminer
mminer / VisualEffectPlayRate.cs
Created June 1, 2021 18:00
Unity component to change a visual effect's play rate.
using UnityEngine;
using UnityEngine.VFX;
[ExecuteInEditMode]
[RequireComponent(typeof(VisualEffect))]
public class VisualEffectPlayRate : MonoBehaviour
{
[SerializeField, Range(0, 10)] float playRate = 1f;
VisualEffect vfx;
@mminer
mminer / RotateAroundCircle.cs
Created May 18, 2021 19:26
Unity scene view handles to rotate an object around a circle.
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
public class RotateAroundCircle : MonoBehaviour
{
public Color color = Color.cyan;
public Vector3 pivot;
}