Skip to content

Instantly share code, notes, and snippets.


Matthew Miner mminer

View GitHub Profile
mminer / AddButtonToUnityToolbar.cs
Last active Mar 15, 2022
Adds a UI element to the Unity 2022.1 toolbar using reflection.
View AddButtonToUnityToolbar.cs
var toolbarType = Assembly
var toolbar = toolbarType
.GetField("get", BindingFlags.Public | BindingFlags.Static)!
var toolbarRootVisualElement = toolbarType
.GetField("m_Root", BindingFlags.Instance | BindingFlags.NonPublic)!
mminer / HVECEncoder.cs
Created Mar 14, 2022
Example of custom Unity Recorder encoder that outputs HVEC (H.265).
View HVECEncoder.cs
using System.Diagnostics;
using Unity.Collections;
using UnityEditor.Media;
using UnityEditor.Recorder.Encoder;
class HVECEncoder : IEncoder
Process process;
public void OpenStream(IEncoderSettings settings, RecordingContext ctx)
mminer / HSLandHSV.cs
Created Mar 11, 2022
Structs to represent HSL and HSV colours in Unity.
View HSLandHSV.cs
using UnityEngine;
/// <summary>
/// Represents a color with hue, saturation, and lightness.
/// </summary>
readonly struct HSL
public Color Color => HSV.FromHSL(this).Color;
public readonly float H;
mminer / GetExtensionMethods.cs
Created Mar 2, 2022
C# function to get extension methods from an assembly.
View GetExtensionMethods.cs
// Adapted from
static IEnumerable<MethodInfo> GetExtensionMethods(Assembly assembly, Type extendedType)
return assembly
.Where(type => type.IsSealed && !type.IsGenericType && !type.IsNested)
.SelectMany(type => type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static))
.Where(method => method.IsDefined(typeof(ExtensionAttribute), false) &&
method.GetParameters()[0].ParameterType == extendedType);
mminer / GetMainWindowPosition.cs
Created Oct 7, 2021
Unity function to get the main editor window's position.
View GetMainWindowPosition.cs
// This contains no error checking, which you want for such fragile code.
// You also probably want to cache `mainWindow` if you call this often.
static Rect GetMainWindowPosition()
var containerWindowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.ContainerWindow");
var showModeField = containerWindowType.GetField("m_ShowMode", BindingFlags.Instance | BindingFlags.NonPublic);
var mainWindow = Resources.FindObjectsOfTypeAll(containerWindowType).First(window => (int)showModeField.GetValue(window) == 4);
var positionProperty = mainWindow.GetType().GetProperty("position", BindingFlags.Instance | BindingFlags.Public);
return (Rect)positionProperty.GetValue(mainWindow, null);
mminer / GetMainEditorWindow.cs
Created Oct 7, 2021
Gets a reference to the main Unity editor window.
View GetMainEditorWindow.cs
static Object GetMainEditorWindow()
var containerWindowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.ContainerWindow");
var showModeField = containerWindowType.GetField("m_ShowMode", BindingFlags.Instance | BindingFlags.NonPublic);
return Resources
.First(window => (int)showModeField.GetValue(window) == 4);
mminer / Draggable.cs
Last active Sep 28, 2021
Unity attribute to make Vector3 and Vector3[] fields draggable in the scene.
View Draggable.cs
using UnityEngine;
/// <summary>
/// Attribute to add to Vector3 and Vector3[] fields to make them draggable in the scene.
/// </summary>
public class Draggable : PropertyAttribute
mminer / GetPropertiesWithAttribute.cs
Created Sep 28, 2021
Unity function to get all properties in a SerializedObject with an attribute.
View GetPropertiesWithAttribute.cs
// Example usage inside Editor.OnSceneGUI for Vector3 fields:
// foreach (var property in GetPropertiesWithAttribute<MyCustomAttribute>(serializedObject))
// {
// Handles.Label(property.vector3Value,;
// }
static IEnumerable<SerializedProperty> GetPropertiesWithAttribute<TAttribute>(SerializedObject serializedObject)
var targetObjectType = serializedObject.targetObject.GetType();
mminer / RandomPointOnUnitCircle.cs
Created Sep 16, 2021
Unity function to get a random point on a unit circle, similar to Random.onUnitSphere.
View RandomPointOnUnitCircle.cs
static Vector2 RandomPointOnUnitCircle()
var angle = Random.Range(0f, Mathf.PI * 2);
var x = Mathf.Sin(angle);
var y = Mathf.Cos(angle);
return new Vector2(x, y);
mminer / StateMachine.cs
Created Sep 13, 2021
Base-bones C# state machine.
View StateMachine.cs
using System;
using System.Collections.Generic;
public class StateMachine
public interface IState
public void OnEnter();
public void OnExit();