Skip to content

Instantly share code, notes, and snippets.

View mminer's full-sized avatar

Matthew Miner mminer

View GitHub Profile
@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();
@mminer
mminer / RandomPointOnUnitCircle.cs
Created September 16, 2021 00:23
Unity function to get a random point on a unit circle, similar to Random.onUnitSphere.
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
mminer / StateMachine.cs
Last active February 3, 2023 04:57
Bare-bones C# state machine.
using System;
using System.Collections.Generic;
public class StateMachine
{
public interface IState
{
public void OnEnter();
public void OnExit();
}
@mminer
mminer / NavMeshPathGetLength.cs
Created September 8, 2021 19:55
Unity NavMeshPath extension function to calculate its length.
public static float GetLength(this NavMeshPath path)
{
if (path == null || path.status == NavMeshPathStatus.PathInvalid || path.corners.Length <= 1)
{
return Mathf.Infinity;
}
var length = 0f;
for (var i = 1; i < path.corners.Length; i++)
@mminer
mminer / ConditionalEnableAttribute.cs
Last active September 8, 2021 18:56
Unity property attribute to conditionally enable a property in the inspector.
using System;
using UnityEngine;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Struct)]
public class ConditionalEnableAttribute : PropertyAttribute
{
public readonly string ConditionalPropertyName;
public ConditionalEnableAttribute(string conditionalPropertyName)
{
@mminer
mminer / GetBounds.cs
Created July 29, 2021 22:08
Unity GameObject extension to calculate its bounds.
public static Bounds GetBounds(this GameObject gameObject)
{
var bounds = new Bounds(gameObject.transform.position, Vector3.zero);
var renderers = gameObject.GetComponentsInChildren<Renderer>();
foreach (var renderer in renderers)
{
bounds.Encapsulate(renderer.bounds);
}
@mminer
mminer / CinemachineBrainTimelineBinder.cs
Created July 22, 2021 01:09
Unity component to bind the main camera's Cinemachine brain to a timeline's Cinemachine tracks.
using System.Linq;
using Cinemachine;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
[ExecuteInEditMode]
public class CinemachineBrainTimelineBinder : MonoBehaviour
{
void Start()