[Unity3d for android]屏幕触摸事件
admin
2023-01-20 20:00:55
0

移动物体:

[csharp] view plaincopy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class example : MonoBehaviour {  
  5.     public float speed = 0.1F;  
  6.     void Update() {  
  7.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {  
  8.             Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;  
  9.             transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);  
  10.         }  
  11.     }  
  12. }  


点击碰撞克隆

[csharp] view plaincopy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class example : MonoBehaviour {  
  5.     public GameObject projectile;  
  6.     void Update() {  
  7.         int i = 0;  
  8.         while (i < Input.touchCount) {  
  9.             if (Input.GetTouch(i).phase == TouchPhase.Began)  
  10.                 clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject;  
  11.               
  12.             ++i;  
  13.         }  
  14.     }  
  15. }  


 

===================

[csharp] view plaincopy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class example : MonoBehaviour {  
  5.     public GameObject particle;  
  6.     void Update() {  
  7.         int i = 0;  
  8.         while (i < Input.touchCount) {  
  9.             if (Input.GetTouch(i).phase == TouchPhase.Began) {  
  10.                 Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);  
  11.                 if (Physics.Raycast(ray))  
  12.                     Instantiate(particle, transform.position, transform.rotation) as GameObject;  
  13.                   
  14.             }  
  15.             ++i;  
  16.         }  
  17.     }  
  18. }  


 

TouchPhase Enumeration  

Describes phase of a finger touch.

Values

Began

A finger touched the screen.

Moved

A finger moved on the screen.

Stationary

A finger is touching the screen but hasn't moved.

Ended

A finger was lifted from the screen. This is the final phase of a touch.

Canceled

The system cancelled tracking for the touch, as when (for example) the user puts the device to her face or more than five touches happened simultaneously. This is the final phase of a touch.

 

 

相关内容

热门资讯

失控的监护“外包”:孩子被送特... 从封闭式特训机构出来三个月后,李雨有时仍感觉自己还在里面。她忍不住看相关新闻。她看到报道中,一名21...
一举创下3项世界纪录,中国自主... 【环球时报报道 记者 郭媛丹】21日上午,中船集团旗下中船澄西船舶修造有限公司为力拓集团建造的自主研...
试图把国内矛盾外引?专家解析菲... 【环球时报报道 记者 樊巍】21日上午,外交部边海司负责人就菲律宾在仁爱礁蓄意挑衅、袭击中国海警执法...
特朗普宣布两年后对仿制药征收1... △美国总统特朗普(资料图)当地时间7月21日,美国总统特朗普在社交媒体发文表示,自2026年8月1日...
美国贸易代表暗示将很快出台新关... 新华社纽约7月21日电(记者刘亚南) 美国贸易代表贾米森·格里尔21日在接受媒体采访时暗示,美国联邦...
中国空调“一机难求”,人民日报... 中国空调“一机难求”,创意产品持续热销,本报记者实地探访——消暑降温,欧洲民众为何追捧中国制造?(第...
美防长:对伊朗战争已耗资375... △美国国防部长赫格塞思(资料图)央视记者当地时间21日获悉,美国国防部长赫格塞思表示,美国对伊朗战争...
在全国抗议声中,泽连斯基解除乌... 据乌克兰媒体《基辅独立报》报道,乌克兰总统泽连斯基解除了乌克兰武装部队总司令瑟尔斯基的职务,并任命联...
也门胡塞武装:停靠沙特港口船只... 新华社科威特城7月21日电(记者尹炣)萨那消息:也门胡塞武装20日警告国际航运公司称,与沙特港口开展...
特朗普:胡塞武装若封锁红海,美... 新华社华盛顿7月21日电(记者黄强 徐剑梅)美国总统特朗普21日在白宫会见到访的黎巴嫩总统奥恩时表示...