Skip to content

Instantly share code, notes, and snippets.

@adammyhre
adammyhre / GravityWell.cs
Last active July 21, 2024 20:41
Player Controller Sandbox Scripts
using UnityEngine;
using UnityUtils;
[RequireComponent(typeof(TriggerArea))]
public class GravityWell : MonoBehaviour {
TriggerArea triggerArea;
void Start() {
triggerArea = GetComponent<TriggerArea>();
}
@adammyhre
adammyhre / AnimationSystem.cs
Created July 14, 2024 02:01
AnimationSystem with Audio and Awaitables
using System;
using UnityEngine.Animations;
using UnityEngine.Playables;
using UnityEngine;
[Serializable]
public class AnimationConfig {
public Animator animator;
public AnimationClip idleClip;
public AnimationClip walkClip;
@adammyhre
adammyhre / AnimationSystem.cs
Last active July 19, 2024 12:20
Simple Wrapper for Unity Playables API
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;
using MEC; // Uses More Effective Coroutines from the Unity Asset Store
public class AnimationSystem {
PlayableGraph playableGraph;
readonly AnimationMixerPlayable topLevelMixer;
@adammyhre
adammyhre / AnimatorControllerCloneTool.cs
Last active June 29, 2024 06:53
Code to clone an existing Animator Controller Asset with or without Motions
using UnityEditor;
using UnityEditor.Animations;
public static class AnimatorControllerCloneTool {
[MenuItem("Assets/Clone Animator Controller", true)]
static bool CanCloneAnimatorController() {
return Selection.activeObject is AnimatorController;
}
[MenuItem("Assets/Clone Animator Controller")]
@adammyhre
adammyhre / MemorySnapshotter.cs
Last active June 17, 2024 13:31
Utility for taking Unity Memory Snapshots Programmatically
using System;
using System.IO;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Profiling;
using Unity.Profiling.Memory;
using UnityEngine;
public class MemorySnapshotter {
const string capturesFolder = "MemoryCaptures";
@adammyhre
adammyhre / ComponentCopier.cs
Last active May 16, 2024 02:50
Copy and Paste All Components Between GameObjects
#if UNITY_EDITOR
using UnityEditorInternal;
using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
/// <summary>
/// A Unity editor extension for copying and pasting all components between GameObjects.
/// The tool supports handling multiple components of the same type and correctly restores
@adammyhre
adammyhre / PriorityQueue.cs
Created April 29, 2024 02:40
Double Key Priority Queue for Unity C#
using System;
using System.Collections.Generic;
public class PriorityQueue<T> {
class KeyNodeComparer : IComparer<(Key, T)> {
public int Compare((Key, T) x, (Key, T) y) {
return x.Item1 < y.Item1 ? -1 : x.Item1 > y.Item1 ? 1 : 0;
}
}
@adammyhre
adammyhre / DStarLite.cs
Last active May 5, 2024 13:50
Implementation of D* Lite Algorithm for Unity
// D* Lite is an incremental heuristic search algorithm that finds the shortest path in a graph
// from a goal node to a start node. It is designed to be efficient in terms of memory and computation
// while updating the graph in real-time.
// Koenig and Likhachev - http://idm-lab.org/bib/abstracts/papers/aaai02b.pdf
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
@adammyhre
adammyhre / ImprovedTimers.cs
Last active July 22, 2024 05:53
Improved Timers for Unity
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.LowLevel;
using UnityEngine.PlayerLoop;
namespace ImprovedTimers {
public static class TimerManager {
static readonly List<Timer> timers = new();
@adammyhre
adammyhre / Mediator.cs
Created March 31, 2024 02:50
Unity Mediator
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public abstract class Mediator<T> : MonoBehaviour where T : Component, IVisitable {
readonly List<T> entities = new List<T>();
public void Register(T entity) {
if (!entities.Contains(entity)) {