前提・実現したいこと
Unityで、RawImageにwebcameraから取得した映像を表示します。
制御内容は、
1. ゲーム開始時は、RawImageは非表示で、Webcameraも起動していない。
2.CameraStartボタンを押すと、RawImageを表示され、同時にwebcameraの開始される。
3.再度CameraStartボタンを押すと、webcameraが停止し、RawImageも非表示となる。
といった内容を実装したいと考えています。
色々と試してみたもののうまくいかず、よろしくお願いいたします。
発生している問題・エラーメッセージ
Cameraが非表示状態にならず、ゲーム開始時からRawImageが表示状態になっている。
該当のソースコード
C#
1// WebCameraTest 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using UnityEngine.UI; 6using System.IO; 7 8// Webカメラ 9public class WebCameraTest : MonoBehaviour 10{ 11 private static int width = 640; 12 private static int height = 512; 13 private static int FPS = 60; 14 private float Alpha = 0.5f; 15 16 // UI 17 RawImage rawImage; 18 WebCamTexture webCamTexture; 19 Color32[] color32; 20 WebCamDevice[] webCamDevice; // 外部カメラ 21 int selectCamera = 0; // カメラの選択用 22 23 [SerializeField] 24 private GameObject ChangeCameraButton; 25 [SerializeField] 26 private GameObject SnapshotOpeButton; 27 28 // スタート時に呼ばれる 29 public void Start() 30 { 31 SnapshotOpeButton.SetActive(true); 32 33 rawImage = GetComponent<RawImage>(); 34 webCamTexture = new WebCamTexture(width, height, FPS); 35 //webCamDevice = WebCamTexture(width, height, FPS); 36 rawImage.texture = webCamTexture; 37 rawImage.color = new Color(rawImage.color.r, rawImage.color.g, rawImage.color.b, Alpha); 38 GetComponent<Renderer>().material.mainTexture = webCamTexture; 39 webCamTexture.Play(); 40 } 41 42 43 // カメラの変更 44 public void OnChangeCamera() 45 { 46 // カメラの取得 47 WebCamDevice[] webCamDevices = WebCamTexture.devices; 48 49 // カメラが1つの時は無処理 50 if (webCamDevices.Length <= 1) return; 51 52 // カメラの切り替え 53 selectCamera++; 54 if (selectCamera >= webCamDevices.Length) selectCamera = 0; 55 webCamTexture.Stop(); // カメラを停止 56 webCamTexture = new WebCamTexture(webCamDevices[selectCamera].name, width, height, FPS); // カメラを変更 57 rawImage.texture = webCamTexture; 58 webCamTexture.Play(); // 別カメラを開始 59 } 60 61 62 // 写真を撮影する 63 void Update() 64 { 65 66 if (Input.GetKeyDown(KeyCode.Space) || Input.touchCount > 0) 67 { 68 color32 = webCamTexture.GetPixels32(); 69 Texture2D texture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.ARGB32, false); 70 GameObject.Find("RawImage").GetComponent<RawImage>().material.mainTexture = texture; 71 texture.SetPixels(webCamTexture.GetPixels()); 72 texture.Apply(); 73 74 // JPGでエンコードする 75 byte[] bytes = texture.EncodeToJPG(); 76 // エンコードが終わったら削除する 77 Object.Destroy(texture); 78 // 保存先の指定 79 File.WriteAllBytes("Assets/SnapShot/photo.jpg", bytes); 80 } 81 82 void OnSnapshotOpe() 83 { 84 if (SnapshotOpeButton == false) { return; } 85 86 if (GUI.Button(new Rect(25, -190, 55, -130), "photo")) 87 { 88 color32 = webCamTexture.GetPixels32(); 89 Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false); 90 GameObject.Find("RawImage").GetComponent<RawImage>().material.mainTexture = texture; 91 texture.SetPixels(webCamTexture.GetPixels()); 92 texture.Apply(); 93 94 // JPGでエンコードする 95 byte[] bytes = texture.EncodeToJPG(); 96 // エンコードが終わったら削除する 97 Object.Destroy(texture); 98 // 保存先の指定 99 File.WriteAllBytes("Assets/SnapShot/photo.jpg", bytes); 100 } 101 } 102 } 103}
C#
1// CameraStart 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using UnityEngine.UI; 6 7public class CameraStartButton : MonoBehaviour 8{ 9 [SerializeField] 10 private GameObject RawImage; 11 12 bool check = false; 13 14 public void Onclick() 15 { 16 if (!check) 17 { 18 RawImage.SetActive(false); 19 check = true; 20 } 21 else 22 if (check) 23 { 24 RawImage.SetActive(true); 25 check = false; 26 } 27 } 28}
試したこと
RawImageにアタッチしているスクリプト(WebCameraTest)では、webcameraを開始するのみにしており、CameraStartボタンにアタッチしているスクリプト(CameraStart)でRawImageの表示、非表示をコントロールしようと試みました。なおRawImageのInspectorはenableとしております。
可能ならばWebCameraTestのスクリプトのみでCameraStartボタンでの制御を組み込みたかったのですが、オフにする方法が調べきれず頓挫した次第です。
補足情報(FW/ツールのバージョンなど)
Unity.2020.3.22f1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/03/07 05:26