Skip to content

Instantly share code, notes, and snippets.

View simonwittber's full-sized avatar

Simon Wittber simonwittber

View GitHub Profile
@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
@simonwittber
simonwittber / SubMeshExtractor.cs
Created May 29, 2019 14:56
Extract a submesh from a mesh into a new mesh instance, for Unity3D.
public static Mesh Extract(Mesh m, int meshIndex)
{
var vertices = m.vertices;
var normals = m.normals;
var newVerts = new List<Vector3>();
var newNorms = new List<Vector3>();
var newTris = new List<int>();
var triangles = m.GetTriangles(meshIndex);
for (var i = 0; i < triangles.Length; i += 3)
@simonwittber
simonwittber / Domain.cs
Created May 1, 2019 12:56
DSL for HTN declaration.
public class AI
{
Domain CreateDomain()
{
using (domain = Domain.New())
{
worldState = DefineWorldState(health, fuel, speed, angularSpeed);
DefinePrimitiveTask(SetRandomDestination);
DefinePrimitiveTask(MoveToDestination)