ARBase.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using UnityEngine;
  2. namespace UnityARInterface
  3. {
  4. public class ARBase : MonoBehaviour
  5. {
  6. protected Transform GetRoot()
  7. {
  8. var camera = GetCamera();
  9. if (camera != null)
  10. return camera.transform.parent;
  11. return null;
  12. }
  13. protected float GetScale()
  14. {
  15. var root = GetRoot();
  16. if (root != null)
  17. return root.transform.localScale.x;
  18. return 1f;
  19. }
  20. // Returns the first enabled ARController
  21. protected ARController GetFirstEnabledControllerInChildren()
  22. {
  23. foreach (var controller in GetComponentsInChildren<ARController>())
  24. {
  25. if (controller.enabled)
  26. {
  27. return controller;
  28. }
  29. }
  30. return null;
  31. }
  32. protected Camera GetCamera()
  33. {
  34. // Use the same camera as the ARController
  35. var arController = GetFirstEnabledControllerInChildren();
  36. if (arController != null)
  37. return arController.arCamera;
  38. // If we're on a camera then use that.
  39. var camera = GetComponent<Camera>();
  40. if (camera != null)
  41. return camera;
  42. // Fallback to main camera
  43. return Camera.main;
  44. }
  45. }
  46. }