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 / 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)
@simonwittber
simonwittber / ColorBlindCorrection.cs
Last active October 16, 2025 17:31
Color Blindness Correction PostProcessing Effect for Unity.
using System;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
[Serializable]
[PostProcess(typeof(ColorBlindCorrectionRenderer), PostProcessEvent.AfterStack, "Custom/ColorBlindCorrection")]
public sealed class ColorBlindCorrection : PostProcessEffectSettings
{
[Header("1:Protanopia 2:Deuteranopia 3:Tritanopia")]
[Range(0, 2)]
@simonwittber
simonwittber / InstanceTracker.cs
Created March 25, 2019 09:04
Tracks enabled MonoBehaviours.
public class InstanceTracker<T> : MonoBehaviour where T : MonoBehaviour
{
public static List<T> Instances { get; private set; } = new List<T>();
int instanceIndex = 0;
void OnEnable()
{
instanceIndex = Instances.Count;
Instances.Add(this as T);
}
@simonwittber
simonwittber / AddThingToSomeScriptableObject.cs
Last active February 12, 2019 11:34
Using Unity's ScriptableWizards as Dialogs in CustomEditors.
public class AddThingToSomeScriptableObject : ScriptableWizard
{
internal SomeScriptableObject someScriptableObject;
public SomeThing someThing;
void OnCreateButton()
{
//add someThing to someScriptableObject.
}
}
@simonwittber
simonwittber / InstancedRenderer.cs
Created February 11, 2019 13:02
Instance Rendering Helper
public class InstancedRenderer : System.IDisposable
{
const int BATCH_MAX = 1023;
public NativeArray<Matrix4x4> matrices;
public readonly int count, allocated;
public readonly Mesh mesh;
public readonly Material material;
Matrix4x4[] batchedMatrices = new Matrix4x4[BATCH_MAX];