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 / Goroutine.cs
Created October 1, 2018 14:18
Pooled coroutines for Unity.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StopAction : YieldInstruction
{
}
public class GoroutinePool
public static class ServiceLocator<TA> where TA : class
{
static System.Func<TA> constructor = null;
static System.Type type;
public static void Bind<TB> () where TB:TA,new()
{
type = typeof(TB);
@simonwittber
simonwittber / HeapQueue.cs
Created August 16, 2018 12:09
Pooled Discrete Event Simulator in C#
using System;
using System.Collections.Generic;
namespace Simulator
{
public class HeapQueue<T> where T : IComparable<T>
{
List<T> items;
public int Count { get { return items.Count; } }
@simonwittber
simonwittber / Event.cs
Created July 31, 2018 08:58
Another pub-sub system for loose coupling.
/*
// Example Usage:
public class SomeEv : Event<SomeEv> { }
public class SomeOtherEv : Event<SomeOtherEv> { }
public class EVTest : MonoBehaviour
{
void Start()
@simonwittber
simonwittber / TouchGesture.cs
Last active July 10, 2018 14:14
Simple touch recogniser for Unity.
public class TouchRecognizer : MonoBehaviour
{
const float LOWPASS_FILTER = 0.1f;
const float ANGLE_DELTA_THRESHOLD = 0.001f;
const float PINCH_DELTA_THRESHOLD = 0.1f;
const float TIME_REQUIRED_FOR_SINGLE_TOUCH = 0.05f;
float _angleDelta, _pinchDelta, angleDelta, pinchDelta;
Vector3 delta;
@simonwittber
simonwittber / GameEvent.cs
Last active July 31, 2018 08:56
A pub-sub system used for coordinating loosely coupled systems.
/// <summary>
/// This is a pubsub system used for coordinating loosely coupled systems.
/// It does some tricks with an internal class to improve the appearance
/// of the public API.
/// </summary>
public static class GameEvent
{
/* Public API */
public static void Publish<T>(T ev) => _InternalGameEvent<T>.Publish(ev);
@simonwittber
simonwittber / AssetForgePostProcessor.cs
Last active July 16, 2023 20:41
A Unity post processor to optimize meshes created with Asset Forge. Reduces draw calls and allows batching.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class AssetForgePostProcessor : AssetPostprocessor
{
@simonwittber
simonwittber / MonoBehaviourComponent.cs
Last active March 29, 2021 02:28
MonoBehaviour classes which allow for Batch Updating.
using System.Collections.Generic;
using UnityEngine;
public abstract class MonoBehaviourSystem<T> : MonoBehaviour where T : MonoBehaviourComponent<T>
{
protected abstract void UpdateBatch(Queue<T> components);
void Update()
{
UpdateBatch(ComponentBatch<T>.components);
@simonwittber
simonwittber / FastSine.cs
Created March 23, 2018 11:12
A fast and accurate Sin for C#
float Sin(float x)
{
while (x < -3.14159265f)
x += 6.28318531f;
while (x > 3.14159265f)
x -= 6.28318531f;
float sin;
var xZ = x < 0 ? -1 : 1;
sin = 1.27323954f * x - xZ * .405284735f * x * x;
var sinZ = sin < 0 ? -1 : 1;
@simonwittber
simonwittber / MyClassDrawer.cs
Last active February 22, 2018 09:26
A unique ID for your Serialized classes in Unity.
[Serializable]
public class MyClass {
public int id;
}
[CustomPropertyDrawer(typeof(MyClass))]
public class MyClassDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{