【功能实现】手机游戏虚拟摇杆功能实现

2024-03-25 05:08

本文主要是介绍【功能实现】手机游戏虚拟摇杆功能实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

【UNITY3D】

声明:

查API和根据自己的想法实现此功能。如果你喜欢我这个实现,希望你能有所收获,如果你能改进那就更棒了,但如果你想直接拿走,希望标明出处。

 

只需要把脚本交给画布下的空物体,自行自定义以下内容,运行即可生成虚拟摇杆。

提供了检测区域大小的自定义。

提供了摇杆检测区域的图片自定义。

提供了摇杆底盘以及摇杆的图片自定义。

提供了摇杆灵敏度的自定义。-----值越大 ----灵敏度越小

提供了摇杆移动百分比,以及移动方向信息,可直接用于接入移动模块。

生成的是仿照Moba类的虚拟摇杆,具体分析看对应的设计分析_【测试分析】手机游戏虚拟摇杆设计分析-By Terrell21

代码中某些数字部分没提供自定义,可根据个人需要修改或设置成自定义。

枚举为  虚拟摇杆的组成部分,摇杆盘底,摇杆。

 

主要功能:

不做出检测区域外的响应。

盘底位置根据在检测区域内的响应进行位置更新。松手后返回初始位置。

盘底不回超出检测区域,不会超出屏幕边缘。

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;enum E_JoystickComponent
{chassis,joystick
}public class VirtualJoyStick : MonoBehaviour {//图片组件private Image click_limit;              //点击区域限制,,跟其RectTransform上的长宽 位置有关private GameObject chassis;          //操纵杆底盘private GameObject joystick;         //操纵杆public Sprite chassis_sprite;public Sprite joystick_sprite;public Sprite limit_sprite;//记录信息private  Vector2 prePos;                 //上一个点击位置private Touch nowTouch;                 //当前触摸信息private  Vector2 iniPos;                 //初始位置public float sensitivity = 2;          //灵敏度  ---这里越大 灵敏度越小private float m_radius;                 //盘子半径public float moveInfo;                  //摇杆移动的百分比public Vector2 moveDir;                 //摇杆移动的方向public float trigger_width;public float trigger_height;// Use this for initializationvoid Start () {//图形部分click_limit = gameObject.AddComponent(typeof(Image)) as Image;click_limit.sprite = limit_sprite;click_limit.color = new Color(0, 0, 0, 0.1f);RectTransform triggerRect = GetComponent<RectTransform>();triggerRect.anchorMax = Vector2.zero;triggerRect.anchorMin = Vector2.zero;triggerRect.pivot = Vector2.one;triggerRect.anchoredPosition = new Vector2(trigger_width, trigger_height);triggerRect.sizeDelta = new Vector2(trigger_width, trigger_height);chassis = CreateChild("Chassis",this.transform,E_JoystickComponent.chassis);joystick = CreateChild("Joystick", chassis.transform,E_JoystickComponent.joystick);}//1)触发区域//2)触发情况//3)放置摇杆//4)摇杆拖动//5)限制摇杆摆放位置//6)边缘位置 直接拖动public void Update(){if (Input.touches.Length > 0){Touch touch = Input.GetTouch(0);//检测上一次的触发是否结束//结束->检验此次按压的位置是否处于检测区域->放置摇杆//非结束->拖动给杆心if (nowTouch.phase == TouchPhase.Ended){Vector2 edge = transform.GetComponent<RectTransform>().anchoredPosition;if (touch.position.x < edge.x && touch.position.y < edge.y){nowTouch = touch;chassis.transform.position = touch.position;prePos = touch.position;CheckIsEdge();}}else{joystick.GetComponent<RectTransform>().anchoredPosition = (touch.position - prePos) / sensitivity;if(joystick.GetComponent<RectTransform>().anchoredPosition.magnitude > m_radius){joystick.GetComponent<RectTransform>().anchoredPosition = joystick.GetComponent<RectTransform>().anchoredPosition.normalized * m_radius;}}//检测抬手//摇杆杆心返回//摇杆返回初始点//将上一状态的触摸信息设置成触摸离开if (touch.phase == TouchPhase.Ended){nowTouch.phase = TouchPhase.Ended;chassis.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;joystick.GetComponent<RectTransform>().anchoredPosition = iniPos;}//刷新当前按压位置的信息//s = touch.position.ToString();}if ((joystick.GetComponent<RectTransform>().anchoredPosition.normalized * m_radius).magnitude > 0)moveInfo = joystick.GetComponent<RectTransform>().anchoredPosition.magnitude / (joystick.GetComponent<RectTransform>().anchoredPosition.normalized * m_radius).magnitude;elsemoveInfo = 0;moveDir = joystick.GetComponent<RectTransform>().anchoredPosition.normalized;}//判断是否触碰触发区边缘,并作相应调整private void CheckIsEdge(){float y = Mathf.Abs(transform.position.y - chassis.transform.position.y);float x = Mathf.Abs(transform.position.x - chassis.transform.position.x);//判断摇杆盘底是否触碰触发区上下边缘if (y > transform.position.x - m_radius){chassis.transform.position = new Vector3(chassis.transform.position.x, m_radius, chassis.transform.position.z);}else if (y < m_radius){chassis.transform.position = new Vector3(chassis.transform.position.x, chassis.transform.position.y - m_radius, chassis.transform.position.z);}//判断摇杆底盘是否触碰触发区域左右边缘if (x > transform.position.x - m_radius){chassis.transform.position = new Vector3(m_radius, chassis.transform.position.y, chassis.transform.position.z);}else if (x < m_radius){chassis.transform.position = new Vector3(chassis.transform.position.x - m_radius, chassis.transform.position.y, chassis.transform.position.z);}}/// <summary>/// 生成对象并建立关系/// </summary>/// <param name="name"></param>/// <param name="parent"></param>/// <returns></returns>GameObject CreateChild(string name,Transform parent,E_JoystickComponent joystickComponent){GameObject gameObject = new GameObject(name);gameObject.transform.SetParent(parent);Image image;RectTransform rectTransform;Vector2 triggerRect = this.GetComponent<RectTransform>().sizeDelta;switch (joystickComponent){case E_JoystickComponent.chassis:image = gameObject.AddComponent(typeof(Image)) as Image;image.sprite = chassis_sprite;image.color = Color.black;rectTransform = image.gameObject.GetComponent<RectTransform>();rectTransform.anchoredPosition = Vector2.zero;rectTransform.sizeDelta = new Vector2(triggerRect.x * 2 / 7, triggerRect.y * 2 / 7);m_radius = triggerRect.x * 2 / 7 / 2;prePos = rectTransform.position;break;case E_JoystickComponent.joystick:image = gameObject.AddComponent(typeof(Image)) as Image;image.sprite = joystick_sprite;image.color = Color.red;rectTransform = image.gameObject.GetComponent<RectTransform>();rectTransform.anchoredPosition = Vector2.zero;rectTransform.sizeDelta = new Vector2(triggerRect.x  / 7, triggerRect.y / 7);iniPos = rectTransform.anchoredPosition;break;}return gameObject;}}


演示:

魅蓝 note5  Unity Remote 5 的录屏情况

这篇关于【功能实现】手机游戏虚拟摇杆功能实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/843907

相关文章

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q

Python实现批量提取BLF文件时间戳

《Python实现批量提取BLF文件时间戳》BLF(BinaryLoggingFormat)作为Vector公司推出的CAN总线数据记录格式,被广泛用于存储车辆通信数据,本文将使用Python轻松提取... 目录一、为什么需要批量处理 BLF 文件二、核心代码解析:从文件遍历到数据导出1. 环境准备与依赖库

linux下shell脚本启动jar包实现过程

《linux下shell脚本启动jar包实现过程》确保APP_NAME和LOG_FILE位于目录内,首次启动前需手动创建log文件夹,否则报错,此为个人经验,供参考,欢迎支持脚本之家... 目录linux下shell脚本启动jar包样例1样例2总结linux下shell脚本启动jar包样例1#!/bin