Color_调色器

43 篇文章 0 订阅
15 篇文章 0 订阅
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;

public class ColorManager : MonoBehaviour, IDragHandler
{
    //本脚本上的物体的区域
    RectTransform rt;
    //滑动条上的脚本
    private ColorRGB CRGB;
    //面板上的脚本
    private ColorPanel CP;
    //圆环上的脚本
    private ColorCircle CC;
    //滑动条 gameobject
    public Slider sliderCRGB;
    //显示面板 gameobjcet
    public Image colorShow;

    //物体销毁时调用 关闭时调用
    void OnDisable()
    {
        //事件移除
        CC.getPos -= CC_getPos;
    }
    //委托调用
    private void CC_getPos(Vector2 pos)
    {
        Color getColor= CP.GetColorByPosition(pos);
        colorShow.color = getColor;
    }
    
    // Use this for initialization
    void Start () {
        //获取 此脚本对象的 (RectTransform)有效区域
        rt = GetComponent<RectTransform>();
        //获取 子类下的脚本对象
        CRGB = GetComponentInChildren<ColorRGB>();
        CP = GetComponentInChildren<ColorPanel>();
        CC = GetComponentInChildren<ColorCircle>();
        //滑动条 事件
        sliderCRGB.onValueChanged.AddListener(OnCRGBValueChanged);
        //事件添加
        CC.getPos += CC_getPos;
    }
	
    public void OnDrag(PointerEventData eventData)
    {
        //Vector3 wordPos;
        将UGUI的坐标转为世界坐标  
        //if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, eventData.pressEventCamera, out wordPos))
        //    rt.position = wordPos;
    }
    //滑动条颜色 付给面板
    void OnCRGBValueChanged(float value)
    {
        Color endColor=CRGB.GetColorBySliderValue(value);
        CP.SetColorPanel(endColor);
        CC.setShowColor();
    }
}
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ColorPanel : MonoBehaviour, IPointerClickHandler, IDragHandler
{
    //接收面板Rawimage 中的texture
    Texture2D tex2d;
    //面板 Rawimage
    public RawImage ri;
    //面板尺寸
    int TexPixelLength = 256;
    //颜色矩阵
    Color[,] arrayColor;

    RectTransform rt;
    //圆环
    public RectTransform circleRect;
    // Use this for initialization
    void Start()
    {
        arrayColor = new Color[TexPixelLength, TexPixelLength];
        tex2d = new Texture2D(TexPixelLength, TexPixelLength, TextureFormat.RGB24, true);
        ri.texture = tex2d;

        rt = GetComponent<RectTransform>();

        SetColorPanel(Color.red);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Color end = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1.0f);
            SetColorPanel(end);
        }
    }
    //面板颜色添加
    public void SetColorPanel(Color endColor)
    {
        Color[] CalcArray = CalcArrayColor(endColor);
        tex2d.SetPixels(CalcArray);
        tex2d.Apply();
    }


    //面板颜色布局
    Color[] CalcArrayColor(Color endColor)
    {
        Color value = (endColor - Color.white) / (TexPixelLength - 1);
        for (int i = 0; i < TexPixelLength; i++)
        {
            arrayColor[i, TexPixelLength - 1] = Color.white + value * i;
        }
        for (int i = 0; i < TexPixelLength; i++)
        {
            value = (arrayColor[i, TexPixelLength - 1] - Color.black) / (TexPixelLength - 1);
            for (int j = 0; j < TexPixelLength; j++)
            {
                arrayColor[i, j] = Color.black + value * j;
            }
        }
        List<Color> listColor = new List<Color>();
        for (int i = 0; i < TexPixelLength; i++)
        {
            for (int j = 0; j < TexPixelLength; j++)
            {
                listColor.Add(arrayColor[j, i]);
            }
        }

        return listColor.ToArray();
    }


    /// <summary>
    /// 获取颜色by坐标
    /// </summary>
    /// <param name="pos"></param>
    /// <returns></returns>
    public Color GetColorByPosition(Vector2 pos)
    {
        Texture2D tempTex2d = (Texture2D)ri.texture;
        Color getColor = tempTex2d.GetPixel((int)pos.x, (int)pos.y);

        return getColor;
    }
    //点击事件
    public void OnPointerClick(PointerEventData eventData)
    {
        Vector3 wordPos;
        //将UGUI的坐标转为世界坐标  
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, eventData.pressEventCamera, out wordPos))
            circleRect.position = wordPos;
        //调用 自定义事件
        circleRect.GetComponent<ColorCircle>().setShowColor();
    }
    //拖拽事件
    public void OnDrag(PointerEventData eventData)
    {
        Vector3 wordPos;
        //将UGUI的坐标转为世界坐标  
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, eventData.pressEventCamera, out wordPos))
            circleRect.position = wordPos;
        //调用 自定义事件
        circleRect.GetComponent<ColorCircle>().setShowColor();
    }
}
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class ColorCircle : MonoBehaviour, IDragHandler
{
    //委托事件
    public delegate void RetureTextuePosition(Vector2 pos);
    //事件
    public event RetureTextuePosition getPos;
    
    RectTransform rt;
    //活动区域
    public int width = 256;
    public int height = 256;
    // Use this for initialization
    void Start () {
        rt = GetComponent<RectTransform>();
    }
	
	// Update is called once per frame
	void Update () {
	
	}
    //调用事件
    public void OnDrag(PointerEventData eventData)
    {
        Vector3 wordPos;
        //将UGUI的坐标转为世界坐标  
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, eventData.pressEventCamera, out wordPos))
            rt.position = wordPos;
        
        if (rt.anchoredPosition.x <= 0)
            rt.anchoredPosition = new Vector2(0, rt.anchoredPosition.y);
        if (rt.anchoredPosition.y <= 0)
            rt.anchoredPosition = new Vector2(rt.anchoredPosition.x, 0);
        if (rt.anchoredPosition.x >= width-1)
            rt.anchoredPosition = new Vector2(width-1,rt.anchoredPosition.y);
        if (rt.anchoredPosition.y >= height-1)
            rt.anchoredPosition = new Vector2(rt.anchoredPosition.x, height-1);

        getPos(rt.anchoredPosition);
    }
    //滑动条 调用委托
    public void setShowColor()
    {
        getPos(rt.anchoredPosition);
    }

}
标题

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值