Last active
June 19, 2019 07:36
-
-
Save shiena/d2cbd2bd100cc9ed7db4b4dfd608feca to your computer and use it in GitHub Desktop.
GoogleVRのGesture C APIをUnityから使うためのコード
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
/* | |
* Copyright (c) 2019 KOGA Mitsuhiro | |
* | |
* This software is released under the MIT License. | |
* http://opensource.org/licenses/mit-license.php | |
*/ | |
using System; | |
using System.Runtime.InteropServices; | |
namespace Gvr.Gesture | |
{ | |
public enum GvrGestureDirection | |
{ | |
Up = 1, | |
Down = 2, | |
Left = 3, | |
Right = 4 | |
} | |
public enum GvrGestureType | |
{ | |
Swipe = 1, | |
ScrollStart = 2, | |
ScrollUpdate = 3, | |
ScrollEnd = 4 | |
} | |
public enum GvrControllerButton | |
{ | |
Click = 1, | |
Count = 9 | |
} | |
[StructLayout(LayoutKind.Sequential)] | |
public struct GvrVec2f | |
{ | |
public float x; | |
public float y; | |
} | |
public class AndroidNativeGesture | |
{ | |
public const int GvrControllerEnableOrientation = 1 << 0; | |
public const int GvrControllerEnableTouch = 1 << 1; | |
public const int GvrControllerEnableGyro = 1 << 2; | |
public const int GvrControllerEnableAccel = 1 << 3; | |
public const int GvrControllerEnableGestures = 1 << 4; | |
public const int GvrControllerEnablePosePrediction = 1 << 5; | |
#if UNITY_ANDROID && !UNITY_EDITOR | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern IntPtr gvr_controller_state_create(); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern void gvr_controller_state_update(IntPtr api, int controller_index, IntPtr out_state); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern void gvr_controller_state_destroy(ref IntPtr state); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern int gvr_controller_get_default_options(); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern IntPtr gvr_controller_create_and_init(int options, IntPtr context); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern void gvr_controller_destroy(ref IntPtr api); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern void gvr_controller_pause(IntPtr api); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern void gvr_controller_resume(IntPtr api); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern IntPtr gvr_gesture_context_create(); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern void gvr_gesture_context_destroy(ref IntPtr gvrGestureContextPtr); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern int gvr_gesture_get_count(IntPtr gvrGestureContextPtr); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern GvrGestureDirection gvr_gesture_get_direction(IntPtr gvrGesturePtr); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern GvrVec2f gvr_gesture_get_displacement(IntPtr gvrGesturePtr); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern GvrGestureType gvr_gesture_get_type(IntPtr gvrGesturePtr); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern GvrVec2f gvr_gesture_get_velocity(IntPtr gvrGesturePtr); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern void gvr_gesture_restart(IntPtr gvrGestureContextPtr); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern void gvr_gesture_update(IntPtr gvrControllerStatePtr, IntPtr gvrGestureContextPtr); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern IntPtr gvr_gesture_get(IntPtr gvrGestureContextPtr, int index); | |
[DllImport(GvrActivityHelper.GVR_DLL_NAME)] | |
public static extern bool gvr_get_button_long_press(IntPtr gvrControllerStatePtr, IntPtr gvrGestureContextPtr, Gvr.Gesture.GvrControllerButton gvrControllerButton); | |
#endif // UNITY_ANDROID && !UNITY_EDITOR | |
} | |
} |
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
/* | |
* Copyright (c) 2019 KOGA Mitsuhiro | |
* | |
* This software is released under the MIT License. | |
* http://opensource.org/licenses/mit-license.php | |
*/ | |
using System; | |
using System.Collections.Generic; | |
using Gvr.Gesture; | |
using UnityEngine; | |
using UnityEngine.XR; | |
public class GvrGesture : MonoBehaviour | |
{ | |
private IntPtr _api = IntPtr.Zero; | |
private IntPtr _gvrGestureContextPtr = IntPtr.Zero; | |
private IntPtr _controllerState = IntPtr.Zero; | |
public struct Gesture | |
{ | |
public GvrGestureType GestureType; | |
public GvrGestureDirection GestureDirection; | |
public GvrVec2f Displacement; | |
public GvrVec2f Velocity; | |
} | |
#if UNITY_ANDROID && !UNITY_EDITOR | |
private static bool IsDayDream() | |
{ | |
return XRSettings.loadedDeviceName == GvrSettings.VR_SDK_DAYDREAM; | |
} | |
GvrGesture() | |
{ | |
if (!IsDayDream()) | |
{ | |
return; | |
} | |
var options = AndroidNativeGesture.gvr_controller_get_default_options(); | |
options |= AndroidNativeGesture.GvrControllerEnableGestures; | |
_api = AndroidNativeGesture.gvr_controller_create_and_init(options, XRDevice.GetNativePtr()); | |
if (IntPtr.Zero == _api) | |
{ | |
return; | |
} | |
AndroidNativeGesture.gvr_controller_resume(_api); | |
_controllerState = AndroidNativeGesture.gvr_controller_state_create(); | |
_gvrGestureContextPtr = AndroidNativeGesture.gvr_gesture_context_create(); | |
} | |
~GvrGesture() | |
{ | |
if (!IsDayDream()) | |
{ | |
return; | |
} | |
if (IntPtr.Zero != _gvrGestureContextPtr) | |
{ | |
AndroidNativeGesture.gvr_gesture_context_destroy(ref _gvrGestureContextPtr); | |
} | |
if (IntPtr.Zero != _controllerState) | |
{ | |
AndroidNativeGesture.gvr_controller_state_destroy(ref _controllerState); | |
} | |
if (IntPtr.Zero != _api) | |
{ | |
AndroidNativeGesture.gvr_controller_destroy(ref _api); | |
} | |
} | |
private void OnApplicationPause(bool pauseStatus) | |
{ | |
if (!IsDayDream()) | |
{ | |
return; | |
} | |
if (IntPtr.Zero == _api) | |
{ | |
return; | |
} | |
if (pauseStatus) | |
{ | |
AndroidNativeGesture.gvr_controller_pause(_api); | |
} | |
else | |
{ | |
AndroidNativeGesture.gvr_controller_resume(_api); | |
} | |
} | |
#endif // UNITY_ANDROID && !UNITY_EDITOR | |
private static readonly List<Gesture> Empty = new List<Gesture>(0); | |
public List<Gesture> GetGesture(int controllerIndex = 0) | |
{ | |
#if UNITY_ANDROID && !UNITY_EDITOR | |
if (!IsDayDream()) | |
{ | |
return Empty; | |
} | |
if (IntPtr.Zero != _gvrGestureContextPtr) | |
{ | |
AndroidNativeGesture.gvr_controller_state_update(_api, controllerIndex, _controllerState); | |
if (IntPtr.Zero != _controllerState) | |
{ | |
AndroidNativeGesture.gvr_gesture_update(_controllerState, _gvrGestureContextPtr); | |
var numGestures = AndroidNativeGesture.gvr_gesture_get_count(_gvrGestureContextPtr); | |
if (numGestures > 0) | |
{ | |
var gestures = new List<Gesture>(numGestures); | |
for (var i = 0; i < numGestures; i++) | |
{ | |
var gesturePtr = AndroidNativeGesture.gvr_gesture_get(_gvrGestureContextPtr, i); | |
var gesture = new Gesture | |
{ | |
GestureType = AndroidNativeGesture.gvr_gesture_get_type(gesturePtr), | |
GestureDirection = AndroidNativeGesture.gvr_gesture_get_direction(gesturePtr), | |
Displacement = AndroidNativeGesture.gvr_gesture_get_displacement(gesturePtr), | |
Velocity = AndroidNativeGesture.gvr_gesture_get_velocity(gesturePtr) | |
}; | |
gestures.Add(gesture); | |
} | |
return gestures; | |
} | |
} | |
} | |
#endif // UNITY_ANDROID && !UNITY_EDITOR | |
return Empty; | |
} | |
public bool LongPressed(int controllerIndex = 0, Gvr.Gesture.GvrControllerButton button = Gvr.Gesture.GvrControllerButton.Click) | |
{ | |
#if UNITY_ANDROID && !UNITY_EDITOR | |
if (!IsDayDream()) | |
{ | |
return false; | |
} | |
AndroidNativeGesture.gvr_controller_state_update(_api, controllerIndex, _controllerState); | |
if (IntPtr.Zero != _controllerState) | |
{ | |
AndroidNativeGesture.gvr_gesture_update(_controllerState, _gvrGestureContextPtr); | |
return AndroidNativeGesture.gvr_get_button_long_press(_controllerState, _gvrGestureContextPtr, button); | |
} | |
#endif // UNITY_ANDROID && !UNITY_EDITOR | |
return false; | |
} | |
public void Restart() | |
{ | |
#if UNITY_ANDROID && !UNITY_EDITOR | |
if (!IsDayDream()) | |
{ | |
return; | |
} | |
AndroidNativeGesture.gvr_gesture_restart(_gvrGestureContextPtr); | |
#endif // UNITY_ANDROID && !UNITY_EDITOR | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment