Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class ExampleBehaviour : MonoBehaviour
- {
- private float mTouchBeginTime;
- private bool mTouchEnded = true;
- void Update()
- {
- DetectInput();
- }
- private void DetectInput()
- {
- if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) {
- // On mobile phones
- if (Input.touchCount == 1) {
- Touch touch = Input.GetTouch(0);
- if (touch.phase == TouchPhase.Began) {
- mTouchBeginTime = Time.time;
- mTouchEnded = false;
- } else if (touch.phase == TouchPhase.Moved) {
- mTouchEnded = true;
- } else if (touch.phase == TouchPhase.Stationary) {
- if (!mTouchEnded && Time.time - mTouchBeginTime > 1f) {
- OnLongPress();
- mTouchEnded = true;
- }
- } else if (touch.phase == TouchPhase.Ended) {
- if (!mTouchEnded && Time.time - mTouchBeginTime <= 1f) {
- OnShortPress();
- mTouchEnded = true;
- }
- }
- }
- } else {
- // On PC
- if (Input.GetMouseButtonDown(0)) {
- mTouchBeginTime = Time.time;
- mTouchEnded = false;
- }
- if (!mTouchEnded && Time.time - mTouchBeginTime > 1f) {
- OnLongPress();
- mTouchEnded = true;
- }
- if (Input.GetMouseButtonUp(0)) {
- if (!mTouchEnded && Time.time - mTouchBeginTime <= 1f) {
- OnShortPress();
- mTouchEnded = true;
- }
- }
- }
- }
- private void OnShortPress()
- {
- // Handle short press ...
- }
- private void OnLongPress()
- {
- // Handle long press ...
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement