Skip to content

Instantly share code, notes, and snippets.

@FlaShG FlaShG/MyStaticCode.cs
Last active Jan 5, 2020

Embed
What would you like to do?
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
{
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
You can’t perform that action at this time.