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就知道坐标的偏移值了,看一下代码应该都可以了解意思,这里就不过多的解释了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值