質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

529閲覧

WebCameraの開始・停止をButton UIで制御したいです。

MRPM

総合スコア3

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2022/01/20 03:59

前提・実現したいこと

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

質問のコードは問題と直接関係なさそうな処理が多かったので 0 から書いてみたら、「実現したい事」に書いてある挙動になったと思うのでコードを置いておきます。

csharp

1using UnityEngine; 2using UnityEngine.UI; 3 4public class WebCamController : MonoBehaviour 5{ 6 [SerializeField] RawImage _image = default; 7 WebCamTexture _texture = default; 8 9 void Start() 10 { 11 _image.enabled = false; 12 _texture = new WebCamTexture(); 13 _image.texture = _texture; 14 } 15 16 // ボタンの OnClick から呼ばれることを想定している 17 public void Begin() 18 { 19 _image.enabled = true; 20 _texture.Play(); 21 } 22 23 // ボタンの OnClick から呼ばれることを想定している 24 public void End() 25 { 26 _texture.Stop(); 27 _image.enabled = false; 28 } 29}

投稿2022/02/28 09:46

bboydaisuke

総合スコア5270

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

MRPM

2022/03/07 05:26

返信が遅くなり申し訳ありません。ご提示のように試しましたところうまくいきました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問