Skip to content

Instantly share code, notes, and snippets.

@divide-by-zero
Last active January 5, 2017 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save divide-by-zero/c0980b9dc87b0ec39a41 to your computer and use it in GitHub Desktop.
Save divide-by-zero/c0980b9dc87b0ec39a41 to your computer and use it in GitHub Desktop.
GearVR用
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;
}
}
}
@suzuki-katsuyuki
Copy link

Readme

使用するときは適当なGameObjectにこのScriptをアタッチして

using UnityEngine;

public class TouchEventTest : MonoBehaviour {
    void Start () {
        GearVRHelper.OnBackHold += () => {
            Debug.Log("BACK HOLD!");
        };
        GearVRHelper.OnBackPush += () => {
            Debug.Log("BACK PUSH!");
        };
        GearVRHelper.OnTap += () => {
            Debug.Log("Tap!");
        };
        GearVRHelper.OnFlick += (x, y) => {
            Debug.Log("Flick:" + x + "," + y);
        };
        GearVRHelper.OnDrag += (x, y) => {
            Debug.Log("Drag:" + x + "," + y);
        };
    }
}

のようにStatic領域のeventに追加。

注意

eventをStaticにした関係上Awakeにeventのクリア処理が入っており、Awakeでイベント追加しても順番的に消されてしまうかもしれないので、Startでの追加推奨。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment