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

Help! How do I change default selected buttons on different SetActive() panels ???

Discussion in 'UGUI & TextMesh Pro' started by FBFrog, Sep 7, 2014.

  1. FBFrog

    FBFrog

    Joined:
    Sep 4, 2013
    Posts:
    28
    I have a panel for a point and click adventure style game with a constant nav on the bottom of the screen using the 4.6 ui.



    i have triange on my ps3 as the button to activate the other panel, another navigation menu for items, keys, docs, options, and save/load.. sort of like darksouls i guess. I want my shoulder buttons to nav these buttons.



    this would turn on at the top of my screen, and the 1st nav should turn off.

    I've set the exam button as first selected in the event system. since i can't add 2 or more event systems in a single scene, when i activate the top nav, there is no selected or selectable button. The 1st panel is explicit left / right/up/down transition and it relates to my joypad dpad. and panel 2 is horizontal which should respond to my L1 and R1 buttons.

    A user named Orb helped me get to the point of using SetSelectedGameObject() but I cannot find much documentation on using these new functions... methods?? I'm a UnityScript guy.

    The problem is weird:
    the items button highlights, and on left or right dpad press it wants to go to the next item but snaps back to items. the R1 and L1 make my print() shows the next button as being selected in console but it snaps right back to the items button.

    the menus open and close fine so far. i'm realyl going to need to master this to work in more sub menus. It's too bad i cant just have more than 1 event system.

    Thank you for any help!!!!!!!!!!

    Here is the code:
    Code (JavaScript):
    1. #pragma strict
    2. import UnityEngine.EventSystems;
    3.  
    4. public var main_canvas : GameObject;
    5.  
    6. public var screen_nav_panel : GameObject;
    7. public var inv_nav_panel : GameObject;
    8. public var diag_box : GameObject;
    9. public var diag_text : GameObject;
    10.  
    11. private var inventoryon : boolean;
    12. private var diagon : boolean;
    13. private var itemon : boolean;
    14.  
    15. public var exam_button : GameObject;
    16.  
    17. public var items_button : GameObject;
    18. public var docs_button : GameObject;
    19. public var keys_button : GameObject;
    20. public var options_button : GameObject;
    21. public var saveload_button : GameObject;
    22.  
    23.  
    24. public var event_object : GameObject;
    25. var event_system : UnityEngine.EventSystems.EventSystem;
    26. var exam_button_logic : ExamButton;
    27. var es : EventSystem;
    28.  
    29. function Start ()
    30.     {
    31.    
    32.         // either this is correct
    33.         event_system = event_object.GetComponent(EventSystems.EventSystem);
    34.         //or this
    35.         es = EventSystemManager.currentSystem;
    36.        
    37.        
    38.         main_canvas = this.gameObject;
    39.         exam_button_logic = exam_button.GetComponent(ExamButton);  
    40.  
    41.        
    42.        
    43.         screen_nav_panel.SetActive(true);
    44.         inv_nav_panel.SetActive(false);
    45.         diag_box.SetActive(false);
    46.         diag_text.SetActive(false);
    47.        
    48.         inventoryon = false;
    49.         diagon = false;
    50.         itemon = false;
    51.     }
    52.  
    53. function Update ()
    54.     {
    55.             // refference each nav button.
    56.             // while exam is selected / highlighted
    57.             if(exam_button_logic.exam_selected == true)
    58.             {
    59.                 if(Input.GetButtonDown("Submit"))
    60.                     {
    61.                         diagon = true;
    62.                     }
    63.                 else
    64.                 if(Input.GetButtonDown("Cancel"))
    65.                     {
    66.                         diagon = false;
    67.                     }
    68.             }
    69.             else
    70.             if(exam_button_logic.exam_selected == false)
    71.                 {
    72.                     diagon = false;
    73.                 }  
    74.            
    75.             //////////////////////////////////////////////////////////////
    76.             // turn on inventory      
    77.             if(Input.GetButtonDown("Inventory"))
    78.                     {
    79.                         inventoryon = true;
    80.                         diagon = false;
    81.                        
    82.                         screen_nav_panel.SetActive(false);
    83.                         inv_nav_panel.SetActive(true);
    84.                     }
    85.            
    86.             //////////////////////////////////////////////////////////////////
    87.             // when inventory is meant to be on
    88.             if(inventoryon == true)
    89.                 {  
    90.                     itemon = true;
    91.                     es.SetSelectedGameObject(items_button, null);
    92.                 }
    93.             else
    94.             if(inventoryon == false)
    95.                 {
    96.                     itemon = false;
    97.                     screen_nav_panel.SetActive(true);
    98.                     inv_nav_panel.SetActive(false);
    99.                 }
    100.                
    101.                
    102.                 if(itemon == true)
    103.                     {
    104.                             if(Input.GetButtonDown("L1"))
    105.                                 {
    106.                                     itemon = false;
    107.                                    
    108.                                     es.SetSelectedGameObject(saveload_button, null);
    109.                                     print(event_system.currentSelectedObject);
    110.                                 }
    111.                             else
    112.                             if(Input.GetButtonDown("R1"))
    113.                                 {
    114.                                     itemon = false;
    115.                                     es.SetSelectedGameObject(docs_button, null);
    116.                                     print(event_system.currentSelectedObject);
    117.                                 }
    118.                             else
    119.                             if(Input.GetButtonDown("Cancel"))
    120.                                 {
    121.                                     itemon = false;
    122.                                     es.SetSelectedGameObject(exam_button, null);
    123.                                     inventoryon = false;
    124.                                     print(event_system.currentSelectedObject);
    125.                                 }
    126.                         }
    127.                
    128.            
    129.                    
    130.             // when diag is meant to be on
    131.             if(diagon == true)
    132.                 {
    133.                     diag_box.SetActive(true);
    134.                     diag_text.SetActive(true);
    135.                    
    136.                     if(Input.GetButtonDown("Cancel"))
    137.                         {
    138.                             diagon = false;
    139.                         }
    140.                 }
    141.             else
    142.             if(diagon == false)
    143.                 {
    144.                     diag_box.SetActive(false);
    145.                     diag_text.SetActive(false);
    146.                 }
    147.            
    148.            
    149.     }
    150.  
    151.  
    152.  
    153.  
    154.  
    155.  
    156.  
    157.  
     
  2. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    557
    Whew, that's a lot of code to go through! Forgive me for skimming. =)

    So SetSelectedGameObject() indeed sets the correct GO to be selected. You can then trigger the "move" animations using ExecuteEvents, like so:

    Code (csharp):
    1. AxisEventData axisEventData = newAxisEventData(event_system);
    2. axisEventData.moveDir = MoveDirection.Right; //or Left, Up, Down
    3. ExecuteEvents.Execute(event_system.lastSelectedObject, axisEventData, ExecuteEvents.moveHandler);
    In this case however, you're dealing with multiple inputs and menus at the same time. While you might be able to make due with the standard StandaloneInputModule and EventSystem classes, you might want to consider writing your own classes instead, extending from BaseInputModule and EventSystem respectively.

    This might seem a bit intimidating, but is actually quite managable in my experience! You can find some (unfinished) documentation on all of the new features in your local copy of the Unity docs.

    Hope that helped a bit!
     
  3. FBFrog

    FBFrog

    Joined:
    Sep 4, 2013
    Posts:
    28
    so for javascript:
    Code (JavaScript):
    1.  
    2.  
    3. var axisEventData : AxisEventData;
    4.  
    5. function start()
    6. {
    7. axisEventData = newAxisEventData(event_system);
    8. }
    9.  
    10. function update()
    11. {
    12.        if(on button down R1 ( for example )
    13.         {
    14.        axisEventData.moveDir = MoveDirection.Right;
    15.        ExecuteEvents.Execute(event_system.lastSelectedObject, axisEventData, ExecuteEvents.moveHandler);
    16.          }
    17. }
    18.  
    would this be correct?
     
  4. FBFrog

    FBFrog

    Joined:
    Sep 4, 2013
    Posts:
    28
    not working =(
    I thought of turning the selected object to null, null to clear the selection before switching but that dosent seem to work.

    Code (JavaScript):
    1. // when item is selected
    2.             if(es.currentSelectedObject == items_button)
    3.                 {
    4.                     if(Input.GetButtonDown("L1"))
    5.                         {
    6.                             itemon = false;
    7.                             keyson = false;
    8.                             docson = false;
    9.                             optionson = false;
    10.                             saveloadon = true;
    11.                          
    12.                             es.SetSelectedGameObject(null, null);
    13.                             es.SetSelectedGameObject(saveload_button, null);
    14.                             print(event_system.currentSelectedObject);
    15.                          
    16.                             axisEventData.moveDir = MoveDirection.Left;
    17.                                ExecuteEvents.Execute(event_system.lastSelectedObject, axisEventData, ExecuteEvents.moveHandler);
    18.                         }
    19.                     else
    20.                     if(Input.GetButtonDown("R1"))
    21.                         {
    22.                             itemon = false;
    23.                             keyson = false;
    24.                             docson = true;
    25.                             optionson = false;
    26.                             saveloadon = false;
    27.                          
    28.                             es.SetSelectedGameObject(null, null);
    29.                             es.SetSelectedGameObject(docs_button, null);
    30.                             print(event_system.currentSelectedObject);
    31.  
    32.                             axisEventData.moveDir = MoveDirection.Right;
    33.                                ExecuteEvents.Execute(event_system.lastSelectedObject, axisEventData, ExecuteEvents.moveHandler);
    34.                         }
    35.                     else
    36.                     if(Input.GetButtonDown("Cancel"))
    37.                         {
    38.                             inventoryon = false;
    39.                      
    40.                             itemon = false;
    41.                             keyson = false;
    42.                             docson = false;
    43.                             optionson = false;
    44.                             saveloadon = false;
    45.                      
    46.                             es.SetSelectedGameObject(null, null);
    47.                             es.SetSelectedGameObject(exam_button, null);
    48.                          
    49.                             print(event_system.currentSelectedObject);
    50.                         }
    51.                 }
     
  5. FBFrog

    FBFrog

    Joined:
    Sep 4, 2013
    Posts:
    28
    it wont deselect the items button =( hellllllllllllllllllllllllllllllllllllllllllllllllllllllllllllp
     
  6. FBFrog

    FBFrog

    Joined:
    Sep 4, 2013
    Posts:
    28
    i have the problem solved.
    wow, i had to correct a few nesting issues but this handles that last bit since 90 something people read this and couldn't answer lol.

    Code (JavaScript):
    1.                     // alocate the selections via bool flips
    2.                     if(default_selection == true)
    3.                         {
    4.                             es.SetSelectedGameObject(items_button, null);
    5.                                
    6.                             if(item_button_logic.item_selected == true)
    7.                                 {
    8.                                     itemon = true;
    9.                                 }
    10.                             }
    11.                        
    12.                     // also do it without it having to be defaulted
    13.                     if(inventoryon == true)
    14.                         {
    15.                             if(item_button_logic.item_selected == true)
    16.                                 {
    17.                                     itemon = true;
    18.                                 }
    19.                         }
    20.                     }