色环选色

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

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

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);
    }
}

 

posted @ 2018-10-27 13:49  81192  阅读(341)  评论(0编辑  收藏  举报