Unity实现简单虚拟摇杆
作者:臣定保幼主周全ぃ
这篇文章主要为大家详细介绍了Unity实现简单虚拟摇杆,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
最近一直在倒腾用UGUI做虚拟摇杆,网上普遍的的做法就是使用以下的代码,但是这个有些注意事项,第一点就是Canvas的Render Mode必须是Screen Space Overlay,第二点就是挂载这个脚本的锚点的x,y必须是0.5,如图下:
using UnityEngine; using UnityEngine.EventSystems; public class JoyStick : MonoBehaviour, IDragHandler, IEndDragHandler { Transform point; Vector3 startPos;//开始位置 Vector3 dir;//方向 float radius = 0;//需要移动的半径 void Start() { point = transform.GetChild(0); radius = (transform as RectTransform).sizeDelta.x * 0.5f; startPos = point.position; } public void OnDrag(PointerEventData eventData) { point.position = eventData.position; dir = (point.position - startPos).normalized; if (Vector3.SqrMagnitude(point.position - startPos) > radius * radius) point.position = startPos + dir * radius; } public void OnEndDrag(PointerEventData eventData) { point.localPosition = Vector3.zero; } }
如果Canvas的Render Mode是Screen Space Camera,这样的话上面的代码是不能满足要求的,花了一点时间才发现是这个原因,导致上面的代码不适用的,最后把代码重写了一下,终于可以成功了!
public class JoyStick : MonoBehaviour, IDragEvent { private Canvas canvas; private RectTransform rectTransform;//坐标 private static Quaternion amendAngle; private static float mRadius = 0,v=0, h=0; private static Transform point; private static Vector3 initPos; private static Vector2 startPos; private void Start() { point = transform.GetChild(0); canvas = GameObject.Find("UIRoot").GetComponent<Canvas>(); rectTransform = transform as RectTransform; //也可以写成this.GetComponent<RectTransform>(),但是不建议; mRadius = (transform as RectTransform).sizeDelta.x * 0.5f; initPos = point.localPosition; h = v = 0; } public void OnBeginDrag(PointerEventData eventData) { RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventData.position, canvas.worldCamera, out startPos); startPos = eventData.position - startPos; h = v = 0; } public void OnDrag(PointerEventData eventData) { point.localPosition = eventData.position - startPos; Vector3 dir = (point.localPosition - initPos).normalized; v = dir.normalized.x; h = dir.normalized.y; if (Vector3.SqrMagnitude(point.localPosition - initPos) > mRadius * mRadius) point.localPosition = initPos + dir * mRadius; } public void OnEndDrag(PointerEventData eventData) { point.localPosition = Vector3.zero; h = v = 0; } }
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventData.position, canvas.worldCamera, out startPos)这个的startPos返回的是点击屏幕的坐标,rectTransform是这个脚本挂载物体上的RectTransform的组件,然后减去eventData.position就知道坐标的偏移值了,看一下代码应该都可以了解意思,这里就不过多的解释了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。