Unity Android设备的输入

Unity Android设备的输入

1依据屏幕位置输入

有的时候也许是为了整个有些风格的干净,减少屏幕上的UI图标,以至于摒弃了虚拟按键这种常用的输入方式。为了替代虚拟按键的输入方式而选择了依据点击事件发生在屏幕中的位置而控制对象的移动等。比如将整个手机屏幕划分为左右两个区域,左边区域负责移动控制,右边区域负责技能释放。

这里我们需要处理的问题分别是点击事件的获取及屏幕信息的获取,下面这两个方面的API文档。

点击事件:http://wiki.ceeger.com/script/unityengine/classes/touch/touch

屏幕信息:http://wiki.ceeger.com/script/unityengine/classes/screen/screen

 

 1 void Update () {
 2 
 3         if (Input.touchCount > 0)//判断是否有触摸信息
 4         {
 5             for (int i = 0; i < Input.touchCount; i++)
 6             {
 7                 Touch touch = Input.touches[i];//将触摸信息传递给touch
 8 
 9                 if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)//touch.phase是触摸的相位信息,通过TouchPhase枚举中来确定当前的状态,Began:一个手指开始接触;Moved:一个手指发生了移动;Stationary:一个手指接触了屏幕但是没有发生移动;Ended:一个手指离开屏幕,也是最后的状态;Cnaceled:系统取消对触摸监听,如用户把屏幕放到他脸上或超过五个接触同时发生。
10                 {
11                     touchPosition = touch.position;//获取触摸的位置信息,该坐标与屏幕坐标一致。
12                     if (touchPosition.x < screenWidth/4)//screenWidth在Start中已经赋值=>screenWidth = Screen.width;
13                     {
14                         SomeMethod1();//某方法1
15                         beeTransform.Translate(new Vector3(-MoveSpeed, -DownSpeed, 0),Space.World);    //beeTransform是游戏对象的位置信息,已在Start中赋值
16                     }
17                     else if (touchPosition.x>=screenWidth/4&& touchPosition.x <screenWidth / 2)
18                     {
19                        SomeMethod1();
20                         beeTransform.Translate(new Vector3(MoveSpeed, -DownSpeed, 0), Space.World);
21 
22                     }
23                 }
24                 else if (touch.phase == TouchPhase.Ended)
25                    SomeMethod2();//某方法
26             }
27         }
28 
29         if (Input.touchCount == 0)//未触摸时
30            SomeMethod2();
31     }

 

转载于:https://www.cnblogs.com/Firepad-magic/p/5967703.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值