Skip to content

Instantly share code, notes, and snippets.

View mminer's full-sized avatar

Matthew Miner mminer

View GitHub Profile
@mminer
mminer / Flock.cs
Created November 4, 2024 01:35
2D flocking implementation for Unity.
using UnityEngine;
public partial class Flock : MonoBehaviour
{
[SerializeField, Min(0)] float spawnRadius = 5f;
[SerializeField, Min(1)] int unitCount = 50;
[SerializeField] Transform unitPrefab;
[Header("Unit")]
[SerializeField, Range(0, 360)] float fieldOfView = 270f;
@mminer
mminer / eventManager.lua
Created March 8, 2024 18:00
Simple event manager for Lua.
local eventManager <const> = {}
function addEventListener(eventName, listener)
local eventListeners = eventManager[eventName]
if not eventListeners then
eventListeners = {}
eventManager[eventName] = eventListeners
end
@mminer
mminer / AddButtonToUnityToolbar.cs
Last active September 28, 2024 03:02
Adds a UI element to the Unity 2022.1 toolbar using reflection.
var toolbarType = Assembly
.GetCallingAssembly()
.GetType("UnityEditor.Toolbar")!;
var toolbar = toolbarType
.GetField("get", BindingFlags.Public | BindingFlags.Static)!
.GetValue(null);
var toolbarRootVisualElement = toolbarType
.GetField("m_Root", BindingFlags.Instance | BindingFlags.NonPublic)!
@mminer
mminer / HVECEncoder.cs
Created March 14, 2022 02:38
Example of custom Unity Recorder encoder that outputs HVEC (H.265).
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
mminer / HSLandHSV.cs
Created March 11, 2022 23:36
Structs to represent HSL and HSV colours in Unity.
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
mminer / GetExtensionMethods.cs
Created March 2, 2022 17:42
C# function to get extension methods from an assembly.
// Adapted from https://stackoverflow.com/a/299526
static IEnumerable<MethodInfo> GetExtensionMethods(Assembly assembly, Type extendedType)
{
return assembly
.GetTypes()
.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
mminer / GetMainWindowPosition.cs
Created October 7, 2021 21:25
Unity function to get the main editor window's position.
// 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
mminer / GetMainEditorWindow.cs
Created October 7, 2021 21:20
Gets a reference to the main Unity editor window.
static Object GetMainEditorWindow()
{
var containerWindowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.ContainerWindow");
var showModeField = containerWindowType.GetField("m_ShowMode", BindingFlags.Instance | BindingFlags.NonPublic);
return Resources
.FindObjectsOfTypeAll(containerWindowType)
.First(window => (int)showModeField.GetValue(window) == 4);
}
@mminer
mminer / Draggable.cs
Last active September 28, 2021 23:57
Unity attribute to make Vector3 and Vector3[] fields draggable in the scene.
using UnityEngine;
/// <summary>
/// Attribute to add to Vector3 and Vector3[] fields to make them draggable in the scene.
/// </summary>
public class Draggable : PropertyAttribute
{
}
@mminer
mminer / GetPropertiesWithAttribute.cs
Created September 28, 2021 23:16
Unity function to get all properties in a SerializedObject with an attribute.
// Example usage inside Editor.OnSceneGUI for Vector3 fields:
//
// foreach (var property in GetPropertiesWithAttribute<MyCustomAttribute>(serializedObject))
// {
// Handles.Label(property.vector3Value, property.name);
// }
static IEnumerable<SerializedProperty> GetPropertiesWithAttribute<TAttribute>(SerializedObject serializedObject)
{
var targetObjectType = serializedObject.targetObject.GetType();