Unity 屏幕触摸事件

该文内容摘抄整理:

移动物体:

[csharp]  view plain  copy
  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 plain  copy
  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 plain  copy
  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.

 

Unity事件系统由四个部分组成:EventSystem、BaseInputModule、BaseRaycaster和IEventSystemHandler。EventSystem是事件系统的核心部分,它提供了向游戏物体发送消息的功能。BaseInputModule是EventSystem的子类,负责处理输入信息,如键盘、鼠标、触摸和自定义输入事件。BaseRaycaster也是EventSystem的子类,用于确定事件发生的位置。最后,IEventSystemHandler接口用于监听和处理事件。 在Unity中,当我们在场景中创建任何UI对象时,系统会自动创建一个名为EventSystem的物体。该物体下有两个组件:EventSystem和StandaloneInputModule。EventSystem是事件系统的核心组件,而StandaloneInputModule是BaseInputModule的一个具体实现,负责处理键盘和鼠标事件。 需要注意的是,EventSystem本身被设计为一种管理器,而不是事件的具体处理者,所以在EventSystem的可调参数较少。它的主要作用是协调各个组件的配合,以实现事件的管理和触发。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Unity事件系统](https://blog.csdn.net/lwtbn1/article/details/72629304)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Unity 事件系统](https://blog.csdn.net/m0_63024355/article/details/130873811)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值