Skip to content

Instantly share code, notes, and snippets.

@adammyhre
adammyhre / Either.cs
Created December 1, 2024 12:29
Monads, Nullables, and the Power of Optionals in Unity
using System;
using System.Collections.Generic;
public struct Either<TLeft, TRight> {
readonly TLeft left;
readonly TRight right;
readonly bool isRight;
Either(TLeft left, TRight right, bool isRight) {
this.left = left;
@adammyhre
adammyhre / Signal.cs
Last active November 24, 2024 19:54
Signals for Unity C#
using System;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Represents a signal that allows subscribers to register callbacks that are invoked when the signal is dispatched.
/// </summary>
public class Signal<T> {
readonly LinkedList<Listener> listeners = new ();
readonly object lockObject = new ();
@adammyhre
adammyhre / DamageCalculator.cs
Created November 17, 2024 08:57
Expression Trees in Unity Examples
using System;
using System.Linq.Expressions;
using UnityEngine;
public class DamageCalculator : MonoBehaviour {
void Start() {
Func<int, int, int> damageExpression = CreateDamageEvaluator();
int damage = damageExpression(10, 2);
Debug.Log($"Calculated Damage: {damage}");
@adammyhre
adammyhre / HierarchyIconDrawer.cs
Created November 9, 2024 16:54
Unity Hierarchy Icons
using UnityEditor;
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
[InitializeOnLoad]
public static class HierarchyIconDrawer {
static readonly Texture2D requiredIcon = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/_Project/Art/RequiredIcon.png");
@adammyhre
adammyhre / PushDownAutomata.cs
Created October 26, 2024 16:50
Stack-Based Finite State Machine (Push Down Automata)
using System.Collections.Generic;
namespace Automata {
public abstract class State {
protected readonly PushDownAutomata pda;
protected State(PushDownAutomata pda) {
this.pda = pda;
}
@adammyhre
adammyhre / OptimalSpatialHashing.cs
Created October 6, 2024 12:56
Unity Spatial Hashing with Jobs and Burst
using System;
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class OptimalSpatialHashing : MonoBehaviour {
@adammyhre
adammyhre / Smooth Follow
Created August 31, 2024 16:14
Smooth Follow Spline Auto Dolly for Cinemachine 3.1.1 Example
using System;
using Unity.Cinemachine;
using UnityEngine;
using UnityEngine.Splines;
[Serializable]
public class SmoothFollow : SplineAutoDolly.ISplineAutoDolly {
[Tooltip("Maximum speed of the dolly along the spline.")]
public float MaxSpeed = 0.5f;
@adammyhre
adammyhre / UniTaskHelpers.cs
Created August 25, 2024 18:29
A helper class for UniTask that implements a WhenAllSettled method, allowing all tasks to complete and capturing both successful results and exceptions.
using System;
using Cysharp.Threading.Tasks;
/// <summary>
/// Provides helper methods for working with UniTask, including methods to await multiple tasks
/// and capture their results or exceptions.
/// </summary>
public class UniTaskHelpers {
/// <summary>
/// Awaits two UniTasks and returns a tuple containing the results or exceptions for each task.
@adammyhre
adammyhre / FixUnityBrokenSelectionBase.cs
Created August 7, 2024 19:27
Custom Unity Editor Script to Fix [SelectionBase] for Consistent Parent Selection
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
// Put this in an Editor folder
[InitializeOnLoad]
public class FixUnityBrokenSelectionBase : Editor {
static List<Object> newSelection;
static Object[] lastSelection = { };
@adammyhre
adammyhre / ProjectSetup.cs
Last active November 27, 2024 03:01
Automated Unity Project Setup Script
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEditor.PackageManager.Requests;
using UnityEngine;
using static System.Environment;