EventCameraRaycaster.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using UnityEngine;
  12. using UnityEngine.EventSystems;
  13. /// <summary> An event camera raycaster. </summary>
  14. public abstract class EventCameraRaycaster : BaseRaycaster
  15. {
  16. /// <summary> True if is destroying, false if not. </summary>
  17. private bool isDestroying = false;
  18. /// <summary> The default camera. </summary>
  19. private Camera defaultCam;
  20. /// <summary> The near distance. </summary>
  21. [SerializeField]
  22. private float nearDistance = 0f;
  23. /// <summary> The far distance. </summary>
  24. [SerializeField]
  25. private float farDistance = 20f;
  26. /// <summary> Gets or sets the near distance. </summary>
  27. /// <value> The near distance. </value>
  28. public float NearDistance
  29. {
  30. get { return nearDistance; }
  31. set
  32. {
  33. nearDistance = Mathf.Max(0f, value);
  34. if (eventCamera != null)
  35. {
  36. eventCamera.nearClipPlane = nearDistance;
  37. }
  38. }
  39. }
  40. /// <summary> Gets or sets the far distance. </summary>
  41. /// <value> The far distance. </value>
  42. public float FarDistance
  43. {
  44. get { return farDistance; }
  45. set
  46. {
  47. farDistance = Mathf.Max(0f, nearDistance, value);
  48. if (eventCamera != null)
  49. {
  50. eventCamera.farClipPlane = farDistance;
  51. }
  52. }
  53. }
  54. /// <summary> <para>The camera that will generate rays for this raycaster.</para> </summary>
  55. /// <value> The event camera. </value>
  56. public override Camera eventCamera
  57. {
  58. get
  59. {
  60. if (isDestroying)
  61. {
  62. return null;
  63. }
  64. if (defaultCam == null)
  65. {
  66. var go = new GameObject(name + " FallbackCamera");
  67. go.SetActive(false);
  68. go.transform.SetParent(EventSystem.current.transform, false);
  69. go.transform.localPosition = Vector3.zero;
  70. go.transform.localRotation = Quaternion.identity;
  71. go.transform.localScale = Vector3.one;
  72. defaultCam = go.AddComponent<Camera>();
  73. defaultCam.clearFlags = CameraClearFlags.Nothing;
  74. defaultCam.cullingMask = 0;
  75. defaultCam.orthographic = true;
  76. defaultCam.orthographicSize = 1;
  77. defaultCam.useOcclusionCulling = false;
  78. #if !(UNITY_5_3 || UNITY_5_2 || UNITY_5_1 || UNITY_5_0)
  79. defaultCam.stereoTargetEye = StereoTargetEyeMask.None;
  80. #endif
  81. defaultCam.nearClipPlane = nearDistance;
  82. defaultCam.farClipPlane = farDistance;
  83. }
  84. return defaultCam;
  85. }
  86. }
  87. /// <summary> <para>See MonoBehaviour.OnDestroy.</para> </summary>
  88. protected override void OnDestroy()
  89. {
  90. base.OnDestroy();
  91. isDestroying = true;
  92. if (defaultCam != null)
  93. {
  94. Destroy(defaultCam);
  95. defaultCam = null;
  96. }
  97. }
  98. }
  99. }