Unity ——UGUI 通过摇杆控制角色移动

本文档介绍了如何使用Unity的UGUI系统,通过摇杆控制游戏中的方块和角色移动。首先,展示了基础的方块移动实现,然后进阶到人物移动,利用动画位移和条件判断来实现精细控制。文中提供了相应的代码示例。

简单版:控制方块的移动。

进阶版:控制人物的移动


知识铺垫:

 首先我们必须要知道,在Unity的UGUI中,对UI的操作有八个回调,分别需要实现八个接口。分别是:
鼠标进入,鼠标离开,鼠标点下,鼠标抬起,鼠标开始拖拽,鼠标拖拽中,拖拽结束
如下所示:

我们可以先对这几个接口方法进行一下测试:
  测试结束后,大家就会对这些接口方法有一些初步的了解。
using UnityEngine;
using UnityEngine.EventSystems;

// UGUI提供了一些用来操作控件的一些方法, 这些方法是以回调的形式提供的
// 通过接口回调来实现的
/*
 * IPointerEnterHandler     void OnPointerEnter(PointerEventData eventData)
 * IPointerExitHandler      void OnPointerExit(PointerEventData eventData)
 * 
 * IPointerDownHandler      void OnPointerDown(PointerEventData eventData)
 * IPointerUpHandler        void OnPointerUp(PointerEventData eventData)
 * IPointerClickHandler     void OnPointerClick(PointerEventData eventData)
 * 
 * IBeginDragHandler        void OnBeginDrag(PointerEventData eventData)
 * IDragHandler             void OnDrag(PointerEventData eventData)
 * IEndDragHandler          void OnEndDrag(PointerEventData eventData)
 */


public class UGUICallBack : MonoBehaviour,
    IPointerEnterHandler, IPointerExitHandler,
    IPointerDownHandler, IPointerUpHandler, IPointerClickHandler,
    IBeginDragHandler, IDragHandler, IEndDragHandler 
{

    /// <summary>
    /// 当鼠标滑入控件的范围
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerEnter(PointerEventData eventData) {
        Debug.Log("鼠标划入");
    }

    /// <summary>
    /// 当鼠标离开控件的范围
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerExit(PointerEventData eventData) {
        Debug.Log("鼠标离开");
    }

    /// <summary>
    /// 当鼠标在控件范围内按下
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerDown(PointerEventData eventData) {
        Debug.Log("鼠标按下");
    }

    /// <summary>
    /// 当鼠标在控件范围内抬起
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerUp(PointerEventData eventData) {
        Debug.Log("鼠标抬起");
    }

    /// <summary>
    /// 当鼠标在控件范围内点击
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerClick(PointerEventData eventData) {
        Debug.Log("鼠标点击");
    }

    /// <summary>
    /// 当鼠标开始拖拽
    /// </summary>
    /// <param name="eventData"></param>
    public void OnBeginDrag(PointerEventData eventData) {
        Debug.Log("开始拖拽");
    }

    /// <summary>
    /// 当鼠标拖拽过程中
    /// </summary>
    /// <param name="eventData"></param>
    public void OnDrag(PointerEventData eventData) {
        Debug.Log("拖拽中");
    }

    /// <summary>
    /// 当拖拽完成
    /// </summary>
    /// <param name="eventData"></param>
    public void OnEndDrag(PointerEventData eventData) {
        Debug.Log("拖拽完成");
    }
}

下面开始讲解案例:

第一步:实现对遥感按钮的操作, 从上面的八大接口方法可以了解到,如果想实现遥感的方法我们需要实现有关拖拽的回调:UI过拽中, UI拖拽结束


