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);
  }
 }
}

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

(0)

相关推荐

  • unity实现虚拟摇杆控制Virtual Joystick

    本文实例为大家分享了unity实现虚拟摇杆控的具体代码,供大家参考,具体内容如下 using UnityEngine; using UnityEngine.UI; public class TouchJoystick : MonoBehaviour { public GameObject go;//需要通过虚拟摇杆控制的目标物体 public float moveSpeed = 3;//移动速度 public Image touchPoint;//摇杆轴对象 private Vector3 Or

  • unity实现手机端摇杆控制人物移动

    前言 玩过王者荣耀或者荒野行动的人,都知道,手机左下方或右下方都会有一个摇杆,滑动摇杆可以让人物向360度方向移动.没有玩过的可以看看下方图片(荒野行动手机端为例).本篇就来讲解如何使用unity制作摇杆控制人物移动. 2种方法: 1.GUITexture制作,是unity自带的一个joystick组件,主要由GUITexture和一个JS脚本构成.优点:unity自带,使用简单方便.缺点是无法适应屏幕大小. 2.EasyTouch插件,可以实现1的功能并且克服了1的缺陷,可以适应屏幕大小.本篇

  • unity实现手游虚拟摇杆

    本文实例为大家分享了unity实现手游虚拟摇杆的具体代码,供大家参考,具体内容如下 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; /// <summary> /// 绑定到摇杆上的摇杆类,参考半径50 /// </summary> public class Rocker : MonoBehaviour { Vector2 m_o

  • Unity使用ScrollRect制作摇杆

    本文实例为大家分享了Unity使用ScrollRect制作摇杆的具体代码,供大家参考,具体内容如下 一. 前言 游戏开发中,摇杆功能是很常见的,Unity的UGUI提供了ScrollRect组件,非常适合用来制作摇杆,效果如下: 二. 实现 1. 制作UI 如下,创建Rocker节点和center节点,分别为摇杆的背景图和摇杆的手柄图. Rocker节点挂上Rocker脚本(代码见文章最后),并赋值Content对象. 设置MovementType为Elastic. 2. 运行Unity进行测试

  • Unity实现简单虚拟摇杆

    本文实例为大家分享了Unity虚拟摇杆的简单实现代码,供大家参考,具体内容如下 简单的Unity虚拟摇杆实现,有详细注释. Game界面 Inspector界面 摇杆脚本 public class YaoGanCtrl : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public RectTransform diPan; public RectTransform anNiu; public Vector2 d

  • Unity虚拟摇杆的实现方法

    本文实例为大家分享了Unity实现虚拟摇杆的具体代码,供大家参考,具体内容如下 设置摇杆的背景图片的锚点如下: 设置摇杆的锚点为背景图片的中心点. 并给摇杆绑定脚本如下: using UnityEngine; using UnityEngine.EventSystems; using System.Collections; using System; public class JoyStickController : MonoBehaviour,IDragHandler,IEndDragHand

  • Unity实现简单的虚拟摇杆

    本文实例为大家分享了Unity实现简单虚拟摇杆的具体代码,供大家参考,具体内容如下 需求:点击创建一个虚拟摇杆底盘,鼠标拖拽时候上方摇杆会跟随鼠标方向移动,并且不会超出摇杆盘范围 *摇杆功能另外实现 UI显示 using System.Collections; using System.Collections.Generic; using UnityEngine; public class RockingIcon : MonoBehaviour { public Transform touchP

  • Unity3D基于UGUI实现虚拟摇杆

    虚拟摇杆在移动游戏开发中,是很常见的需求,今天我们在Unity中,使用UGUI来实现一个简单的虚拟摇杆功能. 1.打开Unity,新创建一个UIJoystick.cs脚本,代码如下: using UnityEngine; using UnityEngine.EventSystems; public class UIJoystick : MonoBehaviour, IDragHandler, IEndDragHandler { /// <summary> /// 被用户拖动的操纵杆 /// &

  • Unity实现虚拟摇杆效果

    本文实例为大家分享了Unity实现虚拟摇杆效果的具体代码,供大家参考,具体内容如下 首先添加两者图片 从左到右分别是Back和Front 将Front放到Back中心 在Front身上添加脚本 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems;//导入命名空间 public class JoyStick : MonoBehavi

  • Unity实现虚拟摇杆

    本文实例为大家分享了Unity实现虚拟摇杆的具体代码,供大家参考,具体内容如下 面板上设置一些属性,比如摇杆拖拽的距离,是否始终可视,是否限制虚拟摇杆位置(我是把虚拟摇杆限制在了屏幕的左下区域). 使用GetDirAndLength()方法去获得移动的方向和长度即可 using UnityEngine; /// <summary> /// 虚拟摇杆管理器 /// </summary> public class VirtualJoystickManager : MonoBehavio

  • Unity3D使用UGUI开发原生虚拟摇杆

    在Unity3d中开发虚拟摇杆方式有比较多,可以使用EasyTouch.FairyGUI等插件来开发.本文给大家介绍使用Unity3d的原生UGUI来开发出自己的虚拟摇杆,这样可以减少游戏资源包的大小. 先展示下效果图: 现在开发我们的开发 创建一个Image1,并且在Image1创建一个子对象Image2 在Image1中挂载一个自定义脚本,这里我命名为Joystick 脚本代码如下 using System.Collections; using System.Collections.Gene

随机推荐