Skip to content

Instantly share code, notes, and snippets.

@ErnSur
Last active August 26, 2023 14:27
Show Gist options
  • Save ErnSur/ad2714817999daa97f86aef53367de39 to your computer and use it in GitHub Desktop.
Save ErnSur/ad2714817999daa97f86aef53367de39 to your computer and use it in GitHub Desktop.
Event system for uGUI singletons
public class FallbackEventSystem : MonoBehaviour
{
private GameObject _fallbackEventSystem;
private void Awake()
{
CreateFallbackEventSystem();
}
private void OnEnable()
{
// If Event System doesn't exist in the scene
// Activate the fallback event system
_fallbackEventSystem.SetActive(EventSystem.current == null);
}
private void OnDisable()
{
_fallbackEventSystem.SetActive(false);
}
private void CreateFallbackEventSystem()
{
_fallbackEventSystem =
new GameObject("Fallback Event System", typeof(EventSystem), typeof(StandaloneInputModule));
_fallbackEventSystem.transform.SetParent(transform);
_fallbackEventSystem.SetActive(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment