Skip to content

Instantly share code, notes, and snippets.

View simonwittber's full-sized avatar

Simon Wittber simonwittber

  • Different Methods
  • Australia
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 / RectBoundsHandle.cs
Created May 14, 2025 02:58
Handle for resizing rects in Unity.
using UnityEditor;
using UnityEngine;
public class RectBoundsHandle
{
public float HandleSize = 10f;
public float CornerGrabRadius = 12f;
public Color FillColor = new Color(0, 1, 1, 0.1f);
public Color OutlineColor = Color.cyan;
public Color HandleColor = Color.blue;
@simonwittber
simonwittber / StateMachine.cs
Created May 16, 2025 02:48
Generic StateMachine
using System;
using System.Collections.Generic;
public abstract class StateMachine<T> where T: struct, Enum
{
public T state { get; protected set; }
public Action<T, T> OnStateChanged;
@simonwittber
simonwittber / LayoutEditorStateMachine.cs
Last active May 16, 2025 04:23
State machine for working with graph like editors.
using UnityEditor;
using UnityEngine;
public enum Interaction
{
Idle,
MouseDownOnItem,
DragItemBegin,
MouseDownOnSelectedItem,
DragItemUpdate,
@simonwittber
simonwittber / PagedIntegerMap.cs
Last active July 21, 2025 01:31
A memory and CPU efficient integer to integer map.
using System;
using System.Collections.Generic;
// PagedMap is a memory-efficient map for large ranges of integer keys.
public class PagedIntegerMap
{
private const int PageBits = 10; // 1024 entries per page
private const int PageSize = 1 << PageBits;
private const int PageMask = PageSize - 1;