Unity实现简单的虚拟摇杆

本文实例为大家分享了Unity实现简单虚拟摇杆的具体代码,供大家参考,具体内容如下

需求:点击创建一个虚拟摇杆底盘,鼠标拖拽时候上方摇杆会跟随鼠标方向移动,并且不会超出摇杆盘范围
*摇杆功能另外实现

UI显示

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

public class RockingIcon : MonoBehaviour
{
 public Transform touchPoint;
 public Transform bgPoint;
 public float radius;
 bool isPressing;
 Vector3 bgPos;

 private void Update()
 {
  bool pressing;
  Vector3 pos;
  if (Application.isEditor)
   GetPressingInfoInEditor(out pressing, out pos);
  else
   GetPressingInfoInPhone(out pressing, out pos);
  SetIcon(pressing, pos);

 }

 void GetPressingInfoInEditor(out bool pressing, out Vector3 pos)
 {
  if (Input.GetMouseButton(0))
  {
   pressing = true;
   pos = Input.mousePosition;
  }
  else
  {
   pressing = false;
   pos = Vector3.zero;
  }
 }

 void GetPressingInfoInPhone(out bool pressing, out Vector3 pos)
 {
  if(Input.touchCount > 0)
  {
   pressing = true;
   pos = Input.GetTouch(0).position;
  }
  else
  {
   pressing = false;
   pos = Vector3.zero;
  }
 }

 void SetIcon(bool pressing, Vector3 pos)
 {
  if (pressing)
  {
   if (!isPressing)
   {
    bgPoint.gameObject.SetActive(true);
    bgPoint.transform.position = pos;
    bgPos = pos;
    isPressing = true;
   }
   else
   {
    bgPoint.gameObject.SetActive(true);
    SetTouchPointPos(pos);
   }
  }
  else
  {
   touchPoint.gameObject.SetActive(false);
   bgPoint.gameObject.SetActive(false);
   isPressing = false;
  }
 }

 void SetTouchPointPos(Vector3 pos)
 {
  Vector3 center = bgPoint.position;
  Vector3 touch = pos;
  Vector3 to;
  float distance = Vector3.Distance(center, touch);
  if (distance < radius)
   to = touch;
  else
  {
   Vector3 dir = touch - center;
   dir.Normalize();
   to = dir * radius;
   to += center;
  }
  touchPoint.gameObject.SetActive(true);
  touchPoint.transform.position = to;
 }
}

预制:

操作控制

#region 鼠标操作

float min_move_x = Global.min_move_distance * (Screen.width / 1080f);
float min_move_y = Global.min_move_distance * (Screen.height / 1900f);

if(Application.platform == RuntimePlatform.WindowsEditor)
  {
   if (Input.GetMouseButtonDown(0))
   {
    touch_time = 0;
    first_touch_pos = Input.mousePosition;
   }
   else if (Input.GetMouseButton(0))
   {

    touch_time += Time.deltaTime;
    if (touch_time >= Global.touch_time_limit)
    {
     Vector2 touch_pos = Input.mousePosition;
     Vector2 distance = touch_pos - first_touch_pos;

     //Vector2 touch_pos_in_func = PosInTheFunc(touch_pos);
     //Vector2 first_pos_in_func = PosInTheFunc(first_touch_pos);
     //Vector2 distance = touch_pos_in_func - first_pos_in_func;

     if (Mathf.Abs(distance.x) > min_move_x && Mathf.Abs(distance.x) > Mathf.Abs(distance.y)) Move(distance.x > 0 ? Vector3.right : Vector3.left);
     if (Mathf.Abs(distance.y) > min_move_y && Mathf.Abs(distance.y) > Mathf.Abs(distance.x)) Move(distance.y > 0 ? Vector3.forward : Vector3.back);
    }
   }
   else if (Input.GetMouseButtonUp(0))
   {

    //if(touch_time < Global.touch_time_limit)
    //{
    // PutBoomb();
    //}
    touch_time = 0;
    first_touch_pos = Vector3.zero;
   }
  }
  #endregion
  #region 手机操作

  if (Application.platform == RuntimePlatform.Android)
  {
   if (Input.touchCount > 0)
   {

    Touch touch = Input.GetTouch(0);
    if (touch.phase == TouchPhase.Began)
    {
     first_touch_pos = touch.position;

    }
    else if (touch.phase == TouchPhase.Ended)
    {
     first_touch_pos = Vector3.zero;
    }
    else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
    {
     Vector2 touch_pos = touch.position;
     Vector2 distance = touch_pos - first_touch_pos;
     if (Mathf.Abs(distance.x) > min_move_x && Mathf.Abs(distance.x) > Mathf.Abs(distance.y)) Move(distance.x > 0 ? Vector3.right : Vector3.left);
     if (Mathf.Abs(distance.y) > min_move_y && Mathf.Abs(distance.y) > Mathf.Abs(distance.x)) Move(distance.y > 0 ? Vector3.forward : Vector3.back);

    }
   }
  }

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

(0)

相关推荐

  • Unity实现虚拟摇杆

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

  • Unity3D基于UGUI实现虚拟摇杆

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

  • Unity实现简单虚拟摇杆

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

  • Unity实现简单的虚拟摇杆

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

  • 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实现虚拟摇杆效果

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

  • 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虚拟摇杆的实现方法

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

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

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

  • unity实现简单的贪吃蛇游戏

    本文实例为大家分享了unity实现简单贪吃蛇游戏的具体代码,供大家参考,具体内容如下 SatUIController代码 using UnityEngine; using UnityEngine.UI; public class StartUIController : MonoBehaviour { public Text lastText; public Text bestText; public Toggle blue; public Toggle yellow; public Toggle

随机推荐