EventSystemSpawner.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="EventSystemSpawner.cs" company="Exit Games GmbH">
  3. // </copyright>
  4. // <summary>
  5. // For additive Scene Loading context, eventSystem can't be added to each scene and instead should be instantiated only if necessary.
  6. // https://answers.unity.com/questions/1403002/multiple-eventsystem-in-scene-this-is-not-supporte.html
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // --------------------------------------------------------------------------------------------------------------------
  10. using UnityEngine;
  11. using UnityEngine.EventSystems;
  12. namespace Photon.Pun.UtilityScripts
  13. {
  14. /// <summary>
  15. /// Event system spawner. Will add an EventSystem GameObject with an EventSystem component and a StandaloneInputModule component.
  16. /// Use this in additive scene loading context where you would otherwise get a "Multiple EventSystem in scene... this is not supported" error from Unity.
  17. /// </summary>
  18. public class EventSystemSpawner : MonoBehaviour
  19. {
  20. void OnEnable()
  21. {
  22. #if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
  23. Debug.LogError("PUN Demos are not compatible with the New Input System, unless you enable \"Both\" in: Edit > Project Settings > Player > Active Input Handling. Pausing App.");
  24. Debug.Break();
  25. return;
  26. #endif
  27. EventSystem sceneEventSystem = FindObjectOfType<EventSystem>();
  28. if (sceneEventSystem == null)
  29. {
  30. GameObject eventSystem = new GameObject("EventSystem");
  31. eventSystem.AddComponent<EventSystem>();
  32. eventSystem.AddComponent<StandaloneInputModule>();
  33. }
  34. }
  35. }
  36. }