Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MultiplayerEventSystem.cs 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #if PACKAGE_DOCS_GENERATION || UNITY_INPUT_SYSTEM_ENABLE_UI
  2. using UnityEngine.EventSystems;
  3. namespace UnityEngine.InputSystem.UI
  4. {
  5. /// <summary>
  6. /// A modified EventSystem class, which allows multiple players to have their own instances of a UI,
  7. /// each with it's own selection.
  8. /// </summary>
  9. /// <remarks>
  10. /// You can use the <see cref="playerRoot"/> property to specify a part of the hierarchy belonging to the current player.
  11. /// Mouse selection will ignore any game objects not within this hierarchy, and all other navigation, using keyboard or
  12. /// gamepad for example, will be constrained to game objects under that hierarchy.
  13. /// </remarks>
  14. [HelpURL(InputSystem.kDocUrl + "/manual/UISupport.html#multiplayer-uis")]
  15. public class MultiplayerEventSystem : EventSystem
  16. {
  17. [Tooltip("If set, only process mouse and navigation events for any game objects which are children of this game object.")]
  18. [SerializeField] private GameObject m_PlayerRoot;
  19. /// <summary>
  20. /// The root object of the UI hierarchy that belongs to the given player.
  21. /// </summary>
  22. /// <remarks>
  23. /// This can either be an entire <c>Canvas</c> or just part of the hierarchy of
  24. /// a specific <c>Canvas</c>.
  25. /// </remarks>
  26. public GameObject playerRoot
  27. {
  28. get => m_PlayerRoot;
  29. set
  30. {
  31. m_PlayerRoot = value;
  32. InitializePlayerRoot();
  33. }
  34. }
  35. protected override void OnEnable()
  36. {
  37. base.OnEnable();
  38. InitializePlayerRoot();
  39. }
  40. protected override void OnDisable()
  41. {
  42. base.OnDisable();
  43. }
  44. private void InitializePlayerRoot()
  45. {
  46. if (m_PlayerRoot == null) return;
  47. var inputModule = GetComponent<InputSystemUIInputModule>();
  48. if (inputModule != null)
  49. inputModule.localMultiPlayerRoot = m_PlayerRoot;
  50. }
  51. protected override void Update()
  52. {
  53. var originalCurrent = current;
  54. current = this; // in order to avoid reimplementing half of the EventSystem class, just temporarily assign this EventSystem to be the globally current one
  55. try
  56. {
  57. base.Update();
  58. }
  59. finally
  60. {
  61. current = originalCurrent;
  62. }
  63. }
  64. }
  65. }
  66. #endif