Skip to content

Instantly share code, notes, and snippets.

View beardordie's full-sized avatar

Beard or Die beardordie

View GitHub Profile
@beardordie
beardordie / SelectionHistoryWindow.cs
Created July 3, 2019 02:39
Unity editor script adds a custom Window to show scene selection history, check to pin to favorite for easy reselection, from FPSSample
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.Linq;
public class SelectionHistoryWindow : EditorWindow
{
[MenuItem("Tools/Selection History Window")]
[MenuItem("Window/Selection History")]
@beardordie
beardordie / FindEmptyFolders.cs
Created July 3, 2019 02:51
Unity Editor menu command to select all Empty folders in the project, easily clean them up or find abandoned or forgotten folders. From FPSSample
using System.Collections.Generic;
using UnityEditor;
public static class FindEmptyFolders
{
[MenuItem("Assets/Find Empty Folders")]
static void OnFindEmptyFolders()
{
var empties = new List<UnityEngine.Object>();
foreach (var d in System.IO.Directory.GetDirectories("Assets", "*", System.IO.SearchOption.AllDirectories))
@beardordie
beardordie / RevertSelectedPrefabs.cs
Created July 3, 2019 03:02
Unity editor script to add a menu command that Reverts all selected prefabs. Includes safe confirmation dialogue and Undo support. Upgraded from FPSSample
using UnityEditor;
using UnityEngine;
public class RevertSelectedPrefabs
{
[MenuItem("Tools/Revert Selected Prefabs")]
static void Execute()
{
if (Selection.gameObjects.Length < 1) Debug.Log("No selection to revert.");
if (EditorUtility.DisplayDialog("Revert Selected Prefabs?",
@beardordie
beardordie / FindTools.cs
Created July 3, 2019 04:23
Unity editor script, adds two menu items to find items. Originally from FPSSample
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class FindWindow
{
[MenuItem("GameObject/Find/Instances of prefab", false, -100)]
static void InstancesOfPrefabInScene()
{
var selected = Selection.activeGameObject;
@beardordie
beardordie / References.cs
Last active July 16, 2023 04:10
Unity Static References class for reducing GetComponent and Find calls for object references throughout a project
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Static References helper using Dictionaries
///
/// Why? Eliminate calls to GetComponent and FindObjectOfType and other such calls for objects that you often need to reference.
/// Cache them once here, then re-use that reference project-wide
/// Singletons won't need this as they store their own static reference
///
@beardordie
beardordie / AutoMover2D.cs
Last active August 28, 2021 15:27
Unity - simple 2D auto movement component with nice Gizmo, separated from Unity Playground, added two movement types, follows Open/Closed principle
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class AutoMover2D : MonoBehaviour
{
public enum AutoMovementType
{
AddForceEveryFrame,
AddImpulseForce,
@beardordie
beardordie / AutoRotator2D.cs
Created July 4, 2019 18:07
Unity - simple 2D auto rotation component with nice Gizmo, separated from Unity Playground, added a rotation type, follows Open/Closed principle
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class AutoRotator2D : MonoBehaviour
{
public enum AutoRotateType
{
AddForceEveryFrame,
AddTorqueImpulse
}
@beardordie
beardordie / Patrol2D.cs
Created July 4, 2019 20:55
Unity - simple 2D patrol component, upgraded from Unity Playground with gizmos and new functionality, follows Open/Closed principle, uses NaughtyAttributes for reorderable list of waypoints (remove line 4 and line 28 if unwanted)
using System;
using System.Collections.Generic;
using UnityEngine;
using NaughtyAttributes;
[RequireComponent(typeof(Rigidbody2D))]
public class Patrol2D : MonoBehaviour
{
public enum FacingType
{
@beardordie
beardordie / Wander2D.cs
Created July 4, 2019 23:49
Unity - Simple Wander 2D component, significantly upgraded from Unity Playground
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody2D))]
public class Wander2D : MonoBehaviour
{
public enum MovementType
{
Smooth,
@beardordie
beardordie / OnTriggerEnterDoStuff.cs
Created July 14, 2019 00:46
Unity - use LayerMask with collision
public LayerMask layerMask;
private void OnTriggerEnter(Collider other)
{
if (((1 << other.gameObject.layer) & layerMask) != 0)
{
// Do stuff
}
}