Skip to content

Instantly share code, notes, and snippets.

View FlaShG's full-sized avatar

Sascha Graeff FlaShG

View GitHub Profile
@FlaShG
FlaShG / CoroutineWorker.cs
Last active September 7, 2021 07:44
Based on MyStaticCode.cs, a class that allows anything to run coroutines from anywhere.
using UnityEngine;
using System.Collections;
public static class CoroutineWorker
{
private class Worker : MonoBehaviour { }
private static Worker worker;
[RuntimeInitializeOnLoadMethod]
@FlaShG
FlaShG / MyStaticCode.cs
Last active January 25, 2022 15:29
Execute any code based on Unity events without having to drag a component into a scene.
using UnityEngine;
/// <summary>
/// This template allows to define code that runs independently of any GameObjects or Components created in the editor, even though using Unity events.
/// It can be used for any Scene-independent code, including coroutines, without having to manually add a component to a scene.
/// </summary>
public static class MyStaticCode
{
[RuntimeInitializeOnLoadMethod]
private static void Initialize()
@FlaShG
FlaShG / ConsoleAccessAttribute.cs
Created April 1, 2019 01:19
A simple, easy-to-use console for Unity.
using System;
[AttributeUsage(AttributeTargets.Method)]
public class ConsoleAccessAttribute : Attribute
{
}
@FlaShG
FlaShG / StateMachine.cs
Last active August 21, 2022 15:15
A simple state machine class for Unity. Use it to run regular methods or coroutines as states.
using UnityEngine;
using System;
using System.Collections;
/// <summary>
/// A simple state machine class. Use it to run regular methods or coroutines as states.
/// Steps to use:
/// 1. Define a private field for the StateMachine object.
/// private StateMachine stateMachine;
/// 2. Initialize the object in Awake. Pass the MonoBehaviour object to the constructor.
@FlaShG
FlaShG / CorouTweenTest.cs
Last active August 26, 2022 00:56
Very simple Tweens for Unity using Coroutines.
using UnityEngine;
public class CorouTweenTest : MonoBehaviour
{
[SerializeField]
private Vector3 targetPosition;
[SerializeField]
private float duration = 1;
private void Start()
@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
{
@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 / 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 / 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 / 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)