Advertisement
LordSolrac2

Joystick.cs

Jan 27th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.23 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Events;
  4. using UnityEngine.EventSystems;
  5. using System.Collections;
  6.  
  7. #if UNITY_EDITOR
  8. using UnityEditor;
  9. #endif
  10.  
  11. namespace UnityEngine.UI {
  12.     [AddComponentMenu("UI/Joystick", 36), RequireComponent(typeof(RectTransform))]
  13.     public class Joystick : UIBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler {
  14.  
  15.         [SerializeField, Tooltip("The child graphic that will be moved around")]
  16.         RectTransform _joystickGraphic;
  17.         public RectTransform JoystickGraphic {
  18.             get { return _joystickGraphic; }
  19.             set {
  20.                 _joystickGraphic = value;
  21.                 UpdateJoystickGraphic();
  22.             }
  23.         }
  24.  
  25. //      [SerializeField]
  26.         public Vector2 _axis;
  27.  
  28.         [SerializeField, Tooltip("How fast the joystick will go back to the center")]
  29.         float _spring = 25;
  30.         public float Spring {
  31.             get { return _spring; }
  32.             set { _spring = value; }
  33.         }
  34.  
  35.         [SerializeField,  Tooltip("How close to the center that the axis will be output as 0")]
  36.         float _deadZone = .1f;
  37.         public float DeadZone {
  38.             get { return _deadZone; }
  39.             set { _deadZone = value; }
  40.         }
  41.  
  42.         [Tooltip("Customize the output that is sent in OnValueChange")]
  43.         public AnimationCurve outputCurve = new AnimationCurve(new Keyframe(0, 0, 1, 1), new Keyframe(1, 1, 1, 1));
  44.  
  45.         public JoystickMoveEvent OnValueChange;
  46.  
  47.         public Vector2 JoystickAxis {
  48.             get {
  49.                 Vector2 outputPoint = _axis.magnitude > _deadZone ? _axis : Vector2.zero;
  50.                 float magnitude = outputPoint.magnitude;
  51.  
  52.                 outputPoint *= outputCurve.Evaluate(magnitude);
  53.  
  54.                 return outputPoint;
  55.             }
  56.             set { SetAxis(value); }
  57.         }
  58.  
  59.         RectTransform _rectTransform;
  60.         public RectTransform rectTransform {
  61.             get {
  62.                 if(!_rectTransform) _rectTransform = transform as RectTransform;
  63.  
  64.                 return _rectTransform;
  65.             }
  66.         }
  67.  
  68.         bool _isDragging;
  69.  
  70.         [HideInInspector]
  71.         bool dontCallEvent;
  72.  
  73.         public void OnBeginDrag(PointerEventData eventData) {
  74.             if(!IsActive())
  75.                 return;
  76.  
  77.             EventSystem.current.SetSelectedGameObject(gameObject, eventData);
  78.  
  79.             Vector2 newAxis = transform.InverseTransformPoint(eventData.position);
  80.  
  81.             newAxis.x /= rectTransform.sizeDelta.x * .5f;
  82.             newAxis.y /= rectTransform.sizeDelta.y * .5f;
  83.  
  84.             SetAxis(newAxis);
  85.  
  86.             _isDragging = true;
  87.             dontCallEvent = true;
  88.         }
  89.  
  90.         public void OnEndDrag(PointerEventData eventData) {
  91.             _isDragging = false;
  92.         }
  93.  
  94.         public void OnDrag(PointerEventData eventData) {
  95.             RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out _axis);
  96.  
  97.             _axis.x /= rectTransform.sizeDelta.x * .5f;
  98.             _axis.y /= rectTransform.sizeDelta.y * .5f;
  99.  
  100.             SetAxis(_axis);
  101.  
  102.             dontCallEvent = true;
  103.         }
  104.  
  105.         void OnDeselect() {
  106.             _isDragging = false;
  107.         }
  108.  
  109.  
  110.         void Update() {
  111.             if(_isDragging)
  112.                 if(!dontCallEvent)
  113.                     if(OnValueChange != null) OnValueChange.Invoke(JoystickAxis);
  114.         }
  115.  
  116.         void LateUpdate() {
  117.             if(!_isDragging)
  118.                 if(_axis != Vector2.zero) {
  119.                     Vector2 newAxis = _axis - (_axis * Time.unscaledDeltaTime * _spring);
  120.  
  121.                     if(newAxis.sqrMagnitude <= .0001f)
  122.                         newAxis = Vector2.zero;
  123.  
  124.                     SetAxis(newAxis);
  125.                 }
  126.  
  127.             dontCallEvent = false;
  128.         }
  129.         protected override void OnValidate() {
  130.             base.OnValidate();
  131.             UpdateJoystickGraphic();
  132.         }
  133.  
  134.  
  135.         public void SetAxis(Vector2 axis) {
  136.             _axis = Vector2.ClampMagnitude(axis, 1);
  137.  
  138.             Vector2 outputPoint = _axis.magnitude > _deadZone ? _axis : Vector2.zero;
  139.             float magnitude = outputPoint.magnitude;
  140.  
  141.             outputPoint *= outputCurve.Evaluate(magnitude);
  142.  
  143.             if(!dontCallEvent)
  144.                 if(OnValueChange != null)
  145.                     OnValueChange.Invoke(outputPoint);
  146.  
  147.             UpdateJoystickGraphic();
  148.         }
  149.  
  150.         void UpdateJoystickGraphic() {
  151.             if(_joystickGraphic)
  152.                 _joystickGraphic.localPosition = _axis * Mathf.Max(rectTransform.sizeDelta.x, rectTransform.sizeDelta.y) * .5f;
  153.         }
  154.  
  155.         [System.Serializable]
  156.         public class JoystickMoveEvent : UnityEvent<Vector2> { }
  157.     }
  158. }
  159.  
  160. #if UNITY_EDITOR
  161. static class JoystickGameObjectCreator {
  162.     [MenuItem("GameObject/UI/Joystick", false, 2000)]
  163.     static void Create() {
  164.         GameObject go = new GameObject("Joystick", typeof(Joystick));
  165.  
  166.         Canvas canvas = Selection.activeGameObject ? Selection.activeGameObject.GetComponent<Canvas>() : null;
  167.  
  168.         Selection.activeGameObject = go;
  169.  
  170.         if(!canvas)
  171.             canvas = Object.FindObjectOfType<Canvas>();
  172.  
  173.         if(!canvas) {
  174.             canvas = new GameObject("Canvas", typeof(Canvas), typeof(RectTransform), typeof(GraphicRaycaster)).GetComponent<Canvas>();
  175.             canvas.renderMode = RenderMode.ScreenSpaceOverlay;
  176.         }
  177.  
  178.         if(canvas)
  179.             go.transform.SetParent(canvas.transform, false);
  180.  
  181.         GameObject background = new GameObject("Background", typeof(Image));
  182.         GameObject graphic = new GameObject("Graphic", typeof(Image));
  183.  
  184.         background.transform.SetParent(go.transform, false);
  185.         graphic.transform.SetParent(go.transform, false);
  186.  
  187.         background.GetComponent<Image>().color = new Color(1, 1, 1, .86f);
  188.  
  189.         RectTransform backgroundTransform = graphic.transform as RectTransform;
  190.         RectTransform graphicTransform = graphic.transform as RectTransform;
  191.  
  192.         graphicTransform.sizeDelta = backgroundTransform.sizeDelta * .5f;
  193.  
  194.         Joystick joystick = go.GetComponent<Joystick>();
  195.         joystick.JoystickGraphic = graphicTransform;
  196.     }
  197. }
  198. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement