A game about forced loneliness, made by TACStudios
at master 2.5 kB view raw
1#if PACKAGE_DOCS_GENERATION || UNITY_INPUT_SYSTEM_ENABLE_UI 2using UnityEngine.EventSystems; 3 4namespace 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, and all other navigation, using keyboard or 13 /// gamepad for example, will be constrained to game objects under that hierarchy. 14 /// </remarks> 15 [HelpURL(InputSystem.kDocUrl + "/manual/UISupport.html#multiplayer-uis")] 16 public class MultiplayerEventSystem : EventSystem 17 { 18 [Tooltip("If set, only process mouse and navigation events for any game objects which are children of this game object.")] 19 [SerializeField] private GameObject m_PlayerRoot; 20 21 /// <summary> 22 /// The root object of the UI hierarchy that belongs to the given player. 23 /// </summary> 24 /// <remarks> 25 /// This can either be an entire <c>Canvas</c> or just part of the hierarchy of 26 /// a specific <c>Canvas</c>. 27 /// </remarks> 28 public GameObject playerRoot 29 { 30 get => m_PlayerRoot; 31 set 32 { 33 m_PlayerRoot = value; 34 InitializePlayerRoot(); 35 } 36 } 37 38 protected override void OnEnable() 39 { 40 base.OnEnable(); 41 42 InitializePlayerRoot(); 43 } 44 45 protected override void OnDisable() 46 { 47 base.OnDisable(); 48 } 49 50 private void InitializePlayerRoot() 51 { 52 if (m_PlayerRoot == null) return; 53 54 var inputModule = GetComponent<InputSystemUIInputModule>(); 55 if (inputModule != null) 56 inputModule.localMultiPlayerRoot = m_PlayerRoot; 57 } 58 59 protected override void Update() 60 { 61 var originalCurrent = current; 62 current = this; // in order to avoid reimplementing half of the EventSystem class, just temporarily assign this EventSystem to be the globally current one 63 try 64 { 65 base.Update(); 66 } 67 finally 68 { 69 current = originalCurrent; 70 } 71 } 72 } 73} 74#endif