Skip to content

Instantly share code, notes, and snippets.

View thebeardphantom's full-sized avatar

Mika Notarnicola thebeardphantom

View GitHub Profile
@thebeardphantom
thebeardphantom / Normalizer.cs
Last active August 29, 2015 14:11
Ever need to normalize an angle of 1748 or -191857 degrees to 0...360 or -180...180? Now you can!
namespace BSGTools.Math {
public static class Normalizer {
public static float NormalizeAngle(float value) {
return Normalize(value, 0f, 360f);
}
public static float Normalize(float value, float start, float end) {
float width = end - start;
float offsetValue = value - start; // value relative to 0
@thebeardphantom
thebeardphantom / .gitignore
Last active April 9, 2017 13:08
Unity GitIgnore
# Ignore everything
/*
/*/
# Inverse ignore some stuff
!/Assets/
!/ProjectSettings/
!.gitignore
# OS Stuff
@thebeardphantom
thebeardphantom / NavMeshAgentVisualizer.cs
Last active April 24, 2017 17:48
NavMeshAgent's shape visualization is broken! Here's a temporary fix. Attach this to a NavMeshAgent's GameObject to get an accurate visualization.
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
[ExecuteInEditMode]
public class NavMeshAgentVisualizer : MonoBehaviour
{
#if UNITY_EDITOR
private NavMeshAgent agent;
private Mesh cylinderMesh;
@thebeardphantom
thebeardphantom / SceneListExporter.cs
Last active September 11, 2022 23:21
Unity doesn't provide an API to get all of the scenes in your build. This script will automatically (at script reload), or manually through the Assets menu, generate a text file that contains all of your enabled scenes in your build settings in a Resources folder. This can then be read at runtime and stored in an array.
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
public static class SceneListExporter
{
[DidReloadScripts]
[MenuItem("Assets/Generate Scene List")]
@thebeardphantom
thebeardphantom / ExecuteEditorTests.cs
Last active November 11, 2018 08:01
How to execute edit mode tests from script. Only subscribes to finished events. Requires that you can reference test assemblies.
private static void ExecuteTests()
{
// Select all types in appdomain
var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).ToArray();
// Need to create an empty runner filter
var filterType = types.First(t => t.Name == "TestRunnerFilter");
var filter = Activator.CreateInstance(filterType);
// Create a test launcher
@thebeardphantom
thebeardphantom / EditorHelper.cs
Created July 29, 2019 03:13
Show confirmation dialog before actually quitting the Unity Editor.
using UnityEditor;
[InitializeOnLoad]
public static class EditorHelper
{
#region Constructors
static EditorHelper()
{
EditorApplication.wantsToQuit += OnEditorWantsToQuit;
@thebeardphantom
thebeardphantom / UnityNullCheck.cs
Last active October 12, 2021 19:07
Useful if you don't know the type of an object and you want to ensure you don't bypass Unity's native-side null checks.
public static bool IsNull<T>(this T obj) where T : class
{
if(obj is UnityEngine.Object unityObj)
{
/* Invokes Unity's custom == check for nulls.
* This case technically only works because obj is
* a "fake null" on the C# side by this point.
* If its a true null it'd hit the else case instead,
* which fortunately still returns what we want.
*/
@thebeardphantom
thebeardphantom / SmoothDampState.cs
Last active September 24, 2020 07:05
A serializable struct wrapper for smooth damping. Debug inspector will show you Current Value and Velocity!
using System;
using UnityEngine;
[Serializable]
public struct SmoothDampState
{
#region Fields
public float Damping;
@thebeardphantom
thebeardphantom / TilemapGraph.cs
Last active October 29, 2020 06:27
An example of a custom graph that extends A* Pathfinding Project to directly use a Unity Tilemap for building nodes and navigation. Also uses https://gist.github.com/thebeardphantom/6a05ef68ce6ce7c890c4a4d9afaccf74
using Pathfinding;
using Pathfinding.Serialization;
using Pathfinding.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Tilemaps;
[Preserve]
using UnityEngine;
public sealed class ReferenceDrawerAttribute : PropertyAttribute { }