SpatialUIController.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using EZXR.Glass.SixDof;
  5. using EZXR.Glass.Core;
  6. using System;
  7. namespace EZXR.Glass.UI
  8. {
  9. [ExecuteInEditMode]
  10. public class SpatialUIController : MonoBehaviour
  11. {
  12. #region Singleton
  13. private static SpatialUIController instance;
  14. public static SpatialUIController Instance
  15. {
  16. get
  17. {
  18. if (instance == null)
  19. {
  20. instance = FindObjectOfType<SpatialUIController>();
  21. if (instance == null)
  22. {
  23. GameObject go = new GameObject("SpatialUIController");
  24. instance = go.AddComponent<SpatialUIController>();
  25. }
  26. }
  27. return instance;
  28. }
  29. }
  30. #endregion
  31. public event Action<float, bool> ReCalculateAllSize;
  32. /// <summary>
  33. /// 中间位相机(通常用于Editor)
  34. /// </summary>
  35. [HideInInspector]
  36. public Camera centerCamera;
  37. /// <summary>
  38. /// 左眼相机
  39. /// </summary>
  40. [HideInInspector]
  41. public Camera leftCamera;
  42. /// <summary>
  43. /// 右眼相机
  44. /// </summary>
  45. [HideInInspector]
  46. public Camera rightCamera;
  47. /// <summary>
  48. /// 头的Transform
  49. /// </summary>
  50. [HideInInspector]
  51. public Transform headTransform;
  52. /// <summary>
  53. /// 图片的每个像素对应的UnityUnit(Cube的默认边长即为1个UnityUnit)
  54. /// </summary>
  55. public float unitsPerPixel;
  56. ////[HideInInspector]
  57. public float lastUnitsPerPixel;
  58. //public ARUIController()
  59. //{
  60. // instance = this;
  61. //}
  62. private void Awake()
  63. {
  64. if (Application.isPlaying)
  65. {
  66. instance = this;
  67. }
  68. else
  69. {
  70. #if UNITY_EDITOR
  71. //Common.PrefabUtility.UnpackPrefabInstance(gameObject);
  72. lastUnitsPerPixel = unitsPerPixel;
  73. #endif
  74. }
  75. }
  76. private void Start()
  77. {
  78. if (Application.isPlaying)
  79. {
  80. if (HMDPoseTracker.Instance != null)
  81. {
  82. leftCamera = HMDPoseTracker.Instance.leftCamera != null ? HMDPoseTracker.Instance.leftCamera : null;
  83. rightCamera = HMDPoseTracker.Instance.rightCamera != null ? HMDPoseTracker.Instance.rightCamera : null;
  84. centerCamera = HMDPoseTracker.Instance.centerCamera != null ? HMDPoseTracker.Instance.centerCamera : null;
  85. headTransform = HMDPoseTracker.Instance.transform;
  86. }
  87. if (leftCamera == null)
  88. {
  89. Debug.LogError("leftCamera 不存在!");
  90. }
  91. if (rightCamera == null)
  92. {
  93. Debug.LogError("rightCamera 不存在!");
  94. }
  95. if (centerCamera == null)
  96. {
  97. Debug.LogError("centerCamera 不存在!");
  98. }
  99. }
  100. }
  101. #if UNITY_EDITOR
  102. public void PerformReCalculateAllSize(float newUnitsPerPixel)
  103. {
  104. if (ReCalculateAllSize != null)
  105. {
  106. ReCalculateAllSize(newUnitsPerPixel, true);
  107. }
  108. }
  109. #endif
  110. }
  111. }