Skip to content

Instantly share code, notes, and snippets.

View FlaShG's full-sized avatar

Sascha Graeff FlaShG

View GitHub Profile
@FlaShG
FlaShG / CanvasPositioningExtensions.cs
Last active July 12, 2024 20:51
A small Unity helper class to convert viewport, screen or world positions to canvas space.
using UnityEngine;
/// <summary>
/// Small helper class to convert viewport, screen or world positions to canvas space.
/// Only works with screen space canvases.
/// </summary>
/// <example>
/// <code>
/// objectOnCanvasRectTransform.anchoredPosition = specificCanvas.WorldToCanvasPoint(worldspaceTransform.position);
/// </code>
@FlaShG
FlaShG / FavoritesWindow.cs
Last active March 14, 2024 04:53
A Unity EditorWindow to pin objects to for quick selection.
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System.Collections.Generic;
/// <summary>
/// An editor window that lets you store assets and GameObjects in it for quickly finding them again.
/// </summary>
public class FavoritesWindow : EditorWindow, IHasCustomMenu
{
@FlaShG
FlaShG / Benchmark.cs
Last active January 3, 2024 15:40
A C# Benchmark class.
using System;
using System.Diagnostics;
/// <summary>
/// Simple benchmark class to measure average time consumption of code.
/// </summary>
public static class Benchmark
{
/// <summary>
/// A benchmark's time result.
@FlaShG
FlaShG / Invoker.cs
Last active September 4, 2023 09:47
A replacement for Unity's MonoBehaviour.Invoke and MonoBehaviour.InvokeRepeating, with extra features.
using System;
using System.Collections;
using UnityEngine;
/// <summary>
/// Replaces MonoBehaviour.Invoke and MonoBehaviour.InvokeRepeating
/// with a more sophisticated attempt. Namely: No strings involved.
/// Use like this:
/// StartCoroutine(Invoker.Invoke(MyMethod, 2));
/// </summary>
@FlaShG
FlaShG / CoroutineEvent.cs
Last active August 17, 2023 19:41
An event class for Unity that can run coroutines.
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// An event that can run coroutines.
/// Invoking it returns an IEnumerator that can be waited for in another coroutine.
/// The Invocation waits until all registered coroutines are finished.
/// </summary>
@FlaShG
FlaShG / HideFlagsUtility.cs
Last active June 13, 2023 06:12
Shows all GameObjects in the scene with hideFlags, so you can debug them.
using UnityEngine;
using UnityEditor;
public static class HideFlagsUtility
{
[MenuItem("Help/Hide Flags/Show All Objects")]
private static void ShowAll()
{
var allGameObjects = Object.FindObjectsOfType<GameObject>(true);
foreach (var go in allGameObjects)
@FlaShG
FlaShG / FloatRange.cs
Last active May 30, 2023 10:28
A small struct representing a range from min to max.
using UnityEngine;
[System.Serializable]
public struct FloatRange
{
public float min;
public float max;
public float range { get { return Mathf.Abs(max - min); } }
public FloatRange(float min, float max)
@FlaShG
FlaShG / FixedUpdateInterpolation.cs
Last active May 11, 2023 21:16
Interpolates a GameObject's position and rotation while being updated in FixedUpdate.
using UnityEngine;
using UnityEngine.LowLevel;
using System.Collections.Generic;
using UnityEngine.PlayerLoop;
/// <summary>
/// Interpolates a GameObject's position and rotation while being updated in FixedUpdate.
/// </summary>
public class FixedUpdateInterpolation : MonoBehaviour
@FlaShG
FlaShG / RuntimeSceneReference.cs
Created May 7, 2023 12:47
A serializable struct for referencing and, at runtime, handle scenes.
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using UnityEngine.SceneManagement;
[System.Serializable]
public struct RuntimeSceneReference : ISerializationCallbackReceiver
{
@FlaShG
FlaShG / UsePropertyNameAttribute.cs
Last active January 20, 2023 07:18
Fix the display name when serializing property backing fields in Unity.
using UnityEngine;
/// <summary>
/// Use this attribute in combination with a [SerializeField] attribute on top of a property to display the property name. Example:
/// [field: SerializeField, UsePropertyName]
/// public int number { get; private set; }
/// </summary>
public class UsePropertyNameAttribute : PropertyAttribute
{