色环选色

项目需求

项目需求,需要一个颜色环,颜色环上有滑块,拖动滑块,滑块在色换上移动,并能获取到色换上的颜色,并赋值给三维物体。首先获取鼠标坐标,并转化为局部坐标,然后根据局部坐标计算滑块在色环上的坐标,移动滑块位置,并获取坐标值对应的color。如下图所示:
在这里插入图片描述

2.解决方案

1、拖动鼠标时,获取鼠标拖动坐标,根据圆环的公式,计算滑块的位置。
2、通过滑块位置,获取此时color。

注意:1)色环图标必须为正方形 2)坐标计算得到的鼠标点坐标对应的坐标原点为image的中心位置 3)下述代码未考虑鼠标坐标为(0,0)时的情况,因为通过色环中间的透明image避免了此情况,如下图所示。

3.完整代码

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ColorController : MonoBehaviour,IPointerDownHandler,IDragHandler,IPointerUpHandler
{
    public GameObject button;
    public Material carMaterial;
    public GameObject[] textMode;
    public GameObject textInfo;

    float aSquare = 35263.45f;
    float r = 187.7856491f;

    Vector2 posOri;
    Color colorOri;
    Texture2D t2d;
    int currentTextMode = 3;

    public void OnDrag(PointerEventData eventData)
    {
        Vector2 anchorPosNew = CalculateButtonPos(eventData.position);

        button.GetComponent<RectTransform>().anchoredPosition = anchorPosNew;

        carMaterial.SetColor("_Color", CalculateColor(anchorPosNew));
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        //Vector3 posLocal = transform.InverseTransformPoint(eventData.position);

        Vector2 anchorPosNew = CalculateButtonPos(eventData.position);

        button.GetComponent<RectTransform>().anchoredPosition = anchorPosNew;

        carMaterial.SetColor("_Color", CalculateColor(anchorPosNew));

        textInfo.SetActive(true);
        //Debug.Log(posLocal);
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        textInfo.SetActive(false);
    }

    public void ResetColor()
    {
        button.GetComponent<RectTransform>().anchoredPosition = posOri;

        carMaterial.SetColor("_Color", colorOri);

        SetTextMode(3, colorOri);
    }

    Vector2 CalculateButtonPos(Vector3 posWorld)
    {
        Vector3 posLocal = transform.InverseTransformPoint(posWorld);
        float k = posLocal.y / posLocal.x;
        float pstiveRoot = Mathf.Sqrt(aSquare / (1 + k * k));
        float x = posLocal.x > 0 ? pstiveRoot : -pstiveRoot;
        float y = x * k;

        return new Vector2(x, y);
    }

    Color CalculateColor(Vector2 position)
    {
        Color color = Color.white;

        int xPos = (int)((float)t2d.width / 2 + position.x);
        int yPos = (int)((float)t2d.height / 2 + position.y);

        color = t2d.GetPixel(xPos, yPos);

        if (position.x <= r && position.x >= 0 &&
            position.y <= r && position.y >= 0)//第一象限
        {
            SetTextMode(0, color);
        }

        if (position.x <= 0 && position.x >= -r &&
            position.y <= r && position.y >= 0)//第二象限
        {
            SetTextMode(1, color);
        }

        if (position.x <= 0 && position.x >= -r &&
            position.y <= 0 && position.y >= -r)//第三象限
        {
            SetTextMode(2, color);
        }

        if (position.x <= r && position.x >= 0 &&
             position.y <= 0 && position.y >= -r)//第四象限
        {
            SetTextMode(3,color);
        }

        return color;
    }

    void SetTextMode(int index, Color color)
    {
        textMode[currentTextMode].SetActive(false);
        textMode[index].SetActive(true);
        currentTextMode = index;

        string text =(int)(color.r * 255) + "/" + (int)(color.g * 255) + "/" + (int)(color.b * 255) + "/" + (int)(color.a * 255);
        textMode[index].transform.GetComponentInChildren<Text>().text = text;
    }

    // Use this for initialization
    void Start ()
    {
        t2d = GetComponent<Image>().sprite.texture;
        posOri = button.GetComponent<RectTransform>().anchoredPosition;
        colorOri = CalculateColor(posOri);
        carMaterial.SetColor("_Color", colorOri);
        SetTextMode(3, colorOri);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值