Skip to content

Instantly share code, notes, and snippets.

@nkjzm
Last active July 25, 2018 10:57
Show Gist options
  • Save nkjzm/1554b679989f68d36606b9ad3cdce1f1 to your computer and use it in GitHub Desktop.
Save nkjzm/1554b679989f68d36606b9ad3cdce1f1 to your computer and use it in GitHub Desktop.
Google VR SDKの入力クラスをZenjectで抽象化する ref: https://qiita.com/splas_boomerang/items/06dfeccca506808b1cbf
[Inject]
IHandController HandController = null;
Container.Bind<[抽象クラス]>().To<[派生クラス]>().AsSingle();
public class GvrHandController : IHandController
{
Transform controller = null;
public Vector3 Position { get { return (controller ?? (controller = GameObject.FindObjectOfType<GvrTrackedController>().transform)).position; } }
public Quaternion Orientation { get { return GvrControllerInput.Orientation; } }
public Vector3 Gyro { get { return GvrControllerInput.Gyro; } }
public Vector3 Accel { get { return GvrControllerInput.Accel; } }
public bool IsTouching { get { return GvrControllerInput.IsTouching; } }
public bool TouchDown { get { return GvrControllerInput.TouchDown; } }
public bool TouchUp { get { return GvrControllerInput.TouchUp; } }
public Vector2 TouchPos { get { return GvrControllerInput.TouchPos; } }
public Vector2 TouchPosCentered { get { return GvrControllerInput.TouchPosCentered; } }
public bool Recentered { get { return GvrControllerInput.Recentered; } }
public bool ClickButton { get { return GvrControllerInput.ClickButton; } }
public bool ClickButtonDown { get { return GvrControllerInput.ClickButtonDown; } }
public bool ClickButtonUp { get { return GvrControllerInput.ClickButtonUp; } }
public bool AppButton { get { return GvrControllerInput.AppButton; } }
public bool AppButtonDown { get { return GvrControllerInput.AppButtonDown; } }
public bool AppButtonUp { get { return GvrControllerInput.AppButtonUp; } }
public bool HomeButtonDown { get { return GvrControllerInput.HomeButtonDown; } }
public bool HomeButtonState { get { return GvrControllerInput.HomeButtonState; } }
}
using Zenject;
public class HandControllerInstaller : MonoInstaller<HandControllerInstaller>
{
public override void InstallBindings()
{
Container.Bind<IHandController>().To<GvrHandController>().AsSingle();
}
}
using UnityEngine;
public interface IHandController
{
Vector3 Position { get; }
Quaternion Orientation { get; }
Vector3 Gyro { get; }
Vector3 Accel { get; }
bool IsTouching { get; }
bool TouchDown { get; }
bool TouchUp { get; }
Vector2 TouchPos { get; }
Vector2 TouchPosCentered { get; }
bool Recentered { get; }
bool ClickButton { get; }
bool ClickButtonDown { get; }
bool ClickButtonUp { get; }
bool AppButton { get; }
bool AppButtonDown { get; }
bool AppButtonUp { get; }
bool HomeButtonDown { get; }
bool HomeButtonState { get; }
}
public class OvrHandController : IHandController
{
// public bool ClickButton { get { return GvrControllerInput.ClickButton; } }
public bool ClickButton { get { return OVRInput.Get(OVRInput.Button.PrimaryTouchpad); } }
}
using UnityEngine;
using Zenject;
public class Test : MonoBehaviour
{
[Inject]
IHandController HandController = null;
void Update()
{
// 書き換え前
// if (GvrControllerInput.ClickButton)
// 書き換え後
if (HandController.ClickButton)
{
Debug.Log("ボタンがクリックされた");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment