Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
1
Question by DustyLv · Feb 15, 2016 at 11:35 AM · uidisabledropdownoption

Disable a single Dropdown option.

Hello!

I have a player setup scene where multiple players can choose their profession, they are listed in a Dropdown. When a player chooses a profession, it becomes unavailable for other players.

Right now I have a list that holds the profession name and its availability. The availability gets changed correctly and I was thinking of using that, but I got stumped at disabling the profession option in the Dropdown. Also, I'm looping through the list using a for loop, and checking the indexes to match the options for disabling.

Is it even possible to disable a single option? Seems kinda weird that that kind of a functionality is not present or easily accessible. Or is there possibly a hacky way of doing that? I mean, I can check for a match and display a message that a player should change his profession, but that's pretty much last resort.

Comment
Add comment · Show 3
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
avatar image brunocoimbra · Feb 15, 2016 at 04:09 PM 0
Share

Yeah, there is, I remember when I did a system like that...

Each Item in the Dropdown menu has an Toggle script attached, which has a variable called "Interactable". Just turn it to false.

avatar image DustyLv brunocoimbra · Feb 15, 2016 at 11:46 PM 0
Share

Yeah, I noticed, but it's there only when the dropdown is open. When I collapse it or select an option, the reference to that Toggle disappears and I can't really do anything with that, as far as my knowledge goes.

avatar image brunocoimbra DustyLv · Feb 17, 2016 at 08:24 PM 0
Share

Just store the options index (starting from 1, not zero) and everytime that you are going to open your dropdown menu, call that method:

     [SerializeField] private RectTransform dropdown$$anonymous$$enu;
 
     public void SetItensDisabled(int[] indexArray)
     {
         var content = dropdown$$anonymous$$enu.GetChild(3).GetChild(0).GetChild(0);
 
         foreach (var index in indexArray)
         {
             content.GetChild(index).GetComponent<Toggle>().interactable = false;
         }
     }

We start at index "1" because that the first child of your content menu is just a copy of the GameObject "item" from the template, so it should remain disabled anyway.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MarkKreitler · Nov 19, 2017 at 09:33 PM

@Malkyne's reply is complete, but here's a little more detail:

To toggle individual Dropdown items' 'active' state:

1) Find the 'item' template in the hierarchy beneath your Dropdown a) You will probably find it under Dropdown -> Template -> Viewport -> Content -> Item b) This Item should have a "Toggle" script on it by default.

2) Add a script to this item. a) The script should get the item's Toggle component b) The script should set the item's 'isInteractable' bool in the Start() method

Example:


 using UnityEngine;
 using UnityEngine.UI;
 
 namespace DropdownItemExample {
 
     public class InitDropdownItem : MonoBehaviour {
         [Serializable]
             private MyGameController gameController = null;
 
         protected void Start() {
             Toggle toggle = gameObject.GetComponent<Toggle>();
 
             if (toggle != null && gameController != null) {
                         toggle.interactable = gameController.IsItemSelectable(this);
                     }
         }
     }

}


Here, MyGameController is the object that manages your game. It could be something else. You could also have the 'item' set it's own toggle based on internal variables (like the string or sprite being displayed on the item).

Here's the key to remember: The Dropdown will create a new set of items every time the user opens the list. This means the Items' Start() method will get called every time the list opens, and that's the only time the item instances exist. That's why they have to set their toggles in the Start() method.

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
avatar image
0

Answer by Malkyne · Jun 09, 2016 at 10:26 PM

You can modify the prototype Item inside the Dropdown Template in any way you feel is necessary. This includes adding scripts.

In your Start() function, you can then call out elsewhere, to see whether this item is allowed to be interacted with. When Start is running, your transform.GetSiblingIndex() should give you a correct index of this item.

So, you might write something that looks like this:

 public class CharacterOptionItem : MonoBehaviour
 {
     [SerializeField]
     private ScreenManager _screenManager;

     private void Start()
     {
         GetComponent<Toggle>().interactable =
             _screenManager.IsItemEnabled(transform.GetSiblingIndex() - 1);
     }
 }

Drop your script on the prototype item, and then, make sure to hook up your external authority (in this example, _screenManager), and it will be hooked up for all the child items.

Comment
Add comment · Show 1 · 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
avatar image JacobPlaysGames · Oct 28, 2017 at 06:31 PM 0
Share

Aaand what the heck would be in your Screen$$anonymous$$anager class? I tried this, and I can't figure out your IsItemEnabled function.

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.

Follow this Question

Answers Answers and Comments

58 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

Related Questions

How to stop a countdown timer and disable/stop everything in the scene? 1 Answer

Hide/Disable a Dropdown 1 Answer

Reuseable Dropdown 0 Answers

Content Size Fitters wont work DropDown Menus? 0 Answers

How to change the Font Asset from a Dropdown List item? 1 Answer


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