Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Unity UI Toolkit + New Input System

Discussion in 'UI Toolkit' started by equal, Jul 10, 2023.

  1. equal

    equal

    Joined:
    Dec 14, 2012
    Posts:
    77
    Hi,

    some months ago i changed to the new Input System.
    I like it, because rather then doing "if keyDown is 'F' do something" i can instead declare some Action.
    Later i can bind this action to whatever key without changing code.

    now i changed from UI to UI Toolkit.
    This allowes me to RegisterCallbacks to Elements.
    But this is again the old way "if keyDown is 'F' do something".

    Here is what i have:
    upload_2023-7-10_16-55-1.png

    And i would like to use this Action, like this:
    Code (CSharp):
    1. .RegisterCallback<basicDefaultInput.Player.FocusSelection>(FocusSelection);
    But this does not work.
    How can i make this work ?
    Even if it would be hefty work around.



    Any help is welcome!
     
    Last edited: Jul 10, 2023
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
  3. equal

    equal

    Joined:
    Dec 14, 2012
    Posts:
    77
    Thank you spiney199,

    after looking more into this i changed my mind. Now i will do this:
    The Hard UI throu UI Toolkit will have some RegisterCallback but not too many because those will be unREbindable.
    The rest, mainly ingame controlls, will go throu the New Input System.

    But out of curiosity, what do you mean with this:
    Do you have any Examples or Vids i could check out on this?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    Just a central manager that you can read/register all inputs through. Be it a singleton object, static class, whatever. I don't know any tutorials, it's something you should be able to do with enough C#/Unity knowledge.
     
  5. equal

    equal

    Joined:
    Dec 14, 2012
    Posts:
    77
    You mean like an InputManager Singleton that does Update( AnyInput -> some Action ) ?
    But thats currently how it is handled.
    I hoped you had something else in mind.

    Thank you non the less
     
  6. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    What I do is this:

    Code (csharp):
    1.  
    2.             // == stage navigation
    3.  
    4.             var btnPrevious = element.Q<Button>("button-prev");
    5.             btnPrevious.clicked += () =>
    6.             {
    7.                 previousPanel();
    8.             };
    9.             var btnNext = element.Q<Button>("button-next");
    10.             btnNext.clicked += () =>
    11.             {
    12.                 nextPanel();
    13.             };
    14.  
    and this:

    Code (csharp):
    1.  
    2.             controls.PlayMenu.NextPanel.performed += nextPanel;
    3.             controls.PlayMenu.PreviousPanel.performed += previousPanel;
    4.  
    and:

    Code (csharp):
    1.  
    2.         private void nextPanel()
    3.         {
    4. ...
    5.         }
    6.  
    7.         private void nextPanel(InputAction.CallbackContext context)
    8.         {
    9.             nextPanel();
    10.         }
    11.  
    I can change the keys anytime and my code won't change. Look for samyam tutorials on youtube.