对遥感的操作代码如下(非移动完整版,下面有移动完整版EasyTouchMove):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class EasyTouchMove : MonoBehaviour, IDragHandler,IEndDragHandler{
    //图标移动最大半径
    public float maxRadius = 100;
    //初始化背景图标位置
    private Vector2 moveBackPos;

    // Use this for initialization
    void Start () {
        //初始化背景图标位置
        moveBackPos = transform.parent.transform.position;
	}
	
    /// <summary>
    /// 当鼠标开始拖拽时
    /// </summary>
    /// <param name="eventData"></param>
    public void OnDrag(PointerEventData eventData) {
        //获取鼠标位置与初始位置之间的向量
        Vector2 oppsitionVec = eventData.position - moveBackPos;
        //获取向量的长度
        float distance = Vector3.Magnitude(oppsitionVec);
        //最小值与最大值之间取半径
        float radius = Mathf.Clamp(distance, 0, maxRadius);
        //限制半径长度
        transform.position = moveBackPos + oppsitionVec.normalized * radius;

    }

    /// <summary>
    /// 当鼠标停止拖拽时
    /// </summary>
    /// <param name="eventData"></param>
    public void OnEndDrag(PointerEventData eventData) {
        transform.position = moveBackPos;
    }
}

如何控制木块的移动呢:

初学者一般在学习Unity的时候都是WSAD控制移动的,遥感控制移动只需要更改一个很小的地方即可:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cube : MonoBehaviour {
    public EasyTouchMove touch;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        //获取horizontal 和 vertical 的值,其值位遥感的localPosition
        float hor = touch.Horizontal;
        float ver = touch.Vertical;

        Vector3 direction = new Vector3(hor, 0, ver);

        if(direction!= Vector3.zero) {
            //控制转向
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(direction),Time.deltaTime*10);
            //向前移动
            transform.Translate(Vector3.forward * Time.deltaTime * 5);
        }
	}
}

木块版本遥感操作代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class EasyTouchMove : MonoBehaviour, IDragHandler,IEndDragHandler{
    //图标移动最大半径
    public float maxRadius = 100;
    //初始化背景图标位置
    private Vector2 moveBackPos;

    //hor,ver的属性访问器
    private float horizontal=0;
    private float vertical=0;
    
    public float Horizontal {
        get { return horizontal; }
    }

    public float Vertical {
        get { return vertical; }
    }

  
    // Use this for initialization
    void Start () {
        //初始化背景图标位置
        moveBackPos = transform.parent.transform.position;
	}
	
	// Update is called once per frame
	void Update () {
		horizontal = transform.localPosition.x;
        vertical = transform.localPosition.y;
    }

    /// <summary>
    /// 当鼠标开始拖拽时
    /// </summary>
    /// <param name="eventData"></param>
    public void OnDrag(PointerEventData eventData) {
        //获取鼠标位置与初始位置之间的向量
        Vector2 oppsitionVec = eventData.position - moveBackPos;
        //获取向量的长度
        float distance = Vector3.Magnitude(oppsitionVec);
        //最小值与最大值之间取半径
        float radius = Mathf.Clamp(distance, 0, maxRadius);
        //限制半径长度
        transform.position = moveBackPos + oppsitionVec.normalized * radius;

    }

    /// <summary>
    /// 当鼠标停止拖拽时
    /// </summary>
    /// <param name="eventData"></param>
    public void OnEndDrag(PointerEventData eventData) {
        transform.position = moveBackPos;
        transform.localPosition = Vector3.zero;
    }
}

如何用遥感控制角色的移动,这里我们通过动画的位移来控制移动。只需当director!=vector3.zero 的时候更改动画控制器里的Float即可:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {
    //获取动画控制器
    private Animator ani;
    //获取遥感脚本
    public EasyTouchMove touch;

	void Start () {
        ani = GetComponent<Animator>();		
	}
	
	// Update is called once per frame
	void Update () {
        //hor = 遥感脚本中的localPosition.x
        float hor = touch.Horizontal;
        //hor = 遥感脚本中的localPosition.y
        float ver = touch.Vertical;

        Vector3 direction = new Vector3(hor, 0, ver);

        if (direction != Vector3.zero) {
            //控制移动
            float newSpeed = Mathf.Lerp(ani.GetFloat("Speed"), 3, Time.deltaTime * 5);
            ani.SetFloat("Speed", newSpeed);
            //控制旋转
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime * 10);
        }else {
            //停止移动
            float newSpeed = Mathf.Lerp(ani.GetFloat("Speed"), 0, Time.deltaTime * 5);
            ani.SetFloat("Speed", 0);
        }
    }
}




























评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值