summaryrefslogtreecommitdiff
path: root/Assets/Samples/Demo/UIEffect_Demo_ColorControl.cs
blob: 9c64aa1919b1768f5bfc9f06e474bccf7b97e977 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class UIEffect_Demo_ColorControl : MonoBehaviour
{
    [SerializeField] private Color m_Color;
    [SerializeField] private ColorEvent m_ColorEvent = new ColorEvent();

    [System.Serializable]
    public class ColorEvent : UnityEvent<Color>
    {
    }

    private void Start()
    {
        var sliders = GetComponentsInChildren<Slider>();
        for (var i = 0; i < sliders.Length; i++)
        {
            var channel = i;
            if (channel == 0)
                sliders[channel].value = m_Color.r;
            else if (channel == 1)
                sliders[channel].value = m_Color.g;
            else if (channel == 2)
                sliders[channel].value = m_Color.b;
            else
                sliders[channel].value = m_Color.a;
            sliders[i].onValueChanged.AddListener(value => ChangeColor(channel, value));
        }
    }

    private void ChangeColor(int channel, float value)
    {
        var old = m_Color;
        if (channel == 0)
            m_Color.r = value;
        else if (channel == 1)
            m_Color.g = value;
        else if (channel == 2)
            m_Color.b = value;
        else
            m_Color.a = value;

        if (old != m_Color)
            m_ColorEvent.Invoke(m_Color);
    }
}