- using UnityEngine;
- using System.Collections;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
-
- public class ArcSlider : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
- {
- public Image handleButton;
- float circleRadius = 0.0f;
-
- bool isPointerDown = false;
- public Image baseCircle;
-
-
- public float ignoreInTouchRadiusHandleOffset = 10;
-
- Vector3 handleButtonLocation;
-
- [Tooltip("初始角度到终止角度")]
- public float firstAngle = 30;
- public float secondAngle = 150;
-
- float tempAngle = 30;
- 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;
- }
- public void Update()
- {
-
- if (Input.GetKeyDown(KeyCode.R))
- {
- ReSet();
- }
- }
- 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 != && input != )
- {
- while( Application.isPlaying )
- {
-
- if (isPointerDown)
- {
- Vector2 localPos;
-
- RectTransformUtility.ScreenPointToLocalPointInRectangle( transform as RectTransform, Input.mousePosition, ray.eventCamera, out localPos );
-
- localPos.x = -localPos.x;
-
-
- float mouseRadius = Mathf.Sqrt(localPos.x*localPos.x+localPos.y*localPos.y);
-
-
- if (mouseRadius > ignoreInTouchRadiusHandleOffset)
- {
-
- float angle = (Mathf.Atan2(localPos.y, localPos.x)) * Mathf.Rad2Deg;
- 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);
-
-
- this.transform.parent.GetComponent<Image>().color = Color.Lerp(Color.green, Color.blue, (angle - firstAngle) / (secondAngle - firstAngle));
-
-
-
- float temp = secondAngle - firstAngle;
-
- float tempangle = (angle - firstAngle)/ (secondAngle - firstAngle);
-
-
- text.text = tempangle.ToString();
- }
- }
- yield return 0;
- }
- }
- }
- }
![]()
![]()
![]()