• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Redsam121 · Nov 18, 2022 at 04:33 PM · uiinstantiatebuttonlistnavigation

Expandable List UI button navigation

I'm creating an item inventory UI for an RPG game I'm making. The lists are expandable using instantiated prefab slots parented to an empty gameobject with a vertical gridlayout. Each slot has a button, so the player can navigate through the list. The navigation is controlled using a game controller.

     void LoadSlots()
     {
         if (inventory == null) return;
         if (consumables.Count != 0)
         {
             ClearList(consumablesList.transform);
             for (int i = 0; i < consumables.Count; i++)
             {
                 var newSlot = Instantiate(Resources.Load<Transform>("ItemSlot"));
                 newSlot.Find("Icon").GetComponent<Image>().sprite = consumables[i].icon;
                 newSlot.Find("Name").GetComponent<Text>().text = consumables[i].name;
                 newSlot.transform.SetParent(consumablesList.transform, false);
             }
         }
     }

Each list is opened using toggles on the top, when toggled on. The event system sets the first c$$anonymous$$ld object as the navigation start point.

     public void OpenConsumables()
     {
         consumablesList.SetActive(true);
         Transform[] c$$anonymous$$ldren = consumablesList.GetComponentsInC$$anonymous$$ldren<Transform>();
         Debug.Log(c$$anonymous$$ldren[1].name);
         EventSystem.current.SetSelectedGameObject(c$$anonymous$$ldren[1].gameObject);
     }

The navigation starts from the top and to the bottom. When the bottom is $$anonymous$$ghlighted, it won't go any further, same for the top.

However, I'm having issues setting up a dynamic button navigation for the lists. The automatic navigation setup just isn't working. Is there a better way to create a navigation for an expandable list.

Visual representation of the UI: alt text

capture.png (262.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Redsam121 · Nov 19, 2022 at 02:51 AM

I wrote a code that is stored inside the prefabs. It uses get c$$anonymous$$ld index to find all adjacent game object in the $$anonymous$$erachy and sets up the navigation.

 using UnityEngine;
 using UnityEngine.UI;
 
 public class ButtonNavigation : MonoBehaviour
 {
     Button button;
     void Start()
     {
         button= GetComponent<Button>();
     }
     void Update()
     {
         Top();
         Bottom();
     }
     void Top()
     { 
         int index = gameObject.transform.GetSiblingIndex();
         if (index == 0)
         {
             Navigation navB = button.navigation;
             navB.selectOnUp = transform.parent.GetC$$anonymous$$ld(transform.parent.c$$anonymous$$ldCount - 1).GetComponent<Button>();
             button.navigation = navB;
             return;
         }
         if (transform.parent == null) return;
         if (transform.parent.c$$anonymous$$ldCount <= index - 1) return;
         Navigation nav = button.navigation;
         nav.selectOnUp = transform.parent.GetC$$anonymous$$ld(index - 1).GetComponent<Button>();
         button.navigation = nav;
     }
     void Bottom()
     { 
         int index = gameObject.transform.GetSiblingIndex();
         if (transform.parent == null) return;
         if (transform.parent.c$$anonymous$$ldCount <= index + 1)
         {
             Navigation navT = button.navigation;
             navT.selectOnDown = transform.parent.GetC$$anonymous$$ld(0).GetComponent<Button>();
             button.navigation = navT;
             return;
         }
         Navigation nav = button.navigation;
         nav.selectOnDown = transform.parent.GetC$$anonymous$$ld(index + 1).GetComponent<Button>();
         button.navigation = nav;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

322 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Buttons in one panel affecting another. 0 Answers

How to Instantiate a prefab button without an object reference error? 0 Answers

calling 3D model with button using INSTANTIATE I did successfully with keyboard now I am struggling to call from UI Button 0 Answers

Unity UI turn keyboard navigation off 2 Answers

UI Buttons Trigger displaced after build. Help Please 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges