Skip to content

Instantly share code, notes, and snippets.

View simonwittber's full-sized avatar

Simon Wittber simonwittber

View GitHub Profile
@simonwittber
simonwittber / Interpolation.cs
Last active December 28, 2017 09:50
Linear interpolation is boring...
using System.Runtime.CompilerServices;
using UnityEngine;
/// <summary>
/// Convert linear 0.0f - 1.0f values to some other non-linear value.
/// </summary>
public static class Interpolation
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Boing(float v) => (Mathf.Sin(v * Mathf.PI * (0.2f + 2.5f * v * v * v)) * Mathf.Pow(1f - v, 2.2f) + v) * (1f + (1.2f * (1f - v)));
@simonwittber
simonwittber / ComponentPool.cs
Last active May 17, 2021 12:06
The last game object pool you will ever need... maybe.
using System.Collections.Generic;
using UnityEngine;
public class ComponentPool<T> where T : Component
{
static ComponentPool<T> Instance = new ComponentPool<T>();
Dictionary<int, Stack<T>> pools = new Dictionary<int, Stack<T>>();
Dictionary<int, int> instances = new Dictionary<int, int>();
@simonwittber
simonwittber / SceneContext.cs
Created February 7, 2018 15:55
Add an "OnContextClick" message to any open Unity Editors.
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Reflection;
[InitializeOnLoad]
public static class SceneContext
{
static System.Diagnostics.Stopwatch clickClock;
@simonwittber
simonwittber / PaletteWizard.cs
Created February 13, 2018 13:57
A Unity wizard to generate a palette from a source image.
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class PaletteWizard : ScriptableWizard
{
public Texture2D sourceImage;
[Range(1, 5)]
@simonwittber
simonwittber / CommandGizmos.cs
Created February 14, 2018 10:50
SceneView Gizmos labels that don't create clutter.
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public static class CommandGizmos
{
static GUIStyle sceneNote;
static CommandGizmos()
@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)
{
@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 / 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 / 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 / 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);