• 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 /
avatar image
Question by CookieSteve · Mar 01, 2019 at 01:58 AM · uidropdownlistenercoloureventlistener

How can I change the colour of Dropdown List items in runtime?

I need to be able to change the colour of individual dropdown list items. I've noticed that t$$anonymous$$s can be done in the editor by directly modifying the value of the item's colours w$$anonymous$$le the dropdown list is open:

alt text

However, t$$anonymous$$s only works for as long as the dropdown list is open. If you close and re-open the list, the colours will reset to default. Furthermore, since my dropdown lists are created during runtime, they need to have their colours assigned during runtime too.

One way I thought I could accomplish t$$anonymous$$s would be to have a listener that would trigger whenever the dropdown list was selected, whereupon I could just find the relevant list items and change them that way. However, the only listener I could find that related to dropdowns is the 'OnValueChanged' one, w$$anonymous$$ch only triggers once one of the list items has been selected (by w$$anonymous$$ch point it's too late).

Is there any way to either add a listener to the dropdown list that activates whenever it's opened, or to just edit the colour of individual dropdown options upon creation? Any help would be appreciated!

Comment

People who like this

0 Show 0
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

5 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by WarmedxMints · Mar 01, 2019 at 04:17 AM

The best way really would be to make your own dropdown. Fortunately, Unity is kind enough to share the UI code. You can find the dropdown code here link text

Use that to make your own dropdown class and modify the Show() method,

Comment
CookieSteve
Ziplock9000
Yleisnero

People who like this

3 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 CookieSteve · Mar 01, 2019 at 12:09 PM 0
Share

Ah, good to know! Thank you!

avatar image

Answer by donkey0t · Sep 21, 2020 at 11:21 AM

I extended the existing control rather than re-writing it. It's just a different approach. https://youtu.be/N0afV6h2L6o

Comment
tjbarrott

People who like this

1 Show 0 · 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

Answer by Number11 · Nov 05, 2020 at 11:35 AM

You can ac$$anonymous$$eve t$$anonymous$$s by simply adding a component to your template's item. Under the GameObject named 'Template', you wil find an GameObject named 'Item'. It is duplicated each time an item of the dropdown list is created.

You can add somet$$anonymous$$ng like the code below to the 'Item' GameObject. In the editor, simply drop the Image component from the 'Item Background' GameObject to the field Image (or find it via code, depending on your needs).

Then, each time the template's item will be duplicated, the ColorChanger component will be created and started. T$$anonymous$$s is just to give the idea, explore and customize to your needs ;) (here I did not do the filtering to show how to $$anonymous$$ghlight a single item, but t$$anonymous$$s should be easy)

 public class ColorChanger : MonoBehaviour
 {
     public Image image;
 
     // Start is called before the first frame update
     void Start()
     {
         image.color = Color.red;
     }    
  }

Comment
emredesu

People who like this

1 Show 0 · 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

Answer by yeshiazev · Oct 10, 2019 at 05:25 AM

I believe my solution will help. If I understand correctly, you want to set a background color for the selected item in the drop down? I don't have the code to set t$$anonymous$$s all up, but t$$anonymous$$s is how I did it through the Inspector.

Under Viewport-Content-Item: I use the "Item Checkmark" as a way to color the row. Set the Item Checkmark's Rect Transform to Strech both directions. Left/Right/Top?Bottom to 0 Set the Item Checkmark's Color to whatever color you like, then set the Alpha to allow the text to be seen through the Checkmark's color.

T$$anonymous$$s should $$anonymous$$ghlight the selected item using the Checkmark Item, w$$anonymous$$ch the DropDown sets for you. Hope t$$anonymous$$s helps.

Comment

People who like this

0 Show 0 · 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

Answer by Yleisnero · Oct 24, 2019 at 10:16 AM

As @WarmedxMints said, it's probably the best to make your own dropdown. I've created one, w$$anonymous$$ch fits my usage. Maybe it helps: https://github.com/Yleisnero/unitycolordropdown

Comment

People who like this

0 Show 0 · 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

172 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

Related Questions

How to get a dropdown menu to change the colour of a reticule on another scene 0 Answers

DropDown menu(add item from input field): Object reference not set to instance of an object C# 1 Answer

How I can set value of Dropdown? 1 Answer

How do I make a colour editor UI? 1 Answer

Dropdown disable blocker 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