using System.Collections; using System.Collections.Generic; using UnityEngine; namespace SC.XR.Unity { public class Module_RGBCamera : MonoBehaviour { private WebCamTexture webcamTexture; public string deviceName; private static MeshRenderer mat; int _width; int _height; bool hasWebCamera = false; Coroutine waitSlamInit = null; void Start() { PlayCamera(); } void OnDestroy() { StopCamera(); } public void PlayCamera() { DebugMy.Log("Module_RGBCamera Step 0: PlayCamera !", this, true); if (waitSlamInit == null) { waitSlamInit = StartCoroutine(WaitSlamInitAction()); } } public void StopCamera() { DebugMy.Log("Module_RGBCamera Step 7: StopCamera !", this, true); if (waitSlamInit != null) { StopCoroutine(waitSlamInit); waitSlamInit = null; } if (webcamTexture != null && webcamTexture.isPlaying) { webcamTexture.Stop(); webcamTexture = null; } } IEnumerator WaitSlamInitAction() { if (webcamTexture != null && webcamTexture.isPlaying) { DebugMy.Log("Module_RGBCamera : webcamTexture already isPlaying !", this, true); yield break; } //yield return new WaitUntil(() => API_GSXR_Slam.SlamManager != null && API_GSXR_Slam.SlamManager.IsRunning); //DebugMy.Log("Module_RGBCamera Step 1: SlamManager.IsRunning !", this, true); yield return new WaitUntil(() => ConnectCamera()); DebugMy.Log("Module_RGBCamera Step 2: ConnectCamera OK !", this, true); yield return Application.RequestUserAuthorization(UserAuthorization.WebCam); DebugMy.Log("Module_RGBCamera Step 3: RequestUserAuthorization OK !", this, true); if (Application.HasUserAuthorization(UserAuthorization.WebCam)) { DebugMy.Log("Module_RGBCamera Step 4: HasUserAuthorization OK !", this, true); yield return new WaitUntil(() => (WebCamTexture.devices.Length > 0)); WebCamDevice[] devices = WebCamTexture.devices; for (int i = 0; i < devices.Length; i++) DebugMy.Log("Module_RGBCamera :device["+i+"]:" + devices[i].name, this, true); if (devices.Length > 0) { _width = 640; _height = 480; if (devices[0].availableResolutions.Length > 0) { _width = devices[0].availableResolutions[0].width; _height = devices[0].availableResolutions[0].height; } deviceName = devices[0].name; DebugMy.Log("Module_RGBCamera Step 5:" + deviceName + " " + _width + " " + _height, this, true); webcamTexture = new WebCamTexture(deviceName, _width, _height, 30); webcamTexture.Play(); DebugMy.Log("Module_RGBCamera Step 6: webcamTexture Play", this, true); mat = this.gameObject.GetComponent(); if (mat) mat.material.mainTexture = webcamTexture; } } else { DebugMy.Log("Module_RGBCamera Step 8: Error No WebCamera", this, true); } waitSlamInit = null; } bool ConnectCamera() { return true; } } }