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

 更新时间:2020年04月15日 09:08:03   作者:Htlas  
这篇文章主要为大家详细介绍了Unity3D基于陀螺仪实现VR相机功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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);
  }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C#中Socket与Unity相结合示例代码

    C#中Socket与Unity相结合示例代码

    这篇文章主要给大家介绍了关于C#中Socket与Unity相结合的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
    2017-10-10
  • 解析使用enumerator模式简化异步操作的详解

    解析使用enumerator模式简化异步操作的详解

    本篇文章是对使用enumerator模式简化异步操作进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C#如何实现用户名与密码登录

    C#如何实现用户名与密码登录

    这篇文章主要介绍了C#如何实现用户名与密码登录问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • 浅析C# 装箱和拆箱

    浅析C# 装箱和拆箱

    这篇文章主要介绍了C# 装箱和拆箱的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • C#利用正则判断输入是否为纯数字、容器类

    C#利用正则判断输入是否为纯数字、容器类

    这篇文章主要介绍了C#利用正则判断输入是否为纯数字、容器类的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • C# Socket通信的实现(同时监听多客户端)

    C# Socket通信的实现(同时监听多客户端)

    这篇文章主要介绍了C# Socket通信的实现(同时监听多客户端),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • Unity3D Shader实现动态屏幕遮罩

    Unity3D Shader实现动态屏幕遮罩

    这篇文章主要为大家详细介绍了Unity3D Shader实现动态屏幕遮罩效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • C#获取全部目录和文件的简单实例

    C#获取全部目录和文件的简单实例

    这篇文章介绍了C#获取全部目录和文件的简单实例,有需要的朋友可以参考一下
    2013-10-10
  • c#生成高清缩略图的二个示例分享

    c#生成高清缩略图的二个示例分享

    这篇文章主要介绍了c#生成高清缩略图的二个示例,需要的朋友可以参考下
    2014-04-04
  • c# 异步编程入门

    c# 异步编程入门

    这篇文章主要介绍了c# 异步编程入门的相关资料,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-03-03

最新评论