Skip to content

Instantly share code, notes, and snippets.

View beardordie's full-sized avatar

Beard or Die beardordie

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / SnapToGround.cs
Created July 3, 2019 01:59
Unity Editor script with shorcut key (Shift + End) to snap a selected object to the surface beneath it, with full undo support
using UnityEditor;
using UnityEngine;
public class SnapToGround : Editor
{
[MenuItem("Tools/Snap Selection To Ground #END")]
public static void SnapToGroundNow()
{
foreach (Transform t in Selection.transforms)
{
Undo.RegisterCompleteObjectUndo(Selection.transforms, "SnapToGroundNow");
@beardordie
beardordie / CreateMaterialsForTextures.cs
Created April 23, 2019 05:15
Unity Editor script. Select multiple texture files for one material in Project window, then click Tools -> Create Materials For Textures. A new material will be created. Adjust Smoothness and other settings to match expected render.
using UnityEngine;
using UnityEditor;
using System.Linq;
// Original source: http://answers.unity.com/answers/746039/view.html
public class CreateMaterialsForTextures : ScriptableWizard
{
[MenuItem("Tools/CreateMaterialsForTextures")]
static void CreateMaterials()
@beardordie
beardordie / ConstantRotation.cs
Last active August 28, 2021 15:33
Unity helper MonoBehaviour to apply constant World-space rotation to a GameObject
using UnityEngine;
public class ConstantRotation : MonoBehaviour
{
public bool randomizeValues;
public Vector3 speed = new Vector3(0f, 100f, 0f);
public float multiplier = 1;
public Vector2 randomMultiplierRange = new Vector2(90f, 120f);
void Start()