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

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

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import