Skip to content

Instantly share code, notes, and snippets.

View thebeardphantom's full-sized avatar

Mika Notarnicola thebeardphantom

View GitHub Profile
using Cysharp.Threading.Tasks;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using UnityEditor;
using UnityEditor.Recorder;
using UnityEditor.Recorder.Input;
using UnityEngine;
[DefaultExecutionOrder(-50)]
public class IconRecorder : MonoBehaviour
@thebeardphantom
thebeardphantom / SerializedPropertyUtility.cs
Created March 4, 2021 23:34
Find FieldInfo for a given SerializedProperty.
public static class SerializedPropertyUtility
{
private static readonly MethodInfo getFieldInfoFromProperty;
static SerializedPropertyUtility()
{
var scriptAttributeUtility = typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.ScriptAttributeUtility");
Assert.IsNotNull(scriptAttributeUtility, "ScriptAttributeUtility != null");
getFieldInfoFromProperty = scriptAttributeUtility.GetMethod(nameof(GetFieldInfoFromProperty), BindingFlags.NonPublic | BindingFlags.Static);
@thebeardphantom
thebeardphantom / FastNoiseRNG.cs
Created December 16, 2020 10:27
A mechanism to use FastNoiseLite as a proper RNG class.
using System.Collections.Generic;
using UnityEngine;
public class FastNoiseRNG : FastNoiseLite
{
#region Fields
private ulong _position;
#endregion
using UnityEngine;
public sealed class ReferenceDrawerAttribute : PropertyAttribute { }
@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]
@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 / GenerateReadonlySerializedField.snippet
Last active February 16, 2020 02:47
A C# snippet for generating a private serialized field and a public readonly property.
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Unity Field</Title>
<Shortcut>uf</Shortcut>
<Description>Code snippet for property and serialized backing field</Description>
<Author>TheBeardPhantom</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
@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 / PointerService.cs
Created January 19, 2020 01:28
Using UI EventSystem to check for presence of layers under pointer.
public class PointerService : IPointerService
{
#region Fields
private readonly List<RaycastResult> _raycastResults = new List<RaycastResult>();
private int _lastRaycastFrameCount = -1;
#endregion
@thebeardphantom
thebeardphantom / SpriteShapeEditorHelper.cs
Last active October 18, 2019 06:53
A utility class to center the transform position on SpriteShapeController splines.
public static class SpriteShapeEditorHelper
{
#region Methods
[MenuItem("CONTEXT/SpriteShapeController/Center Transform")]
private static void CenterTransform(MenuCommand cmd)
{
var controller = cmd.context as SpriteShapeController;
Undo.RecordObjects(