Skip to content

Instantly share code, notes, and snippets.

View simonwittber's full-sized avatar

Simon Wittber simonwittber

View GitHub Profile
@simonwittber
simonwittber / Observer.cs
Last active May 4, 2025 11:57
Simply binding to anything using a VisualElement.schedule extension
// Usage: schedule.Watch(Func<T> getValue, Action<T> valueWasChanged)
// Eg: visualElement.schedule.Watch(() => myInstance.someProperty, i => visualElement.text = $"Value:{i}");
public class Observer<T>
{
private readonly Func<T> _getValue;
private readonly Action<T> _onChange;
private T _lastValue;
public Observer(Func<T> getValue, Action<T> onChange)
@simonwittber
simonwittber / Tree.cs
Last active December 22, 2024 04:18
A generic tree data structure.
using System;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class Tree<T>
{
[Serializable]
private struct Node
{
@simonwittber
simonwittber / GetSetCompiler.cs
Last active August 24, 2022 05:17
Create Getter and Setter methods at runtime
using System.Reflection;
using System.Reflection.Emit;
using System;
using System.Collections.Generic;
namespace GetSetGenerator
{
public interface IGetSet<TInstance, TField>
{
TField Get(TInstance item);
@simonwittber
simonwittber / ByteArrayPool.cs
Created August 20, 2021 08:31
A bytearray pool that tracks its objects using weak references.
using System;
using System.Collections.Generic;
public static class ByteArrayPool
{
private static Queue<WeakReference> pool = new Queue<WeakReference>();
public static ArraySegment<byte> Take(int count)
{
// if there are any items to check in the pool
@simonwittber
simonwittber / ComponentBatch.cs
Created August 1, 2021 13:55
Component Systems using Jobs and Burst
using System.Collections.Generic;
using Unity.Jobs;
using UnityEngine;
static internal class ComponentBatch<T, U, V>
where T : MonoBehaviourComponent<T, U, V> where U : MonoBehaviourSystem<T, U, V>
where V: struct, IJobParallelFor
{
internal static Queue<T> components = new Queue<T>();
internal static Queue<T> newComponents = new Queue<T>();
using System;
using UnityEngine;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine.Assertions;
[StructLayout(LayoutKind.Explicit)]
struct SpatialKey
{
[FieldOffset(0)] public Int32 xz;
@simonwittber
simonwittber / Logger.cs
Last active June 3, 2021 02:48
Unity Logger with Color and Headings
using UnityEngine;
public class Logger
{
string header;
public bool enabled = true;
public Logger(string name, string color)
{
header = $"<color={color}>{name}:</color>";
}
@simonwittber
simonwittber / CleanupMissingScriptsHelper.cs
Created January 14, 2021 07:15
Cleanup missing scripts on Prefabs.
using UnityEditor;
using UnityEngine;
public class CleanupMissingScriptsHelper
{
[MenuItem("Assets/Cleanup Missing Scripts")]
private static void CleanupMissingScripts()
{
foreach (var i in Selection.gameObjects)
{
@simonwittber
simonwittber / SomeBehaviour.HDRP.cs
Created June 1, 2020 01:33
Howto: Write package dependent code for Unity.
#if USE_HDRP
public partial class SomeBehaviour : MonoBehaviour
{
partial public void SomeMethod() {
}
}
#endif
@simonwittber
simonwittber / ScheduleSystem.cs
Last active May 10, 2021 12:38
A pattern to implement a scheduler in Unity DOTS.
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
namespace PatternsOfDots
{
/// <summary>
/// Add a WaitForSeconds component to your entity with a delay parameter.
/// The ScheduleSystem will then wait that many seconds before removing
/// the component, and add a Ready tag component. You can then process