Skip to content

Instantly share code, notes, and snippets.

Sascha Graeff FlaShG

  • Hamburg
View GitHub Profile
@FlaShG
FlaShG / CanvasPositioningExtensions.cs
Last active Jul 5, 2020
A small Unity helper class to convert viewport, screen or world positions to canvas space.
View CanvasPositioningExtensions.cs
using UnityEngine;
/// <summary>
/// Small helper class to convert viewport, screen or world positions to canvas space.
/// Only works with screen space canvases.
/// Usage:
/// objectOnCanvasRectTransform.anchoredPosition = specificCanvas.WorldToCanvasPoint(worldspaceTransform.position);
/// </summary>
public static class CanvasPositioningExtensions
{
@FlaShG
FlaShG / CoroutineWorker.cs
Last active Sep 25, 2018
Based on MyStaticCode.cs, a class that allows anything to run coroutines from anywhere.
View CoroutineWorker.cs
using UnityEngine;
using System.Collections;
public static class CoroutineWorker
{
private class Worker : MonoBehaviour { }
private static Worker worker;
[RuntimeInitializeOnLoadMethod]
@FlaShG
FlaShG / FloatRange.cs
Last active Jun 24, 2020
A small struct representing a range from min to max.
View FloatRange.cs
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 Jul 4, 2018
Allows setting a string in the Unity Editor by dragging a file or folder from outside the editor.
View PathAttribute.cs
using UnityEngine;
public class PathAttribute : PropertyAttribute
{
}
@FlaShG
FlaShG / Invoker.cs
Last active Mar 2, 2020
A replacement for Unity's MonoBehaviour.Invoke and MonoBehaviour.InvokeRepeating, with extra features.
View Invoker.cs
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 Jul 4, 2018
Add this to a MonoBehaviour to set its Script Execution Order without access to the editor.
View SetScriptExecutionOrder.cs
#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 Jan 5, 2020
Execute any code based on Unity events without having to drag a component into a scene.
View MyStaticCode.cs
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 / Placr.cs
Last active Oct 27, 2019
Placr increases your level design speed by letting you place objects quickly. https://youtu.be/nm57A2ySfIY
View Placr.cs
using UnityEngine;
using UnityEditor;
/// <summary>
/// Placr increases your level design speed by letting you place objects quickly.
/// Enable Placr in the scene view and select a Prefab or 3D model in your project view.
/// * Left click to place the object.
/// * Right click to de-select the object.
/// * Hold Ctrl to rotate the object with your mouse.
/// * Hold Shift to change the vertical offset. Re-select your asset to reset it.
@FlaShG
FlaShG / CoroutinePoolThread.cs
Last active Oct 28, 2018
Small classes that allow for comfortably executing a single thread or a threadpool task in a coroutine.
View CoroutinePoolThread.cs
using UnityEngine;
using System;
using System.Threading;
/// <summary>
/// Orders the Threadpool to perform an action and waits until it's done.
/// Use for short actions that maybe are performed more often.
/// </summary>
public class CoroutinePoolThread : CustomYieldInstruction
{
@FlaShG
FlaShG / FixedUpdateInterpolation.cs
Last active May 29, 2020
Interpolates a GameObject's position and rotation while being updated in FixedUpdate.
View FixedUpdateInterpolation.cs
using UnityEngine;
using System.Collections;
/// <summary>
/// Interpolates a GameObject's position and rotation while being updated in FixedUpdate.
/// </summary>
[DefaultExecutionOrder(-100)]
public class FixedUpdateInterpolation : MonoBehaviour
{
private Vector3 pos0;
You can’t perform that action at this time.