UnityCameraIntegration.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.Android;
  4. using UnityEngine.UI;
  5. public class UnityCameraIntegration : MonoBehaviour
  6. {
  7. private bool camAvailable;
  8. [HideInInspector]
  9. public WebCamTexture cameraTexture;
  10. public RawImage background;
  11. public AspectRatioFitter fit;
  12. public readonly static int width = 352;
  13. public readonly static int height = 288;
  14. void Start()
  15. {
  16. SetupCamera();
  17. }
  18. private void SetupCamera()
  19. {
  20. if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
  21. {
  22. Permission.RequestUserPermission(Permission.Camera);
  23. return;
  24. }
  25. WebCamDevice[] devices = WebCamTexture.devices;
  26. if (devices.Length == 0)
  27. return;
  28. for (int i = 0; i < devices.Length; i++)
  29. {
  30. var curr = devices[i];
  31. if (curr.isFrontFacing == true)
  32. { //RhinoX using front facing camera
  33. cameraTexture = new WebCamTexture(curr.name, width, height, 60);
  34. break;
  35. }
  36. }
  37. if (cameraTexture == null)
  38. {
  39. Debug.LogError("Failed to aquire Camera");
  40. Destroy(this);
  41. return;
  42. }
  43. cameraTexture.Play();
  44. //if (null == background)
  45. // return;
  46. background.texture = cameraTexture;
  47. camAvailable = true;
  48. Debug.Log("RGB texture has been setup for T3D hand tracking");
  49. }
  50. //void Update () {
  51. // if (!camAvailable) {
  52. // SetupCamera ();
  53. // return;
  54. // }
  55. // if (null == background || null == fit) {
  56. // Destroy (this);
  57. // return;
  58. // }
  59. // fit.aspectRatio = (float) cameraTexture.width / (float) cameraTexture.height;
  60. // float scaleY = cameraTexture.videoVerticallyMirrored ? -1f : 1f; // Find if the camera is mirrored or not
  61. // background.rectTransform.localScale = new Vector3 (1f, scaleY, 1f); // Swap the preview of the mirrored camera
  62. // int orient = -cameraTexture.videoRotationAngle;
  63. // background.rectTransform.localEulerAngles = new Vector3 (0, 0, orient);
  64. //}
  65. }