CanvasUtils.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Collections;
  2. using UnityEngine;
  3. using System.Linq;
  4. using Rokid.UXR.Config;
  5. using Rokid.UXR.Native;
  6. namespace Rokid.UXR.Utility
  7. {
  8. public class CanvasUtils
  9. {
  10. public static IEnumerator FitCanvasToCameraFov(Canvas canvas, Camera camera, float distance, float fov, bool followToCamere)
  11. {
  12. canvas.renderMode = RenderMode.ScreenSpaceCamera;
  13. canvas.worldCamera = camera;
  14. canvas.planeDistance = distance;
  15. camera.fieldOfView = fov;
  16. if (followToCamere)
  17. {
  18. canvas.transform.SetParent(camera.transform);
  19. }
  20. yield return new WaitForSeconds(1.0f);
  21. canvas.renderMode = RenderMode.WorldSpace;
  22. }
  23. /// <summary>
  24. /// 获取眼镜的显示fov
  25. /// </summary>
  26. /// <returns></returns>
  27. public static float GetGlassViewFov()
  28. {
  29. string glassName = NativeInterface.NativeAPI.GetGlassName();
  30. glassName = glassName.Contains("Air") ? "Air" : "Max";
  31. DeviceInfo deviceInfo = DeviceInfos.GetInfos().Where(info => info.DeviceName.Contains(glassName)).FirstOrDefault();
  32. if (deviceInfo != null)
  33. {
  34. return deviceInfo.CameraFov;
  35. }
  36. return 20.3f;
  37. }
  38. public static IEnumerator FitCanvasToCameraFov(Canvas canvas, Camera camera, float distance, bool followToCamere)
  39. {
  40. canvas.renderMode = RenderMode.ScreenSpaceCamera;
  41. canvas.worldCamera = camera;
  42. canvas.planeDistance = distance;
  43. camera.fieldOfView = GetGlassViewFov();
  44. if (followToCamere)
  45. {
  46. canvas.transform.SetParent(camera.transform);
  47. }
  48. yield return new WaitForSeconds(1.0f);
  49. canvas.renderMode = RenderMode.WorldSpace;
  50. canvas.worldCamera = null;
  51. }
  52. }
  53. }