SHARE
TWEET

Untitled

a guest Sep 20th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Linq;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.SceneManagement;
  6. using UnityEngine.UI;
  7.  
  8. /// <summary>
  9. /// エディタ上でのアプリ起動時に自動実行されるデバッグ用スクリプト
  10. /// VRアプリ向けになっているEventSystemをEditor上からも操作できる形に組み替える
  11. /// </summary>
  12. public class ReplaceEventSystems
  13. {
  14.     const float InitialHeight = 1.6f;
  15.  
  16.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
  17.     static void Init()
  18.     {
  19.         Replace();
  20.         SceneManager.sceneLoaded += OnSceneLoaded;
  21.     }
  22.     static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  23.     {
  24.         Replace();
  25.     }
  26.     static void Replace()
  27.     {
  28.         // VR用シーンでなさそうならreturn
  29.         if (GameObject.FindObjectOfType<OVRCameraRig>() == null) { return; }
  30.  
  31.         // カメラ位置の調整
  32.         var ovrManager = GameObject.FindObjectOfType<OVRManager>();
  33.         if (ovrManager != null && ovrManager.trackingOriginType == OVRManager.TrackingOrigin.FloorLevel)
  34.         {
  35.             Camera.main.transform.position = Camera.main.transform.position + Vector3.up * InitialHeight;
  36.         }
  37.         // カメラにキーボード操作用コンポーネントを追加(必要なければ消してください)
  38.         Camera.main.gameObject.AddComponent<EditorCamera>();
  39.  
  40.         // OVRInputModuleを無効 かつ StandaloneInputModuleを有効に
  41.         var eventSystem = GameObject.FindObjectOfType<EventSystem>();
  42.         eventSystem.GetComponent<OVRInputModule>().enabled = false;
  43.         eventSystem.gameObject.AddComponent<StandaloneInputModule>();
  44.  
  45.         // Project中の全Canvasコンポーネントの中からAssets以下でないもの(=Hierarchy上のもの)を全て習得
  46.         var canvases = Resources.FindObjectsOfTypeAll<Canvas>()
  47.         .Where(c => AssetDatabase.GetAssetOrScenePath(c).Contains(".unity")).ToArray();
  48.         // GraphicRaycasterがあれば有効に、なければ追加
  49.         // OVRRaycasterは競合しないので特にdisableにしていない
  50.         foreach (var canvas in canvases)
  51.         {
  52.             var raycaster = canvas.gameObject.GetComponent<GraphicRaycaster>();
  53.             if (raycaster != null && raycaster.GetType() == typeof(GraphicRaycaster))
  54.             {
  55.                 raycaster.enabled = true;
  56.             }
  57.             else
  58.             {
  59.                 canvas.gameObject.AddComponent<GraphicRaycaster>();
  60.             }
  61.         }
  62.     }
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top