Skip to content

Instantly share code, notes, and snippets.

@margaretdax
margaretdax / PooledEnumerator.cs
Last active April 12, 2021 18:28
Example implementation of an abstraction to "simply" implement object pooling for compiler generated IEnumerator state machines. Makes use of goto and requires a great deal of usage dicipline. Not ideal but for those applications that can't tollerate IEnumerator GC/alloc costs.
using System;
using System.Collections;
using Zenject;
public abstract class PooledEnumerator<T> : IDisposable, IEnumerator where T : PooledEnumerator<T>, new()
{
private static readonly StaticMemoryPool<T> Pool = new StaticMemoryPool<T>();
// ReSharper disable once StaticMemberInGenericType
private static readonly object CompletedSignal = new object();
@margaretdax
margaretdax / PausableBehaviour.cs
Last active March 24, 2021 11:52
Tools I used in the development of OX OX OH for fully pausable/resumable "Coroutines" that can be run via Unity Coroutines but is not dependent on Unity at all. The most important thing here is the Run function, which takes a Stack of IEnumerator. The first IEnumerator in the stack passed to run should be whatever you'd normally pass to StartCor…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PausableBehaviour : MonoBehaviour
{
private Stack<IEnumerator> stack = new Stack<IEnumerator>();
private Coroutine stackRoutine;
private void StartStack()