Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Unity UI [SOLVED] Multiple Event System

Discussion in 'UGUI & TextMesh Pro' started by eron82, Jan 13, 2018.

  1. eron82

    eron82

    Joined:
    Mar 10, 2017
    Posts:
    69
    How can i have 2 Event Systems in the scene so 2 players can select different UI buttons to choose their character? (like tekken for example) Thanks
     
  2. eron82

    eron82

    Joined:
    Mar 10, 2017
    Posts:
    69
    Create 2 empty objects and add this script and a Standalone Input Model on both:
    Code (csharp):
    1. using UnityEngine.EventSystems;
    2. using UnityEngine;
    3.  
    4. public class MyEventSystem : EventSystem
    5. {
    6.  
    7. protected override void OnEnable(){
    8.         base.OnEnable ();
    9. }
    10.  
    11. protected override void Update(){
    12.     EventSystem originalCurrent = EventSystem.current;
    13.     current = this;
    14.     base.Update();
    15.     current = originalCurrent;
    16. }
    17. }
    Then add these two scripts on each button selecting one event systems:
    Code (csharp):
    1. using UnityEngine.EventSystems;
    2. using UnityEngine.UI;
    3.  
    4. public class MyButton : Button
    5. {
    6.     public EventSystem eventSystem;
    7.  
    8.     protected override void Awake()
    9.     {
    10.         base.Awake ();
    11.         eventSystem = GetComponent<MyEventSystemProvider> ().eventSystem;
    12.     }
    13.  
    14.     public override void OnPointerDown(PointerEventData eventData)
    15.     {
    16.         if (eventData.button != PointerEventData.InputButton.Left)
    17.             return;
    18.  
    19.         // Selection tracking
    20.         if (IsInteractable() && navigation.mode != Navigation.Mode.None)
    21.             eventSystem.SetSelectedGameObject(gameObject, eventData);
    22.  
    23.         base.OnPointerDown(eventData);
    24.     }
    25.  
    26.     public override void Select()
    27.     {
    28.         if (eventSystem.alreadySelecting)
    29.             return;
    30.  
    31.         eventSystem.SetSelectedGameObject(gameObject);
    32.     }
    33. }
    Code (csharp):
    1. using UnityEngine.EventSystems;
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MyEventSystemProvider : MonoBehaviour
    6. {
    7.     public EventSystem eventSystem;
    8. }
     
    DigitalDesignDude likes this.
  3. AshMcConnell

    AshMcConnell

    Joined:
    Aug 10, 2011
    Posts:
    7
    Hi @eron82 ,

    Does this still work for you in recent versions of Unity? I'm having trouble getting it to work for my splitscreen game

    Thanks for your help!
     
    dthoma6403 likes this.
  4. dthoma6403

    dthoma6403

    Joined:
    Nov 15, 2017
    Posts:
    2
    Hey @AshMcConnell In the next few days I plan on giving this a shot if you still need help
     
  5. AshMcConnell

    AshMcConnell

    Joined:
    Aug 10, 2011
    Posts:
    7
    Only seeing this now. How did you get on? I made a very basic custom solution that allows me to move between controls on a page (only buttons and a car selector for the moment), but a "proper" solution would be great :)
     
  6. davdeo

    davdeo

    Joined:
    Aug 11, 2019
    Posts:
    4
    Hey @dthoma6403 did you already solve the problem?
    I am having the same problem as described above but i need to support 4 Players at the same time (local multiplayer character selection)
    Thank you in advance :)
     
  7. mr_blahblah

    mr_blahblah

    Joined:
    Jan 15, 2016
    Posts:
    36
    Anyone else still having this problem? I am, and the solution above doesn't work for me, unless you need to replicate this for each individual button...
     
  8. DigitalDesignDude

    DigitalDesignDude

    Joined:
    Mar 25, 2020
    Posts:
    3
    It's working alright for my project :).

    Don't know if using unity 2017 affects this or not but here are some additional instructions that might help.


    Create 2 empty objects and add this script and a Standalone Input Model on both. these will act as event systems for each player. Delete any other event systems in your scene to prevent issues.

    Code (CSharp):
    1. using UnityEngine.EventSystems;
    2. using UnityEngine;
    3. public class MyEventSystem : EventSystem
    4. {
    5. protected override void OnEnable(){
    6.         base.OnEnable ();
    7. }
    8. protected override void Update(){
    9.     EventSystem originalCurrent = EventSystem.current;
    10.     current = this;
    11.     base.Update();
    12.     current = originalCurrent;
    13. }
    14. }
    In the standalone input components, assign the horizontal and vertical axis, submit and cancel buttons, to the unique inputs your players are using. They shouldn't be using the same inputs for this to work.


    For each button that player 1 controls, add the following scripts then assign player 1's event system object as the event system in the "My Event System Provider Script".

    You'll also have to remove the old button script sadly and customize your buttons' colours again.

    Repeat this for all of player's 2 buttons except you'll use player 2's event system obj instead.

    Code (CSharp):
    1. using UnityEngine.EventSystems;
    2. using UnityEngine;
    3. using System.Collections;
    4. public class MyEventSystemProvider : MonoBehaviour
    5. {
    6.     public EventSystem eventSystem;
    7. }

    Code (CSharp):
    1. using UnityEngine.EventSystems;
    2. using UnityEngine.UI;
    3. public class MyButton : Button
    4. {
    5.     public EventSystem eventSystem;
    6.     protected override void Awake()
    7.     {
    8.         base.Awake ();
    9.         eventSystem = GetComponent<MyEventSystemProvider> ().eventSystem;
    10.     }
    11.     public override void OnPointerDown(PointerEventData eventData)
    12.     {
    13.         if (eventData.button != PointerEventData.InputButton.Left)
    14.             return;
    15.         // Selection tracking
    16.         if (IsInteractable() && navigation.mode != Navigation.Mode.None)
    17.             eventSystem.SetSelectedGameObject(gameObject, eventData);
    18.         base.OnPointerDown(eventData);
    19.     }
    20.     public override void Select()
    21.     {
    22.         if (eventSystem.alreadySelecting)
    23.             return;
    24.         eventSystem.SetSelectedGameObject(gameObject);
    25.     }
    26. }

    Finally set all of your button's navigation in the "My Button (Script)" component to "Explicit" navigation so that you control the order in which buttons are selected. Otherwise, player 2 may be able to select player 1's buttons by accident due to unity's odd automatic button navigation.
     
unityunity