[Unity3d for android]屏幕触摸事件

简介: 移动物体: [csharp] view plaincopy using UnityEngine;   using System.

移动物体:

[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.

 

 

相关文章
|
Java Android开发 图形学
unity 调用android的震动
在unity中调用震动一般有两种方式: 1.使用unity自带的 Handheld.Vibrate();优点:方便、简单缺点:无法控制震动的频率 2.自己导入jar包到unity中优点:便于后期的调节震动的频率缺点:如果是初步接触android studio可能比较麻烦 这次就不说Handheld.Vibrate(),直接调用就ok了。
3297 0
|
Android开发
android:windowIsTranslucent 导致 Activity切换动画无效
android:windowIsTranslucent 导致 Activity切换动画无效
428 0
|
Android开发
Android 横屏全屏方法
Android 横屏全屏方法
173 0
|
Android开发
Android触摸事件传递机制(一)
在开发过程中会经常遇到View与ViewGroup嵌套的问题,如ViewPager嵌套Fragment,而Fragment中又需要实现一个广告滑动,此时广告滑动就会与ViewPager的滑动事件产生冲突,而深入理解Android触摸事件的传递机制则是解决问题的关键。
1410 0
|
API Android开发
Android屏幕窗口各部分超详细介绍
目前,android屏幕上大致分成三个部分:通知栏,内容区、虚拟导航栏(NavigatorBar) 因为这三个区域引起的问题数不胜数,必须好好总结一下
634 0
|
Android开发
Android Studio 滚动视图ScrollView使用与全屏实例
本文目录 1. 使用场景 2. 注册页面实例 3. ScrollView填满窗口
934 0
Android Studio 滚动视图ScrollView使用与全屏实例