Thursday, April 30, 2015

Unity UI: Easy Tabs (no scripting)

Today I would like to demonstrate a method I have devised to create Tabs with Unity's UI tools (v4.6 and up).  This implementation behaves similar to tabs seen in web browsers and numerous other applications.


For this tutorial you will need Tab sprites.  Below you will find the atlas I made (in GIMP) for my project.  This atlas is meant to be sliced into 3 64x64 sprites with all border values at 18 (the Unity manual has more information on canvas and sprite settings).  If you want to use this atlas right click and save as.

Tab Sprites Atlas
From left to right these sprites will be referred to as "Tab_Window", "Tab_Inactive" and "Tab_Active".

Step 1:
Add a new UI image (UI>Image) to your canvas and rename it (TabPanel), set the source image to Tab_Window and adjust the rect size and position to your liking.

Next, create an empty child to contain the tabs (TabContainer).
Step 1

Step 2:
Add a Toggle Group (Add Component>UI) and a Horizontal Layout Group (Add Component>Layout) to the TabContainer.

The Toggle Group will ensure that only one tab is active at a time, and the Horizontal Layout Group will control the size and spacing of tabs.

Set the TabContainer's Anchor to top-stretch (you can wait to set it's position until tab images are added).

Next, add a UI Toggle to the TabContainer.
Step 2

Step 3:
The Toggle will be your first Tab, (renamed "TabOne").

Select the Group field and choose the TabContainer.
Step 3

Step 4:
Change the Background's Source Image to Tab_Inactive, set it's Anchor to stretch and set it's dimension values to zero.

Rename Checkmark to "Active", change it's Source Image to Tab_Active, set it's Anchor to stretch and set it's dimension values to zero.

Center the Label and change it's text to the tab's name.

Next, adjust the TabContainer's position so that the bottom of the new tab aligns with the top of the TabPanel.
Step 4

Step 5:
Duplicate the tab (Ctrl+D) to add more tabs.

Adjust The TabConatiner's Horizontal Layout Group spacing to you liking.

Uncheck "Is On" for all tabs but the first.

Rename and change the Label text of your tabs.
Step 5

Step 6:
Create content containers for your tabs (empty game objects).

Add UI elements to the containers (text, buttons, images, etc).

Add a new action (+) to the On Value Changed (Boolean) listener on each tab.

Set the corresponding content container as the target object for each action.

Disable all the content containers except the one corresponding to the active tab.

Choose GameObject>(Dynamic Bool) Set Active as the function.
Step 6

That's it! Each tab will now activate it's content when selected, and deactivate it's content when a different tab is selected.


28 comments:

  1. Thanks for this tutorial! :D very helpful

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. Awesome tutorial. It was exactly what I was looking for. Thanks a lot!

    ReplyDelete
  4. How do you "Disable all the content containers except the one corresponding to the active tab."?

    ReplyDelete
    Replies
    1. In the hierarchy tab(left) click on the object you want to disable, then in the inspector tab(right) at the very top look for the checkbox to the left of the object name(content2,3,4,etc.), uncheck that box to disable the object. Repeat this process for all content containers except the one you want to leave active.

      Delete
    2. Thank You! This is a great tutorial!

      Delete
  5. that looks promising. but I can't select the layout group of the tab container at group of the toggle. Unity 2018.14f1.

    ReplyDelete
    Replies
    1. Ok, I forgot the Toggle Group component.

      Delete
  6. This is awesome, thanks!

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Hi, thank you for the tutorial! I have a few questions about the setup.
    Here is what my current tab system looks like: https://imgur.com/TwasIu0

    1. How do you make the TabPanel and tabs look so nice and neat? When I stretch mine, they become pixelated and blurry. I sliced the sprites you provided into 64 x 64 tabs. I'm not sure what the problem is.

    2. How do you adjust the Horizontal Layout Group so the tabs are next to each other? Here are my Horizontal Layout Group settings https://imgur.com/GMn85bt (default settings). I don't know why there's a big space between mine. https://imgur.com/TwasIu0

    Thanks!

    ReplyDelete
    Replies
    1. 1) If you did not also set the borders to 18, then each tab slice would not end up further divided into 9 sprite slices. 9-slicing the tabs is what prevents the pixelation - if you don't 9-slice it by setting borders, Unity just stretches the tab image, which is why it pixelates.

      2) You can mess with the Horizontal Layout Group alignment/spacing settings on the Tab Container if you like. Unchecking Child Force Expand Width will move them closer together and shift them left, if you have Top Left alignment. Another reason his could be closer together is if his TabPanel and TabContainer have a lower width than yours.

      Delete
  9. Note that in Unity 2018.4.13f1 checking or unchecking "Is On" on a Tab will not change the tab image between Tab_Active and Tab_Inactive until you also deactivate and re-activate the tab gameObject while you are still on Step 5.

    It took me a couple of minutes to figure out why the images weren't changing and I thought this might help some folks.

    ReplyDelete
    Replies
    1. Could you please elaborate a bit more on this? I am a real noob and I am trying to figure out this whole tab thing but I seem to be running into the same problem! Many thanks in advance.

      Delete
  10. in the toggle group, there's no tabcontainer for me :( im stuck at that step

    ReplyDelete
  11. Simple and efficient - thankyou :D

    ReplyDelete
  12. Simple, genius, perfection. I can't believe this still works 6 years later, great technique... one less thing to have to manage in code

    ReplyDelete
  13. Saved me a TON of time in development! Cannot recommend enough.

    ReplyDelete
  14. If you owe a house, you probably know that maintaining a neat and clean appearance on the exterior of your home is very important. It creates a long-lasting impression. The most crucial stage in developing an appealing look is giving the house painting a coat of paint.

    Therefore, it is best to choose experts. If you are living in Canberra and looking for domestics painter in Canberra, you should reach out to Act House Landscaping. They have a team of talented and knowledgeable professionals who can handle everything without any hassle.

    ReplyDelete
  15. Are you planning on getting good-quality maintenance for your leather lounge? It is a great idea to take your leather items for deep cleaning once in a while to improve their functionality and appearance. De Vere is specialized in delivering the best leather cleaning services that will rejuvenate your leather items inside out and enhance your experience. To book a service, call us at 0754967455.

    ReplyDelete
  16. A pediatric urgent care clinic is highly reliable and beneficial to ensure immediate and effective assistance for your kid’s health issues. If you are unsure about the best pediatric urgent care in Palm beach, then Vital Urgent is the best option for you. With high-quality urgent care services rendered by expert healthcare professionals, your child is in safe hands at Vital Urgent. Additionally, their optimum services provide maximum comfort and convenience to the child, thereby keeping them calm throughout the process.

    ReplyDelete
  17. Okidas is delighted to provide electric scooters in USA and easy-to-use electric bikes and scooters of excellent quality. Okidas has something for everybody, whether you're looking for a small scooter that's easy to store or a fast-electric scooter for lengthy commutes. Okidas has plenty of electric scooters to pick from, so you can be sure you'll find the right one for your requirements and budget. With Okidas, you can indulge in the excitement of electric scooters without paying much. Checkout more details on okidas.com

    ReplyDelete