Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Controlling player cursor across windowed UI

Discussion in 'Scripting' started by dannyryba, Jul 27, 2020.

  1. dannyryba

    dannyryba

    Joined:
    Jun 22, 2020
    Posts:
    45
    Hi everyone,

    I'm at a loss of how to approach a way to manipulate what sections of a UI interface a player can/can't access, based on what the cursor has selected.

    I have this UI mocked up:


    Here are my struggles, I'm hoping someone can help point me in the right direction for figuring this out:
    1. When this menu is opened, I would like for the 'Equipment' button to already start highlighted, and for movement up and down from Equipment through Crafting buttons to be handled with the arrow keys or WASD. While I know how to control player movement via scripting, I am unsure how to control something like this.

    2. When Equipment is selected using the Submit button (Enter in my case), the selector should then move to the item buttons shown on the right, allowing movement between with WASD/arrows as before. When an item is highlighted (such as the Rusty Sword), the interface below should update with its description and icon - this part I can probably figure out, since that opens up a whole can of worms on how I've coded the inventory objects that would probably need its own other thread. However, it would be nice to understand how UI elements such as that can be updated based on what is currently highlighted.

    3. And finally, if the player hits ESC while in this item area, their cursor moves back to the Equipment - Crafting area.

    Hopefully someone can help me understand - I have learned a lot about coding but in this area (coding to control the UI and highlighting), I am at a loss on where to begin. If I have caused any confusion in my explanation please let me know so I can make clarifications. :)

    Thanks in advance!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    Start looking into Unity UI Navigation: https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/script-SelectableNavigation.html and the Selectable component: https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/script-Selectable.html

    Of utmost importance also are the InputModule which lets you define which inputs control the navigation, and the Unity EventSystem, which among other things, is the object that keeps track of what is currently "selected" in the UI.

    https://learn.unity.com/tutorial/ui-navigation
     
    Last edited: Jul 27, 2020
    dannyryba likes this.
  3. dannyryba

    dannyryba

    Joined:
    Jun 22, 2020
    Posts:
    45
    Thanks for providing those resources. However, I'm not sure how to actually get the cursor moving. Right now it is only using the mouse to hover over the buttons, when I need it to be controlled by the keyboard. I can't add 'Selectable' to this because it already has a Button component, yet the button component has no options for determining how to select it. Here is my hierarchy if that helps, perhaps I have something wrong here:

     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    Buttons inherit from Selectable, so they are already Selectable. You need a few things for navigation to work:

    1. Make sure your EventSystem selects something in your UI to start with. You can set a "First Selected" object in your event system. Alternatively you can select something in OnEnable for your UI panel or wherever appropriate with EventSystem.current.SetSelectedGameObject(...).
    2. Make sure your Input Module (StandaloneInputModule or Input System UI Input Module) exists and the axes/inputs for ui navigation are set up properly.
    3. Buttons (like all Selectables) have navigation settings in the inspector for the Button component. Set those up to configure navigation directions. Auto usually works fine.
    4. Buttons (like all Selectables) have settings for appearance and transitions when being selected/deselected etc.. Make sure those are set up.
     
    dannyryba likes this.
  5. dannyryba

    dannyryba

    Joined:
    Jun 22, 2020
    Posts:
    45
    Ah this seemed to be the missing piece for me - I have not worked with the Event System before so that makes sense. Thank you!

    This brings me to another question. My inventory items (buttons) are instantiated as the player picks them up, using a prefab. How would it be possible to use the UnityEvent system to use OnClick on the Equipment button to then have the selector move to the first item in the item list, as long as there is one? I'm not sure if the UnityEvent system is able to do this, or if it would require some sort of script.