Skip to content

Instantly share code, notes, and snippets.

@FlaShG
Last active January 25, 2022 15:29
Show Gist options
  • Save FlaShG/9f5b3632415df42f1272e7b6eba7ba36 to your computer and use it in GitHub Desktop.
Save FlaShG/9f5b3632415df42f1272e7b6eba7ba36 to your computer and use it in GitHub Desktop.
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()
{
var go = new GameObject("MyStaticCode");
go.AddComponent<Worker>();
go.hideFlags = HideFlags.HideAndDontSave;
Object.DontDestroyOnLoad(go);
}
private class Worker : MonoBehaviour
{
private void Update()
{
Debug.Log("What's up?");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment