Unity小功能记录(一)------ 色环与拾取颜色(圆形Slider)

最近有个需求,一个圆环图片要移动拾取色环上的颜色,具体效果如:

                                        

圆环slider移动参考:https://blog.csdn.net/shenmifangke/article/details/53582505

但是该博主的有一点小BUG,到边界(0度与360度转换)会飘一下,下文放出的资源链接已经修改了


主要代码如下:

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


public class ArcSlider : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
{
    public Texture2D heatMapImage;
    public Color32 outputColor;
    public Image handleButton;
    float circleRadius = 0.0f;
    bool isPointerDown = false;
    public Text showColor;
    //忽略圈内的交互
    public float ignoreInTouchRadiusHandleOffset = 10;
    Vector3 handleButtonLocation;
    [Tooltip("初始角度到终止角度")]
    public float firstAngle = 30;
    public float secondAngle = 150;
    float tempAngle = 30;//用来缓动
    public float width;
    public float height;
    public void Start()
    {
        circleRadius = Mathf.Sqrt(Mathf.Pow(handleButton.GetComponent<RectTransform>().localPosition.x, 2) + Mathf.Pow(handleButton.GetComponent<RectTransform>().localPosition.y, 2));
        ignoreInTouchRadiusHandleOffset = circleRadius - ignoreInTouchRadiusHandleOffset;
        handleButtonLocation = handleButton.GetComponent<RectTransform>().localPosition;
        width = GetComponent<RectTransform>().rect.width;
        height = GetComponent<RectTransform>().rect.height;
    }
    //重置初始位置
    public void ReSet()
    {
        handleButton.GetComponent<RectTransform>().localPosition = handleButtonLocation;
    }
    public void OnPointerEnter( PointerEventData eventData )
	{
		StartCoroutine( "TrackPointer" );
	}
	//如果需要移动到外部时仍然有效可以去掉这里的
	public void OnPointerExit( PointerEventData eventData )
	{
		StopCoroutine( "TrackPointer" ); //停止
	}
	public void OnPointerDown(PointerEventData eventData)
	{
		isPointerDown= true;
	}
	public void OnPointerUp(PointerEventData eventData)
	{
		isPointerDown= false;
	}
	IEnumerator TrackPointer()
	{
		var ray = GetComponentInParent<GraphicRaycaster>();
		var input = FindObjectOfType<StandaloneInputModule>();


		var text = GetComponentInChildren<Text>();
		
		if( ray != null && input != null )
		{
			while( Application.isPlaying )
			{
                //这个是左侧的
                if (isPointerDown)
                {
                    Vector2 localPos;
                    //获取鼠标当前位置out里赋值
                    RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform, Input.mousePosition, ray.eventCamera, out localPos);
                    showColor.text = "SelectColor:" + GetHeatMapColor((int)(localPos.x+ width/2), (int)(localPos.y+height/2));
                    localPos.x = -localPos.x;
                    //半径
                    float mouseRadius = Mathf.Sqrt(localPos.x * localPos.x + localPos.y * localPos.y);
                    //阻止圆内部点击的响应,只允许在一个圆环上进行响应
                    if (mouseRadius > ignoreInTouchRadiusHandleOffset)
                    {
                        //0-180  -180-0偏移后的角度 从第一象限校正到0-360
                        float angle = (Mathf.Atan2(localPos.y, localPos.x)) * Mathf.Rad2Deg;
                        if ((angle < 0 && tempAngle > 0))
                        {
                            tempAngle = 360 + angle;
                        }
                        if((angle > 0 && tempAngle > 330))
                        {
                            tempAngle = angle;
                        }
                        if (angle < 0) angle = 360 + angle;;


                        if (angle < firstAngle) angle = firstAngle;
                        if (angle > secondAngle) angle = secondAngle;


                        angle = (tempAngle + angle) / 2f;
                        tempAngle = angle;
                        //改变小圆的位置
                        handleButton.GetComponent<RectTransform>().localPosition = new Vector3(Mathf.Cos(-angle / Mathf.Rad2Deg + 45.0f * Mathf.PI) * circleRadius, Mathf.Sin(-angle / Mathf.Rad2Deg + 45.0f * Mathf.PI) * circleRadius, 0);
                    }
                }
				yield return 0;
			}        
		}   
	}
    public Color32 GetHeatMapColor(int x, int y)
    {
        outputColor = heatMapImage.GetPixel(x, y);
        return outColor;
    }
}
Demo下载地址:https://download.csdn.net/download/dengshunhao/10490227
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千喜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值