效果:
功能原理介绍:
功能: 通过代码控制Input Field 输入的颜色名称和slider的value 值来改变rgb的值从而改变图片的背景颜色;图片初始颜色为黑色
代码:
using UnityEngine;
using UnityEngine.UI;
public class sliderrgb : MonoBehaviour
{
public Image ugui_image;
public Text ugui_text;
public Slider[] sliders;
public Toggle toggle;
public bool is_UpdateImage;
public float r, g, b;
// Start is called before the first frame update
void Start()
{
r = g = b = 0;
UpdateInfo();
}
// Update is called once per frame
void Update()
{
if (toggle.isOn)
{
UpdateInfo();
}
}
void UpdateInfo()
{
Debug.Log("what!");
ugui_image.color = new Color(r, g, b);
ugui_text.text = "color:";
}
public void InputField_SetColor(InputField inputField)
{
switch (inputField.text)
{
case "Red": case "red":
ugui_image.color = Color.red;break;
case "Green": case "green":
ugui_image.color = Color.green;break;
case "Blue": case "blue":
ugui_image.color = Color.blue;break;
}
}
public void Slider_SetColorR(Slider slider)
{
r = slider.value;
}
public void Slider_SetColorG(Slider slider)
{
g = slider.value;
}
public void Slider_SetColorB(Slider slider)
{
b = slider.value;
}
public void Button_Reset()
{
r = g = b = 1.0f;
ugui_image.color = new Color(r, g, b);
foreach (Slider slider in sliders)
slider.value = 1f;
toggle.isOn = false;
}
}
本项目把代码挂载到画布上,然后依次调整slides的value值来改变rgb的值,进而控制图片颜色,具体操作如下: