Question by
DeathProfessor · Jan 24, 2017 at 01:35 PM ·
dropdown
How to add color to dropdown item in runtime?
I want to make dropdown for choosing player color. But I need to add colors as items in runtime.
Dropdown.OptionData wants Sprite as parameter, but I have no sprite, just Color.
How can I solve this problem? Thanks!
Comment
Best Answer
Answer by DeathProfessor · Jan 25, 2017 at 12:36 PM
You should create sprite from color. Something like this:
var texture = new Texture2D(1,1); // creating texture with 1 pixel
texture.SetPixel(0, 0, color); // setting to this pixel some color
texture.Apply(); //applying texture. necessarily
var item = new Dropdown.OptionData(Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0))); // creating dropdown item and converting texture to sprite
dropdown.options.Add(item); // adding this item to dropdown options