Last active
January 25, 2022 15:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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