kazumalab tech log

流行りとリラックマと嵐が大好きです。技術的ログ。

Unity 5.4.0p1 + GearVRでの開発メモ

かずまです。


今回Unityのバージョン5.3から5.4に上げた時に躓いた点をメモしておきます。
起こったエラーはCanvas->Buttonをクリックしようとした時に認識されないという問題です。

PointerEventData pointer = new PointerEventData (EventSystem.current);
		pointer.position = new Vector3 ((float)Screen.width / 2f, (float)Screen.height / 2f, 0);

以前はこのようなコードを書いていました。
このコードはスクリーンの中心からポインターを飛ばして当たったオブジェクト(UI)に対してどういう動きをするというコードでした。
PCだと動きますがGearVRにビルドすると全く動かないという問題に直面しました。

そこで

using UnityEngine.VR // この行を一番上に追加

PointerEventData pointer = new PointerEventData (EventSystem.current);
		pointer.position = new Vector3 ((float)VRSettings.eyeTextureWidth / 2f, (float)VRSettings.eyeTextureHeight / 2f, 0);

これで動きました。

テストでPCで動かす場合もあるので

#if UNITY_ANDROID && !UNITY_EDITOR
		pointer.position = new Vector3 ((float)VRSettings.eyeTextureWidth / 2f, (float)VRSettings.eyeTextureHeight / 2f, 0);
#elif UNITY_EDITOR
		pointer.position = new Vector3 ((float)Screen.width / 2f, (float)Screen.height / 2f, 0);
#endif

とするといいかもしれませんね。