UGUI 虚拟摇杆的实现

建2张 image,大小自己看着办:
这里写图片描述

第一步:将上一次拖拽实现的代码全部复制过来.将脚本放在较小的正方形 image

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Rocker : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler{

    public float radius = 100f;
    private RectTransform selfTransform;


    void Start(){

    }



    public void OnBeginDrag(PointerEventData eventData){

    }



    public void OnDrag(PointerEventData eventData){
        Vector3 pos;
RectTransformUtility.ScreenPointToWorldPointInRectangle(selfTransform,eventData.position,eventData.pressEventCamera,out pos);
        selfTransform.position = pos;



    public void OnEndDrag(PointerEventData eventData){


    }
}

我们来试一试效果
这里写图片描述
很明显,我们需要将图片设定范围,已经松开之后,小图片应该回到原点.
我们先让它回原点:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Rocker : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler{

    public float radius = 100f;
    private RectTransform selfTransform;
    private Vector2 origin_position;
    void Start(){

        selfTransform = GetComponent<RectTransform>();
        //记住原点
        origin_position = selfTransform.anchoredPosition;
    }


    public void OnBeginDrag(PointerEventData eventData){

    }


    public void OnDrag(PointerEventData eventData){

        Vector3 pos;
        RectTransformUtility.ScreenPointToWorldPointInRectangle(selfTransform,eventData.position,eventData.pressEventCamera,out pos);
        selfTransform.position = pos;


    }
    public void OnEndDrag(PointerEventData eventData){
        //拖拽结束,回到原点
        selfTransform.anchoredPosition = origin_position;

    }
}

添加了4句话,再来看看效果:
这里写图片描述

下步限定它的范围:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Rocker : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler{

    public float radius = 100f;
    private RectTransform selfTransform;
    private Vector2 origin_position;
    void Start(){


        selfTransform = GetComponent<RectTransform>();
        //记住原点
        origin_position = selfTransform.anchoredPosition;
    }

    public void OnBeginDrag(PointerEventData eventData){
    }


    public void OnDrag(PointerEventData eventData){

        Vector3 pos;
        RectTransformUtility.ScreenPointToWorldPointInRectangle(selfTransform,eventData.position,eventData.pressEventCamera,out pos);
        selfTransform.position = pos;
        Vector2 touchAxis = selfTransform.anchoredPosition - origin_position;
        if(touchAxis.magnitude >= radius)
        {
            touchAxis = touchAxis.normalized * radius;
            selfTransform.anchoredPosition = touchAxis;
        }

    }
    public void OnEndDrag(PointerEventData eventData){
        //拖拽结束,回到原点
        selfTransform.anchoredPosition = origin_position;
    }
}

再次添加了4句话,看看效果:
这里写图片描述

我们添加个物体来移动看看,代码需修改一下,为了方便我直接使用静态变量:

using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Rocker : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler{

    public float radius = 100f;
    private RectTransform selfTransform;
    private Vector2 origin_position;
    public static Vector2 touchAxis;
    public static bool is_ready_move = false;
    void Start(){


        selfTransform = GetComponent<RectTransform>();
        //记住原点
        origin_position = selfTransform.anchoredPosition;
    }

    public void OnBeginDrag(PointerEventData eventData){
        is_ready_move = true;
    }


    public void OnDrag(PointerEventData eventData){

        Vector3 pos;
        RectTransformUtility.ScreenPointToWorldPointInRectangle(selfTransform,eventData.position,eventData.pressEventCamera,out pos);
        selfTransform.position = pos;
        touchAxis = selfTransform.anchoredPosition - origin_position;
        if(touchAxis.magnitude >= radius)
        {
             touchAxis = touchAxis.normalized * radius;
            selfTransform.anchoredPosition = touchAxis;
        }

    }
    public void OnEndDrag(PointerEventData eventData){
        //拖拽结束,回到原点
        selfTransform.anchoredPosition = origin_position;
        is_ready_move = false;
    }
}

然后挂在物体上的代码:


public class Unit : MonoBehaviour {

    public float speed =20f;
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        if (Rocker.is_ready_move)
        {
            transform.Translate(new Vector3(Rocker.touchAxis.normalized.x,0,Rocker.touchAxis.normalized.y)*Time.deltaTime*speed);

        }

    }
}

看看效果
这里写图片描述

我们发现,鼠标点击的情况下,移动到中间,物体还在移动,这里相信你们自己就可以搞定了.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值