Unity 编写类似神庙逃亡手势

news/2025/2/2 3:02:10/

1、首先我们先定义一个枚举,用来表示手势滑动的四个方向:


<span style="font-size:14px;">public enum TouchDirection
{ Unkown,Left,Right,Up,Down
}</span>

2、定义类:TouchInput
<span style="font-size:14px;">public class TouchInput : MonoBehaviour {public static TouchInput instance;private Vector2 touchBeginPos;private Vector2 touchEndPos;
}
</span>

3、对instance进行初始化:
<span style="font-size:14px;">void Start () {Init();}void Init(){if (instance == null){instance = this;}}</span>

4、编写根据Touch计算滑动方向:
<span style="font-size:14px;">public TouchDirection GetTouchMoveDirection(){if (Input.touchCount > 0){TouchDirection dir = TouchDirection.Unkown;if (Input.touches[0].phase != TouchPhase.Canceled){switch (Input.touches[0].phase){ case TouchPhase.Began:touchBeginPos = Input.touches[0].position;break;case TouchPhase.Ended:touchEndPos = Input.touches[0].position;if (Mathf.Abs(touchBeginPos.x - touchEndPos.x) > Mathf.Abs(touchBeginPos.y - touchEndPos.y)){if (touchBeginPos.x > touchEndPos.x){dir = TouchDirection.Left;}else{dir = TouchDirection.Right;}}else{if (touchBeginPos.y > touchEndPos.y){dir = TouchDirection.Down;}else{dir = TouchDirection.Up;}}break;}}return dir;}else{return TouchDirection.Unkown;}}</span>

5、使用方法:

首先,我们必须把我们写好的TouchInput脚本挂在物体上实例,在需要用到的地方应用:

void Update () {TouchInputTest();}
void TouchInputTest(){switch (TouchInput.instance.GetTouchMoveDirection()){case TouchDirection.Up:Debug.Log("Up");break;case TouchDirection.Down:Debug.Log("Down");break;case TouchDirection.Left:Debug.Log("Left");break;case TouchDirection.Right:Debug.Log("Right");break;}}

这样不论在什么地方,只要我们需要,我们就可以实例TouchInput.instance,进而拿到我们的滑动方向。



http://www.ppmy.cn/news/382600.html

相关文章

WP8手机安装《神庙逃亡》的教程

虽然神庙逃亡的最新版宣称支持512RAM的WP8手机&#xff0c;但大部分童鞋测试结果依然悲催&#xff0c;今天新锋网小编就为您带来如何顺利安装神庙逃亡的教程&#xff01; 本方法必须使用手机WIFI&#xff1a; 1.请关闭手机的商城&#xff08;如果已经打开过&#xff0c;先切回去…

神庙逃亡破解分析

分析目标:破解神庙逃亡内购 一.神庙逃亡网络验证破解 首先用Android Killer载入, 然后编译,运行,发现提示盗版软件 继续搜索dialog_initconfig_msg a方法show弹出了警告框,其主要是这个arg4.d为校验值 向上层追踪来源于这里赋值,但是这个只没法做交叉引用,只好用堆栈法来进行跟…

Problem 2121 神庙逃亡(FZU)

Problem 2121 神庙逃亡 Accept: 700 Submit: 1788 Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description 话说最近穷猫猫LKity意外得到了一部ANDROID手机&#xff0c;于是&#xff0c;LKity兴奋地为自己的新机子安装了神往已久的游戏——神庙逃亡&#xf…

神庙逃亡——单纯的物理题

Description 话说最近穷猫猫LKity意外得到了一部ANDROID手机&#xff0c;于是&#xff0c;LKity兴奋地为自己的新机子安装了神往已久的游戏——神庙逃亡&#xff08;Temple Run&#xff09;。可惜&#xff0c;LKity不仅仅是一只穷猫猫&#xff0c;更是一只笨猫猫。每次她玩这款…

delphi XE模拟Android手机PDA设备的虚拟键盘按键及扫码过程输入焦点及信号接收

delphi XE模拟Android手机PDA设备的虚拟键盘按键及扫码过程输入焦点及信号接收 今天&#xff0c;群里有几位同学讨论这个问题&#xff0c;汇总了一下&#xff0c;分享出来&#xff0c;供同学们学习研究。 一、直接上代码示例 1、屏蔽应用Terminated &#xff1b;如果按了虚拟…

[FOJ 2121] 神庙逃亡

题目描述 神庙逃亡 解题思路 参考代码 #include <stdio.h> int main() {int n;scanf("%d",&n);while (n--){int s,h,vx,vy,t;scanf("%d %d %d %d",&s,&h,&vx,&vy);t s/vx;printf("%s\n",(vy*t - 5*t*t) > h?…

简析神庙逃亡的游戏元素与成功因素

目录 简单介绍游戏背景 游戏设计元素玩家游戏目标&#xff08;goals&#xff09;过程&#xff08;procedures) 规则与行动&#xff08;rules and actions)规则&#xff08;rules)行动&#xff08;actions)道具/资源&#xff08;resources)边界&#xff08;boundary)结果&#x…

神庙逃亡

Problem A 神庙逃亡 Accept: 214 Submit: 551 Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description 话说最近穷猫猫LKity意外得到了一部ANDROID手机&#xff0c;于是&#xff0c;LKity兴奋地为自己的新机子安装了神往已久的游戏——神庙逃亡&#xff08…