简体   繁体   中英

How can I change background color

I want to change my background color with slider value. But I want to select colors from my PickColor() method. By the way, I am learning new UI system. I dont so much know how it works

public Slider bgColorSldr;
public Image background;

private Color32 color;

void PickColor(int colorNo)//My colors
    {
        switch (colorNo)
        {
            case 0:
                color = new Color32(0, 0, 0, 255);
                break;
            case 1:
                color = new Color32(255, 255, 255, 255);
                break;
            case 2:
                color = new Color32(255, 0, 0, 255);
                break;
            case 3:
                color = new Color32(0, 255, 0, 255);
                break;
            case 4:
                color = new Color32(0, 0 , 255, 255);
                break;
            case 5:
                color = new Color32(255, 255, 0, 255);
                break;
            case 6:
                color = new Color32(0, 255, 255, 255);
                break;
            case 7:
                color = new Color32(255, 0, 255, 255);
                break;
            case 8:
                color = new Color32(192, 192, 192, 255);
                break;
            case 9:
                color = new Color32(128, 128, 128, 255);
                break;
        }
    }

I dont understand your question fully, but I try to help you.

  1. Set your Slider like this. Max value is 10, because you have 10 color. And choose Whole Numbers it is important, 图片

  2. You have public ChangeColor method like this. Put this method any your object and add it to your slider's On Value Changed() method.

     public void ChangeColor() { PickColor ((int)bgColorSldr.value); background.color = color; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM