unity实现手游虚拟摇杆
作者:玉骨寒 时间:2021-11-23 07:16:44
本文实例为大家分享了unity实现手游虚拟摇杆的具体代码,供大家参考,具体内容如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 绑定到摇杆上的摇杆类,参考半径50
/// </summary>
public class Rocker : MonoBehaviour {
Vector2 m_offet;//偏移向量
Vector2 m_originalPos;//摇杆原始屏幕坐标
Touch[] touches;//屏幕上触控点数组
int touch_Id = -1;//触控点数组下标
bool isMove = false;//是否移动
float m_ScreenScale;
/// <summary>
/// 给外部调用的偏移向量,告知摇杆参数
/// </summary>
public Vector3 Offet
{
get
{
return m_offet;
}
}
// Use this for initialization
void Start () {
m_originalPos = transform.position;//摇杆中心的屏幕坐标位置
m_ScreenScale = Screen.width / 800f;
}
// Update is called once per frame
void Update () {
//得到屏幕触控数组
touches = Input.touches;
if (touches.Length > 0)//如果触点开启
{
//得到离摇杆中心最近的触点下标 touch_Id;
if (touches.Length == 1)//只有一个触点时
{
touch_Id = 0;
}
else if (touches.Length > 1)//触点大于1个时
{
touch_Id = 0;//先假设下标为0
for (int i = 1; i < touches.Length; i++)//遍历触点数组
{
if (Vector2.SqrMagnitude(touches[i].position - m_originalPos) < Vector2.SqrMagnitude(touches[touch_Id].position - m_originalPos))//第i个点比假设的点近
{
touch_Id = i;//假设的点改为第i个点
}
}
}
//如果得到的触点不是取消或抬起
if (Input.GetTouch(touch_Id).phase != TouchPhase.Canceled && Input.GetTouch(touch_Id).phase != TouchPhase.Ended)
{
//触点在摇杆范围内
if(Vector2.SqrMagnitude(touches[touch_Id].position - m_originalPos) <= 50*50 * m_ScreenScale * m_ScreenScale)//50为背景半径
{
isMove = true;//开启遥控
//摇杆开始控制,计算偏移量
SetOffetIn();
}
else if(isMove)//触点在摇杆范围外,但是遥控已经开启
{
SetOffetOut();
}
}
else// 手指抬起,摇杆回归原始位置
{
transform.position = m_originalPos;
m_offet = Vector2.zero;
isMove = false;
touch_Id = -1;
}
}
}
/// <summary>
/// 触点在操作盘内时
/// 摇杆控制方法
/// </summary>
void SetOffetIn()
{
//距离过小视为不偏移摇杆位置不变
if(Vector2.SqrMagnitude(touches[touch_Id].position - m_originalPos) < 5 * m_ScreenScale)
{
GetComponent<Image>().rectTransform.position = m_originalPos;//摇杆定位在原始位置
m_offet = Vector3.zero;
}
else
{
//摇杆位置追踪
GetComponent<Image>().rectTransform.position = touches[touch_Id].position;
m_offet = touches[touch_Id].position - m_originalPos;//赋值偏移值
m_offet = m_offet.normalized;//归一化
}
}
/// <summary>
/// 触点在操作盘外时
/// 摇杆控制方法
/// </summary>
void SetOffetOut()
{
Vector2 tempDir;//临时偏移向量
tempDir = touches[touch_Id].position - m_originalPos;
//更新摇杆位置:距离原始位置127各单位
GetComponent<Image>().rectTransform.position = m_originalPos + (tempDir.normalized) * 25*m_ScreenScale;
//偏移量
m_offet = tempDir.normalized;//归一化
}
private void OnGUI()
{
GUIStyle style = new GUIStyle(); //实例化一个新的GUIStyle,名称为style ,后期使用
style.fontSize = 50; //字体的大小设置数值越大,字越大,默认颜色为黑色
style.normal.textColor = new Color(1, 1, 1); //设置文本的颜色为 新的颜色(0,0,0)修改值-代表不同的颜色,值为整数 我个人觉得有点像RGB的感觉
GUI.Label(new Rect(20, 30, 300, 60), "原始位置:" + m_originalPos.ToString(),style);
GUI.Label(new Rect(20, 100, 300, 60), "摇杆位置:" + GetComponent<Image>().rectTransform.position.ToString(), style);
GUI.Label(new Rect(20, 170, 300, 60), "触点位置:" + touches[touch_Id].position.ToString(), style);
GUI.Label(new Rect(20, 240, 300, 60), "屏幕分辨率:" + Screen.currentResolution, style);
}
}
来源:https://blog.csdn.net/yanggenye/article/details/73087773
标签:unity,虚拟摇杆
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
javaWeb使用servlet搭建服务器入门
2023-11-21 04:47:45
Android 判断日期是否在一年以内的算法实例
2023-03-22 06:04:13
mybatis-plus update更新操作的三种方式(小结)
2023-10-08 14:05:08
![](https://img.aspxhome.com/file/2023/9/62639_0s.png)
Java项目开发命名规范(动力节点Java学院整理)
2021-06-24 07:16:56
spring aop注解配置代码实例
2022-02-16 21:13:33
Spring配置shiro时自定义Realm中属性无法使用注解注入的解决办法
2022-09-18 11:18:22
![](https://img.aspxhome.com/file/2023/2/83122_0s.jpg)
c#实现获取字符串阵列中元素最长或最短的长度
2022-08-22 08:54:01
![](https://img.aspxhome.com/file/2023/3/100203_0s.jpg)
android 大图片拖拽并缩放实现原理
2022-11-10 05:59:55
![](https://img.aspxhome.com/file/2023/6/100596_0s.png)
Java使用JDBC连接Oracle_MSSQL实例代码
2023-04-19 19:34:46
使用自定义注解+springAop实现参数非空校验方式
2023-09-21 00:13:22
![](https://img.aspxhome.com/file/2023/3/70263_0s.png)
初识Java环境变量配置及IDEA
2022-09-20 06:46:41
![](https://img.aspxhome.com/file/2023/5/78885_0s.jpg)
解决Android手机屏幕横竖屏切换
2022-10-21 18:55:33
![](https://img.aspxhome.com/file/2023/4/104644_0s.png)
java开发线上事故理解RocketMQ异步精髓
2023-07-25 07:41:20
![](https://img.aspxhome.com/file/2023/1/57501_0s.jpg)
Java解决计算相邻两个数的最大差值的问题
2022-03-29 05:47:20
![](https://img.aspxhome.com/file/2023/7/94307_0s.png)
Spring的refresh()方法相关异常解析
2021-12-08 07:39:07
C# 在PDF中添加墨迹注释Ink Annotation的步骤详解
2022-01-31 14:23:29
![](https://img.aspxhome.com/file/2023/8/89748_0s.png)
Java中volatile关键字的作用
2023-06-02 17:02:55
![](https://img.aspxhome.com/file/2023/1/108381_0s.png)
Java 获取网站图片的示例代码
2022-03-11 14:52:44
![](https://img.aspxhome.com/file/2023/9/67969_0s.jpg)
Visual C#类的定义及实现方法实例解析
2023-06-04 18:10:37
JAVA遍历Map集合的几种方法汇总
2021-09-01 12:34:07