Skip to content

Instantly share code, notes, and snippets.

@belzecue
Forked from baba-s/RadialSlider.cs
Created December 6, 2019 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belzecue/31e62a16a521d6b5c01c6924676e6928 to your computer and use it in GitHub Desktop.
Save belzecue/31e62a16a521d6b5c01c6924676e6928 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class RadialSlider : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
{
bool isPointerDown = false;
// Called when the pointer enters our GUI component.
// Start tracking the mouse
public void OnPointerEnter( PointerEventData eventData )
{
StartCoroutine( "TrackPointer" );
}
// Called when the pointer exits our GUI component.
// Stop tracking the mouse
public void OnPointerExit( PointerEventData eventData )
{
StopCoroutine( "TrackPointer" );
}
public void OnPointerDown( PointerEventData eventData )
{
isPointerDown = true;
//Debug.Log("mousedown");
}
public void OnPointerUp( PointerEventData eventData )
{
isPointerDown = false;
//Debug.Log("mousedown");
}
// mainloop
IEnumerator TrackPointer()
{
var ray = GetComponentInParent<GraphicRaycaster>();
var input = FindObjectOfType<StandaloneInputModule>();
var text = GetComponentInChildren<Text>();
if ( ray != null && input != null )
{
while ( Application.isPlaying )
{
// TODO: if mousebutton down
if ( isPointerDown )
{
Vector2 localPos; // Mouse position
RectTransformUtility.ScreenPointToLocalPointInRectangle( transform as RectTransform, Input.mousePosition, ray.eventCamera, out localPos );
// local pos is the mouse position.
float angle = ( Mathf.Atan2( -localPos.y, localPos.x ) * 180f / Mathf.PI + 180f ) / 360f;
GetComponent<Image>().fillAmount = angle;
GetComponent<Image>().color = Color.Lerp( Color.green, Color.red, angle );
text.text = ( ( int )( angle * 360f ) ).ToString();
//Debug.Log(localPos+" : "+angle);
}
yield return 0;
}
}
else
UnityEngine.Debug.LogWarning( "Could not find GraphicRaycaster and/or StandaloneInputModule" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment