亲宝软件园·资讯

展开

Unity颜色渐变滑动条 Unity实现颜色渐变滑动条

杰弗里斯_ 人气:0
想了解Unity实现颜色渐变滑动条的相关内容吗,杰弗里斯_在本文为您仔细讲解Unity颜色渐变滑动条的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:unity,滑动条,下面大家一起来学习吧。

效果展示

代码

直接挂在UGUI Slider上即可

using UnityEngine;
using UnityEngine.UI;

public class ColorFade : MonoBehaviour
{
    Color[] colors = new Color[]{
      new Color(1, 0, 0),
      new Color(1, 1, 0),
      new Color(0, 1, 0),
      new Color(0, 1, 1),
      new Color(0, 0, 1),
      new Color(1, 0, 1),
      new Color(1, 0, 0)};

    private Slider slider_color;
    private Image img_handle;

    private void Awake()
    {
        slider_color = transform.GetComponent<Slider>();
        slider_color.onValueChanged.AddListener(OnColorSliderValueChanged);
        // handle
        img_handle = slider_color.handleRect.GetComponent<Image>();
        img_handle.color = Color.red;
        // bg
        var hueTex = new Texture2D(colors.Length, 1);
        hueTex.SetPixels(colors);
        hueTex.Apply();
        slider_color.transform.Find("Background").GetComponent<Image>().sprite = 
            Sprite.Create(hueTex, new Rect(Vector2.zero, new Vector2(colors.Length, 1)), Vector2.one * 0.5f);
        // fill
        slider_color.fillRect.GetComponent<Image>().enabled = false;
    }
    private void OnColorSliderValueChanged(float _value)
    {
        img_handle.color = Color.HSVToRGB(_value, 1, 1);
    }
}

加载全部内容

相关教程
猜你喜欢
用户评论