前提・実現したいこと
Unityで、RawImageにwebcameraから取得した映像を表示します。
制御内容は、
1. ゲーム開始時は、RawImageは非表示で、Webcameraも起動していない。
2.CameraStartボタンを押すと、RawImageを表示され、同時にwebcameraの開始される。
3.再度CameraStartボタンを押すと、webcameraが停止し、RawImageも非表示となる。
といった内容を実装したいと考えています。
色々と試してみたもののうまくいかず、よろしくお願いいたします。
発生している問題・エラーメッセージ
Cameraが非表示状態にならず、ゲーム開始時からRawImageが表示状態になっている。
該当のソースコード
C#
// WebCameraTest using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.IO; // Webカメラ public class WebCameraTest : MonoBehaviour { private static int width = 640; private static int height = 512; private static int FPS = 60; private float Alpha = 0.5f; // UI RawImage rawImage; WebCamTexture webCamTexture; Color32[] color32; WebCamDevice[] webCamDevice; // 外部カメラ int selectCamera = 0; // カメラの選択用 [SerializeField] private GameObject ChangeCameraButton; [SerializeField] private GameObject SnapshotOpeButton; // スタート時に呼ばれる public void Start() { SnapshotOpeButton.SetActive(true); rawImage = GetComponent<RawImage>(); webCamTexture = new WebCamTexture(width, height, FPS); //webCamDevice = WebCamTexture(width, height, FPS); rawImage.texture = webCamTexture; rawImage.color = new Color(rawImage.color.r, rawImage.color.g, rawImage.color.b, Alpha); GetComponent<Renderer>().material.mainTexture = webCamTexture; webCamTexture.Play(); } // カメラの変更 public void OnChangeCamera() { // カメラの取得 WebCamDevice[] webCamDevices = WebCamTexture.devices; // カメラが1つの時は無処理 if (webCamDevices.Length <= 1) return; // カメラの切り替え selectCamera++; if (selectCamera >= webCamDevices.Length) selectCamera = 0; webCamTexture.Stop(); // カメラを停止 webCamTexture = new WebCamTexture(webCamDevices[selectCamera].name, width, height, FPS); // カメラを変更 rawImage.texture = webCamTexture; webCamTexture.Play(); // 別カメラを開始 } // 写真を撮影する void Update() { if (Input.GetKeyDown(KeyCode.Space) || Input.touchCount > 0) { color32 = webCamTexture.GetPixels32(); Texture2D texture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.ARGB32, false); GameObject.Find("RawImage").GetComponent<RawImage>().material.mainTexture = texture; texture.SetPixels(webCamTexture.GetPixels()); texture.Apply(); // JPGでエンコードする byte[] bytes = texture.EncodeToJPG(); // エンコードが終わったら削除する Object.Destroy(texture); // 保存先の指定 File.WriteAllBytes("Assets/SnapShot/photo.jpg", bytes); } void OnSnapshotOpe() { if (SnapshotOpeButton == false) { return; } if (GUI.Button(new Rect(25, -190, 55, -130), "photo")) { color32 = webCamTexture.GetPixels32(); Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false); GameObject.Find("RawImage").GetComponent<RawImage>().material.mainTexture = texture; texture.SetPixels(webCamTexture.GetPixels()); texture.Apply(); // JPGでエンコードする byte[] bytes = texture.EncodeToJPG(); // エンコードが終わったら削除する Object.Destroy(texture); // 保存先の指定 File.WriteAllBytes("Assets/SnapShot/photo.jpg", bytes); } } } }
C#
// CameraStart using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class CameraStartButton : MonoBehaviour { [SerializeField] private GameObject RawImage; bool check = false; public void Onclick() { if (!check) { RawImage.SetActive(false); check = true; } else if (check) { RawImage.SetActive(true); check = false; } } }
試したこと
RawImageにアタッチしているスクリプト(WebCameraTest)では、webcameraを開始するのみにしており、CameraStartボタンにアタッチしているスクリプト(CameraStart)でRawImageの表示、非表示をコントロールしようと試みました。なおRawImageのInspectorはenableとしております。
可能ならばWebCameraTestのスクリプトのみでCameraStartボタンでの制御を組み込みたかったのですが、オフにする方法が調べきれず頓挫した次第です。
補足情報(FW/ツールのバージョンなど)
Unity.2020.3.22f1
まだ回答がついていません
会員登録して回答してみよう