UGUI实现的虚拟摇杆,可改变摇杆位置

实现方式主要参考这篇文章:http://www.cnblogs.com/plateFace/p/4687896.html。

主要代码如下:

  1 using UnityEngine;
  2 using System.Collections;
  3 using UnityEngine.UI;
  4 using UnityEngine.EventSystems;
  5 
  6 
  7 public delegate void JoystickMoveDelegate(JoystickData data);
  8 
  9 public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
 10 {
 11 
 12 
 13     public GameObject joystickUI;                   //摇杆整体UI,方便Active
 14     public RectTransform joystickCenter;            //摇杆重心
 15     public RectTransform joystickBackground;        //摇杆背景
 16 
 17     public RectTransform joystickRect;
 18     private float radius;
 19     bool isClick;
 20     Vector2 clickPosition;
 21 
 22 
 23     public static event JoystickMoveDelegate JoystickMoveEvent;
 24 
 25 
 26 
 27     // Use this for initialization
 28     void Start()
 29     {
 30         radius = 71;
 31     }
 32 
 33     // Update is called once per frame
 34     void Update()
 35     {
 36         JoystickController();
 37     }
 38 
 39     public void JoystickController()
 40     {
 41         if (isClick)
 42         {
 43             clickPosition = GetClickPosition();
 44             float distance = Vector2.Distance(new Vector2(clickPosition.x, clickPosition.y) / Screen.width * 960, joystickRect.anchoredPosition);
 45 
 46             if (distance < radius)
 47             {
 48                 //当距离小于半径就开始移动 摇杆重心
 49                 joystickCenter.anchoredPosition = new Vector2(clickPosition.x / Screen.width * 960 - joystickRect.anchoredPosition.x, clickPosition.y / Screen.width * 960 - joystickRect.anchoredPosition.y);
 50             }
 51             else
 52             {
 53                 //求圆上的一点:(目标点-原点) * 半径/原点到目标点的距离
 54                 Vector2 endPosition = (new Vector2(clickPosition.x, clickPosition.y) / Screen.width * 960 - joystickRect.anchoredPosition) * radius / distance;
 55                 joystickCenter.anchoredPosition = endPosition;
 56             }
 57 
 58             if (JoystickMoveEvent != null)
 59             {
 60                 JoystickMoveEvent(new JoystickData() { x = joystickCenter.anchoredPosition.x - joystickBackground.anchoredPosition.x, y = joystickCenter.anchoredPosition.y - joystickBackground.anchoredPosition.y });
 61             }
 62 
 63         }
 64     }
 65 
 66     public void OnPointerDown(PointerEventData eventData)
 67     {
 68         ChangeAlpha(1);
 69         clickPosition = GetClickPosition();
 70         joystickRect.anchoredPosition = clickPosition / Screen.width * 960;
 71         isClick = true;
 72     }
 73 
 74     public void OnPointerUp(PointerEventData eventData)
 75     {
 76         ChangeAlpha(0.3f);
 77         joystickCenter.anchoredPosition = Vector2.zero;
 78         isClick = false;
 79     }
 80 
 81     /// <summary>
 82     /// 根据平台不同获取点击位置坐标
 83     /// </summary>
 84     /// <returns></returns>
 85     Vector2 GetClickPosition()
 86     {
 87         if (Application.platform == RuntimePlatform.Android)
 88         {
 89             return Input.GetTouch(0).position;
 90 
 91         }
 92         else if (Application.platform == RuntimePlatform.WindowsEditor)
 93         {
 94             return Input.mousePosition;
 95         }
 96         return Vector2.zero;
 97     }
 98 
 99     /// <summary>
100     /// 改变图片alpha值
101     /// </summary>
102     /// <param name="alphaValue"></param>
103     void ChangeAlpha(float alphaValue)
104     {
105         joystickBackground.GetComponent<RawImage>().color = new Color(1,1,1,alphaValue);
106         joystickCenter.GetComponent<Image>().color = new Color(1, 1, 1, alphaValue);
107     }
108 }
109 
110 public class JoystickData
111 {
112     public float x;
113     public float y;
114 
115 }

主要实现了两个接口:IPointerDownHandler, IPointerUpHandler,监测按下和抬起事件。

原文地址:https://www.cnblogs.com/bzyzhang/p/5411822.html