123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- 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<GameObject> mats=new List<GameObject>();
- 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<MeshRenderer>().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<MeshRenderer>();
- RawImage raw = go.GetComponent<RawImage>();
-
- 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();
-
- }
- }
|