Skip to content

Instantly share code, notes, and snippets.

View FlaShG's full-sized avatar

Sascha Graeff FlaShG

View GitHub Profile
@FlaShG
FlaShG / ClassicEditorArrowMovement.cs
Last active July 18, 2018 14:24
A Unity editor script for enabling pre-2018.x scene view camera behaviour for arrow keys.
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
/// <summary>
/// Re-enables pre-2018.x scene view camera behaviour for arrow keys.
/// Allows the user to move the scene view camera on a horizontal plane.
/// </summary>
public static class ClassicEditorArrowMovement
{
@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 / ParticleManipulator.cs
Last active February 17, 2021 10:22
A superclass for classes that manipulate particles in Update.
using UnityEngine;
/// <summary>
/// Create a subclass of this class to manipulate particles in Update any way you see fit.
/// </summary>
[RequireComponent(typeof(ParticleSystem))]
[ExecuteInEditMode]
public abstract class ParticleManipulator : MonoBehaviour
{
new protected ParticleSystem particleSystem { get; private set; }
@FlaShG
FlaShG / CanvasPositioningExtensions.cs
Last active April 26, 2024 12:46
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 / 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 / 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 / PathAttribute.cs
Last active July 4, 2018 16:20
Allows setting a string in the Unity Editor by dragging a file or folder from outside the editor.
using UnityEngine;
public class PathAttribute : PropertyAttribute
{
}
@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 / SetScriptExecutionOrder.cs
Last active July 4, 2018 16:24
Add this to a MonoBehaviour to set its Script Execution Order without access to the editor.
#if UNITY_EDITOR
private const int executionOrder = -1000;
[UnityEditor.InitializeOnLoadMethod]
private static void SetScriptOrder()
{
var go = new GameObject("Temp");
var monoScript = UnityEditor.MonoScript.FromMonoBehaviour(go.AddComponent<NAME_OF_THIS_MONOBEHAVIOUR>());
if (UnityEditor.MonoImporter.GetExecutionOrder(monoScript) != executionOrder)
{
@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()