using SC.XR.Unity.Module_InputSystem; using SC.XR.Unity.Module_InputSystem.InputDeviceHand; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; using UnityEngine.UI; public class openCamera : MonoBehaviour { //[DllImport("htinstance")] //public static extern void HANDTRACK_Stop(); [HideInInspector] public WebCamTexture webTex; [HideInInspector] public string deviceName; public static openCamera Instance; private static List mats=new List(); void Start() { #if UNITY_ANDROID && !UNITY_EDITOR if (Instance!=null) { webTex = openCamera.Instance.webTex; Debug.Log("webTex复制"); GameObject game = GameObject.Find("BackgroundPlane"); setTexture(this.gameObject, game.GetComponent().material.mainTexture); mats.Add(this.gameObject); } else { Screen.sleepTimeout = SleepTimeout.NeverSleep; Instance = this; mats.Add(this.gameObject); StartCoroutine(CallCamera()); } #endif } public void setTexture(GameObject go,Texture tx) { MeshRenderer mr = go.GetComponent(); RawImage raw = go.GetComponent(); if(mr ==null) { // raw.transform.eulerAngles = new Vector3(180,0,0); raw.texture = tx; } else { // mr.transform.eulerAngles = new Vector3(180, 0, 0); mr.material.mainTexture = tx; } } public void restart(int fps=30) { CustomInfo.FPS = fps; StartCoroutine(CallCamera()); } void Update() { } /// ///调用摄像头 /// Texture2D tex2D; IEnumerator CallCamera() { yield return Application.RequestUserAuthorization(UserAuthorization.WebCam); if (Application.HasUserAuthorization(UserAuthorization.WebCam)) { WebCamDevice[] devices = WebCamTexture.devices; deviceName = devices[0].name; if (webTex != null) { webTex.Stop(); webTex = null; } //设置摄像机摄像的区域 webTex = new WebCamTexture(deviceName, CustomInfo.mWidth, CustomInfo.mHight, CustomInfo.FPS); for (int i = 0; i < mats.Count; i++) { setTexture(mats[i], webTex); } webTex.Play();//开始摄像 } } private void OnApplicationPause(bool pause) { } private void OnDestroy() { // HANDTRACK_Stop(); } }