MultiplayerEventSystem.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #if PACKAGE_DOCS_GENERATION || UNITY_INPUT_SYSTEM_ENABLE_UI
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. namespace UnityEngine.InputSystem.UI
  5. {
  6. /// <summary>
  7. /// A modified EventSystem class, which allows multiple players to have their own instances of a UI,
  8. /// each with it's own selection.
  9. /// </summary>
  10. /// <remarks>
  11. /// You can use the <see cref="playerRoot"/> property to specify a part of the hierarchy belonging to the current player.
  12. /// Mouse selection will ignore any game objects not within this hierarchy. For gamepad/keyboard selection, you need to make sure that
  13. /// the navigation links stay within the player's hierarchy.
  14. /// </remarks>
  15. public class MultiplayerEventSystem : EventSystem
  16. {
  17. [Tooltip("If set, only process mouse events for any game objects which are children of this game object.")]
  18. [SerializeField] private GameObject m_PlayerRoot;
  19. public GameObject playerRoot
  20. {
  21. get => m_PlayerRoot;
  22. set => m_PlayerRoot = value;
  23. }
  24. protected override void Update()
  25. {
  26. var originalCurrent = current;
  27. current = this; // in order to avoid reimplementing half of the EventSystem class, just temporarily assign this EventSystem to be the globally current one
  28. try
  29. {
  30. base.Update();
  31. }
  32. finally
  33. {
  34. current = originalCurrent;
  35. }
  36. }
  37. }
  38. }
  39. #endif