最近有个需求,一个圆环图片要移动拾取色环上的颜色,具体效果如:
主要代码如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class PointMove : MonoBehaviour { public RectTransform rect; public Texture2D heatMapImage; public Color32 outputColor; public Text text; // Use this for initialization void Start () { rect = GetComponent<RectTransform>(); } // Update is called once per frame void Update () { } public void SetPos(Vector2 pos) { float Y =1; float X = 1; if(pos.x >180 && pos.x < 550) { X = pos.x - 375; if (pos.y > 667) { //上半部分 Y = 1; } else { //下半部分 Y = -1; } if (X >= 220) { X = 220; } if (X <= -220) { X = -220; } Y = Y * Mathf.Sqrt(220 * 220 - X * X); if (float.IsNaN(Y)) { Y = pos.y - 667f; } } else { if (pos.x > 375) { //上半部分 X = 1; } else { //下半部分 X = -1; } Y = pos.y - 667f; X = X * Mathf.Sqrt(220 * 220 - Y * Y); if (float.IsNaN(X)) { X = pos.x - 375; } } rect.anchoredPosition = new Vector2(X, Y); outputColor = GetHeatMapColor((int)(X+220),(int)(Y+220)); text.text ="Color:" + outputColor.ToString(); Debug.Log(outputColor.ToString()); } public Color32 GetHeatMapColor(int x, int y) { outputColor = heatMapImage.GetPixel(x, y); Color32 outColor = new Color32((byte)(outputColor.r * 255), (byte)(outputColor.g * 255), (byte)(outputColor.b * 255), 255); return outColor; } }Demo下载地址:https://download.csdn.net/download/dengshunhao/10427987