服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity UGUI通过摇杆控制角色移动

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

2022-08-08 10:23Htlas C#

这篇文章主要为大家详细介绍了Unity3D基于陀螺仪实现VR相机功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Unity UGUI通过摇杆控制角色移动的具体代码,供大家参考,具体内容如下

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

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

进阶版:控制人物的移动

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

知识铺垫:

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

如下所示:

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

我们可以先对这几个接口方法进行一下测试:

测试结束后,大家就会对这些接口方法有一些初步的了解。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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拖拽结束

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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控制移动的,遥感控制移动只需要更改一个很小的地方即可:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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);
  }
 }
}

木块版本遥感操作代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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即可:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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);
  }
 }
}

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

原文链接:https://blog.csdn.net/Htlas/article/details/79188008

延伸 · 阅读

精彩推荐
  • C#c#中合并excel表格的方法示例

    c#中合并excel表格的方法示例

    本篇文章主要介绍了c#中合并excel表格的方法,就是将excel表格结构一样的合并到一起,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。...

    红叶飘飞落6452021-12-08
  • C#C#串口通讯概念及简单的实现方法

    C#串口通讯概念及简单的实现方法

    这篇文章主要给大家介绍了关于C#串口通讯概念及简单的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者使用C#具有一定的参考学习价值,...

    张子浩5542022-07-13
  • C#C#编程实现对象与JSON串互相转换实例分析

    C#编程实现对象与JSON串互相转换实例分析

    这篇文章主要介绍了C#编程实现对象与JSON串互相转换的方法,结合实例分析了在DoNet2.0与Donet3.5环境下实现对象与JSON转换的相关技巧,需要的朋友可以参考下...

    Jan.David11682021-11-02
  • C#C#实现餐厅管理系统

    C#实现餐厅管理系统

    这篇文章主要为大家详细介绍了C#实现餐厅管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    malloc_w10062022-03-09
  • C#深入浅析Restful接口的两种使用方式

    深入浅析Restful接口的两种使用方式

    restful接口常用的两种方式是get和post.接下来通过本文给大家介绍Restful接口的两种使用方式,本文给大家介绍的非常详细,需要的朋友参考下吧...

    陈晓婵10842022-03-01
  • C#WPF通过线程使用ProcessBar的方法详解

    WPF通过线程使用ProcessBar的方法详解

    这篇文章主要给大家介绍了关于WPF通过线程使用ProcessBar的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用WPF具有一定的参考学习价值,...

    风幻影6162022-07-19
  • C#C#清理非托管对象实例分析

    C#清理非托管对象实例分析

    这篇文章主要介绍了C#清理非托管对象的方法,结合实例形式详细分析了C#清理非托管对象释放资源的相关原理与实现技巧,需要的朋友可以参考下...

    Microblue7942021-11-14
  • C#C#正方形图片的绘制方法

    C#正方形图片的绘制方法

    这篇文章主要为大家详细介绍了C#正方形图片的绘制方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    薛定谔家的猫12212022-03-02