Skip to content

Instantly share code, notes, and snippets.

@adammyhre
adammyhre / AllocCounter.cs
Created October 19, 2025 12:34
Allocation Analyzer
using System;
using UnityEngine.Profiling;
// See UnityEngine.TestTools.Constraints.AllocatingGCMemoryConstraint
// and https://maingauche.games/devlog/20230504-counting-allocations-in-unity/
public class AllocCounter {
UnityEngine.Profiling.Recorder rec;
public AllocCounter() {
rec = Recorder.Get("GC.Alloc");
@adammyhre
adammyhre / EnemyWorldUI.cs
Created August 24, 2025 09:23
Unity 6.2 World Space UI
using System;
using System.Collections.Generic;
using TMPro;
using Unity.Cinemachine;
using UnityEngine;
using UnityEngine.UI;
using UnityUtils;
[DisallowMultipleComponent]
public class EnemyWorldUI : MonoBehaviour {
@Kosmik123
Kosmik123 / FogColorSynchronization
Created May 24, 2025 14:24
Synchronize Camera background color and Scene fog color in Unity
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
public static class FogColorSynchronization
{
private const string EnableSyncMenuItemPath = "CONTEXT/Camera/Enable Synchronize Fog Color";
private const string DisableSyncMenuItemPath = "CONTEXT/Camera/Disable Synchronize Fog Color";
private enum ColorChange
@deltacode1
deltacode1 / Fullscreen.cs
Last active May 10, 2025 03:59
Fullscreen game window in unity editor
#if UNITY_EDITOR
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
public class FullscreenHotkeyHandler : MonoBehaviour
{
bool makeFullscreenAtStart = true;
@chamchi0809
chamchi0809 / SingletonScriptable.cs
Last active October 18, 2025 04:30
Using ScriptableObject as singleton manager.
using System;
using UnityEngine;
using UnityEngine.AddressableAssets;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.AddressableAssets;
using UnityEditor.AddressableAssets.Settings;
#endif
public class SingletonScriptable<T> : ScriptableObject where T : SingletonScriptable<T>
@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 / 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");
@MirzaBeig
MirzaBeig / SimpleAudioGenerator.cs
Created August 15, 2024 03:52
A simple procedural audio (pure sine wave) generator for Unity, using OnAudioFilterRead().
using UnityEngine;
// MB: Simple procedural audio generator.
// Generates a pure sine wave/tone at a given frequency.
public class SimpleAudioGenerator : MonoBehaviour
{
int outputSampleRate;
const float TAU = Mathf.PI * 2.0f;
@adammyhre
adammyhre / GravityWell.cs
Last active March 31, 2025 02:32
Player Controller Sandbox Scripts
using UnityEngine;
using UnityUtils;
[RequireComponent(typeof(TriggerArea))]
public class GravityWell : MonoBehaviour {
TriggerArea triggerArea;
void Start() {
triggerArea = GetComponent<TriggerArea>();
}
@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();