Unity实现简单的虚拟摇杆

 更新时间:2020年04月14日 11:35:24   作者:RiKoPon  
这篇文章主要为大家详细介绍了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);

    }
   }
  }

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

相关文章

  • C#开发Windows UWP系列之3D变换

    C#开发Windows UWP系列之3D变换

    这篇文章介绍了C#开发Windows UWP系列之3D变换,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • C#如何通过probing指定dll寻找文件夹详解

    C#如何通过probing指定dll寻找文件夹详解

    这篇文章主要给大家介绍了关于C#如何通过probing指定dll寻找文件夹的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-12-12
  • C#子类对基类方法的继承、重写与隐藏详解

    C#子类对基类方法的继承、重写与隐藏详解

    这篇文章主要介绍了C#子类对基类方法的继承、重写与隐藏的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-07-07
  • C#操作注册表的方法

    C#操作注册表的方法

    以下从‘读’‘写’‘删除’‘判断’四个事例实现对注册表的简单操作
    2007-03-03
  • C# 读写自定义的Config文件的实现方法

    C# 读写自定义的Config文件的实现方法

    本文主要介绍了C# 读写自定义的Config文件的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • 关于System.Convert的那些事儿

    关于System.Convert的那些事儿

    本篇文章是对System.Convert的那些事儿,进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C#实现WinForm全屏置顶的示例代码

    C#实现WinForm全屏置顶的示例代码

    我们在运行一些 Windows 应用程序的时候,需要将其运行在窗体置顶的模式,并且进入全屏状态,本文将介绍如何使用 C# 来实现 WinForm 的全屏置顶的基本功能,感兴趣的可以了解下
    2024-12-12
  • 字符串替换Replace仅替换第一个字符串匹配项

    字符串替换Replace仅替换第一个字符串匹配项

    C#里面的String.Replace(string,string)方法替换的时候是替换所有的匹配项,我们需要只替换第一个匹配项,写一个方法来实现这个功能
    2013-12-12
  • C#实现让窗体永远在窗体最前面显示的实例

    C#实现让窗体永远在窗体最前面显示的实例

    这篇文章主要介绍了C#实现让窗体永远在窗体最前面显示,功能非常实用,需要的朋友可以参考下
    2014-07-07
  • gridview 显示图片的实例代码

    gridview 显示图片的实例代码

    gridview 图片的二进制数据库存储和显示
    2013-04-04

最新评论