-
-
Save neon-izm/addc864a7704613df9a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class GearVRHelper : MonoBehaviour{ | |
//タップ・フリック検出用の | |
public delegate void TrackPad(float x, float y); | |
//戻るボタンの短押しと、長押し用 | |
public delegate void Trigger(); | |
//戻るボタンの長押し検出時間 | |
public const float holdDetectTime = 2; | |
//タップ・フリック検出時間 | |
public const float tapDetectTime = 2; | |
//タップ検出距離 | |
public const float tapDetectLength = 2; | |
private const float tapDetectLengthSqrt = tapDetectLength*tapDetectLength; | |
private Vector3 currentPos; | |
private float holdTime; | |
private bool isDrag; | |
private Vector3 tapPos; | |
private float tapTime; | |
public static event Trigger OnBackPush , OnBackHold; | |
public static event Trigger OnTap; | |
public static event TrackPad OnFlick, OnDrag; | |
public void Awake(){ | |
OnBackPush = delegate { }; | |
OnBackHold = delegate { }; | |
OnTap = delegate { }; | |
OnFlick = delegate { }; | |
OnDrag = delegate { }; | |
} | |
private void Update(){ | |
//戻るボタンの短押しと、長押し | |
if (Input.GetKey(KeyCode.Escape)){ | |
//戻るボタン | |
var oldHoldTime = holdTime; | |
holdTime += Time.deltaTime; | |
if (oldHoldTime < holdDetectTime && holdTime > holdDetectTime){ | |
//長押ししたとみなす | |
OnBackHold(); | |
} | |
} | |
else if (Input.GetKeyUp(KeyCode.Escape)){ | |
if (holdTime < holdDetectTime){ | |
OnBackPush(); | |
} | |
} | |
else{ | |
holdTime = 0; | |
} | |
//タッチパッドのタップ・ドラッグ・フリック | |
var mousePos = Input.mousePosition; | |
if (Input.GetMouseButtonDown(0)){ | |
isDrag = true; | |
tapPos = mousePos; | |
tapTime = Time.time; | |
currentPos = mousePos; | |
} | |
else if (Input.GetMouseButton(0)){ | |
var d = currentPos - mousePos; | |
if (isDrag && d.sqrMagnitude > tapDetectLengthSqrt) { | |
if (d.x*d.x > d.y*d.y){//xの方が移動量が多い | |
OnDrag(d.x, 0); | |
} | |
else{//y軸の方が移動量が多い | |
OnDrag(0, d.y); | |
} | |
} | |
currentPos = mousePos; | |
} | |
else{ | |
if (isDrag) {//PULL | |
if (Time.time - tapTime < tapDetectTime){ | |
var d = tapPos - mousePos; | |
if (d.sqrMagnitude < tapDetectLengthSqrt) { | |
OnTap(); | |
} | |
else{ | |
if (d.x*d.x > d.y*d.y){ | |
OnFlick(d.x, 0); | |
} | |
else{ | |
OnFlick(0, d.y); | |
} | |
} | |
} | |
} | |
isDrag = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment