用UI制作半椭圆的Slider效果

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using UnityEngine.EventSystems;  
  4. using UnityEngine.UI;  
  5.   
  6. public class ArcSlider : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler  
  7. {  
  8.     public Image handleButton;  
  9.     float circleRadius = 0.0f;  
  10.   
  11.     bool isPointerDown = false;  
  12.     public Image baseCircle;  
  13.   
  14.     //忽略圈内的交互  
  15.     public float ignoreInTouchRadiusHandleOffset = 10;  
  16.   
  17.     Vector3 handleButtonLocation;  
  18.   
  19.     [Tooltip("初始角度到终止角度")]  
  20.     public float firstAngle = 30;  
  21.     public float secondAngle = 150;  
  22.   
  23.     float tempAngle = 30;//用来缓动  
  24.     public void Start()  
  25.     {  
  26.         circleRadius = Mathf.Sqrt(Mathf.Pow(handleButton.GetComponent<RectTransform>().localPosition.x, 2) + Mathf.Pow(handleButton.GetComponent<RectTransform>().localPosition.y, 2));  
  27.         ignoreInTouchRadiusHandleOffset = circleRadius - ignoreInTouchRadiusHandleOffset;  
  28.   
  29.         handleButtonLocation = handleButton.GetComponent<RectTransform>().localPosition;  
  30.     }  
  31.     public void Update()  
  32.     {  
  33.         //用来重置  
  34.         if (Input.GetKeyDown(KeyCode.R))  
  35.         {  
  36.             ReSet();  
  37.         }  
  38.     }  
  39.     public void ReSet()  
  40.     {  
  41.         handleButton.GetComponent<RectTransform>().localPosition = handleButtonLocation;  
  42.     }  
  43.     public void OnPointerEnter( PointerEventData eventData )  
  44.     {  
  45.         StartCoroutine( "TrackPointer" );  
  46.     }  
  47.       
  48.     //如果需要移动到外部时仍然有效可以去掉这里的  
  49.     public void OnPointerExit( PointerEventData eventData )  
  50.     {  
  51.         StopCoroutine( "TrackPointer" );  
  52.     }  
  53.   
  54.     public void OnPointerDown(PointerEventData eventData)  
  55.     {  
  56.         isPointerDown= true;  
  57.     }  
  58.   
  59.     public void OnPointerUp(PointerEventData eventData)  
  60.     {  
  61.         isPointerDown= false;  
  62.     }  
  63.       
  64.     IEnumerator TrackPointer()  
  65.     {  
  66.         var ray = GetComponentInParent<GraphicRaycaster>();  
  67.         var input = FindObjectOfType<StandaloneInputModule>();  
  68.   
  69.         var text = GetComponentInChildren<Text>();  
  70.           
  71.         if( ray !=  && input !=  )  
  72.         {  
  73.             while( Application.isPlaying )  
  74.             {                      
  75.                 //这个是左侧的  
  76.                 if (isPointerDown)  
  77.                 {  
  78.                     Vector2 localPos;  
  79.                     //获取鼠标当前位置out里赋值  
  80.                     RectTransformUtility.ScreenPointToLocalPointInRectangle( transform as RectTransform, Input.mousePosition, ray.eventCamera, out localPos );  
  81.   
  82.                     localPos.x = -localPos.x;  
  83.   
  84.                     //半径  
  85.                     float mouseRadius = Mathf.Sqrt(localPos.x*localPos.x+localPos.y*localPos.y);  
  86.   
  87.                     //阻止圆内部点击的响应,只允许在一个圆环上进行响应  
  88.                     if (mouseRadius > ignoreInTouchRadiusHandleOffset)// && handleButton.GetComponent<RectTransform>().localPosition.x <= 0  
  89.                     {  
  90.                         //0-180  -180-0偏移后的角度 从第一象限校正到0-360  
  91.                         float angle = (Mathf.Atan2(localPos.y, localPos.x)) * Mathf.Rad2Deg;  
  92.                         if (angle < 0) angle = 360 + angle;;  
  93.   
  94.                         if (angle < firstAngle) angle = firstAngle;  
  95.                         if (angle > secondAngle) angle = secondAngle;  
  96.   
  97.                         angle = (tempAngle + angle) / 2f;  
  98.                         tempAngle = angle;  
  99.                         //改变小圆的位置  
  100.                         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);  
  101.   
  102.   
  103.                         this.transform.parent.GetComponent<Image>().color = Color.Lerp(Color.green, Color.blue, (angle - firstAngle) / (secondAngle - firstAngle));  
  104.   
  105.                               
  106.                         //数值的偏移值  
  107.                         float temp = secondAngle - firstAngle;// 360 - 285 + 64;  
  108.   
  109.                         float tempangle = (angle - firstAngle)/ (secondAngle - firstAngle);  
  110.                               
  111.                         //可能会出现很小的数 注意保留小数位数  
  112.                         text.text = tempangle.ToString();  
  113.                     }  
  114.                 }  
  115.                 yield return 0;  
  116.             }          
  117.         }     
  118.     }  
  119. }  

 

posted on 2018-03-27 12:02  pnzpb  阅读(201)  评论(0编辑  收藏  举报

导航