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 / 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 / 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 / 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 / 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>";
}