NREmulatorManager.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #if UNITY_EDITOR
  12. using UnityEngine;
  13. /// <summary> Manager for nr emulators. </summary>
  14. internal class NREmulatorManager : MonoBehaviour
  15. {
  16. /// <summary> Gets or sets the instance. </summary>
  17. /// <value> The instance. </value>
  18. public static NREmulatorManager Instance { get; set; }
  19. /// <summary> Identifier for the simulation plane. </summary>
  20. public static int SIMPlaneID = 0;
  21. /// <summary> True if inited. </summary>
  22. public static bool Inited = false;
  23. /// <summary> The center camera. </summary>
  24. private Camera centerCam = null;
  25. /// <summary> Starts this object. </summary>
  26. private void Start()
  27. {
  28. DontDestroyOnLoad(this);
  29. Instance = this;
  30. }
  31. /// <summary> Query if 'worldPos' is in game view. </summary>
  32. /// <param name="worldPos"> The world position.</param>
  33. /// <returns> True if in game view, false if not. </returns>
  34. public bool IsInGameView(Vector3 worldPos)
  35. {
  36. if (centerCam == null) centerCam = GameObject.Find("CenterCamera").GetComponent<Camera>();
  37. Transform camTransform = centerCam.transform;
  38. Vector2 viewPos = centerCam.WorldToViewportPoint(worldPos);
  39. Vector3 dir = (worldPos - camTransform.position).normalized;
  40. float dot = Vector3.Dot(camTransform.forward, dir);
  41. if (dot > 0 && viewPos.x >= 0 && viewPos.x <= 1 && viewPos.y >= 0 && viewPos.y <= 1)
  42. {
  43. return true;
  44. }
  45. else
  46. {
  47. return false;
  48. }
  49. }
  50. }
  51. #endif
  52. }