openCamera.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using SC.XR.Unity.Module_InputSystem;
  2. using SC.XR.Unity.Module_InputSystem.InputDeviceHand;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. public class openCamera : MonoBehaviour
  9. {
  10. //[DllImport("htinstance")]
  11. //public static extern void HANDTRACK_Stop();
  12. [HideInInspector]
  13. public WebCamTexture webTex;
  14. [HideInInspector]
  15. public string deviceName;
  16. public static openCamera Instance;
  17. private static List<GameObject> mats=new List<GameObject>();
  18. void Start()
  19. {
  20. #if UNITY_ANDROID && !UNITY_EDITOR
  21. if (Instance!=null)
  22. {
  23. webTex = openCamera.Instance.webTex;
  24. Debug.Log("webTex复制");
  25. GameObject game = GameObject.Find("BackgroundPlane");
  26. setTexture(this.gameObject, game.GetComponent<MeshRenderer>().material.mainTexture);
  27. mats.Add(this.gameObject);
  28. }
  29. else
  30. {
  31. Screen.sleepTimeout = SleepTimeout.NeverSleep;
  32. Instance = this;
  33. mats.Add(this.gameObject);
  34. StartCoroutine(CallCamera());
  35. }
  36. #endif
  37. }
  38. public void setTexture(GameObject go,Texture tx)
  39. {
  40. MeshRenderer mr = go.GetComponent<MeshRenderer>();
  41. RawImage raw = go.GetComponent<RawImage>();
  42. if(mr ==null)
  43. {
  44. // raw.transform.eulerAngles = new Vector3(180,0,0);
  45. raw.texture = tx;
  46. }
  47. else
  48. {
  49. // mr.transform.eulerAngles = new Vector3(180, 0, 0);
  50. mr.material.mainTexture = tx;
  51. }
  52. }
  53. public void restart(int fps=30)
  54. {
  55. CustomInfo.FPS = fps;
  56. StartCoroutine(CallCamera());
  57. }
  58. void Update()
  59. {
  60. } ///
  61. ///调用摄像头
  62. ///
  63. Texture2D tex2D;
  64. IEnumerator CallCamera()
  65. {
  66. yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
  67. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  68. {
  69. WebCamDevice[] devices = WebCamTexture.devices;
  70. deviceName = devices[0].name;
  71. if (webTex != null)
  72. {
  73. webTex.Stop();
  74. webTex = null;
  75. }
  76. //设置摄像机摄像的区域
  77. webTex = new WebCamTexture(deviceName, CustomInfo.mWidth, CustomInfo.mHight, CustomInfo.FPS);
  78. for (int i = 0; i < mats.Count; i++)
  79. {
  80. setTexture(mats[i], webTex);
  81. }
  82. webTex.Play();//开始摄像
  83. }
  84. }
  85. private void OnApplicationPause(bool pause)
  86. {
  87. }
  88. private void OnDestroy()
  89. {
  90. // HANDTRACK_Stop();
  91. }
  92. }