iPhoneのカメラで撮影した画像をPC上のUnityに送信して表示したいと思っています.
現在Photonを使用して,無線で実装したいと考えているのですが,
iPhoneにビルドしたアプリからカメラを起動し撮影した画像をPhotonを使ってPCに送信し貼り付けようとしています.
以下のサイトを参考に,テクスチャを送信することはできました.
https://nabla-tech-lab.hatenablog.com/entry/2019/05/15/180000
また,また,別のサイトを参考にiOSアプリからの映像の取得,スクリーンショットをテクスチャとして貼り付けるところまではできています.(https://loumo.jp/wp/archive/20150414000004/)
下記のコードからWebカム画像を映し,上のサイトのようにそのテクスチャをDistributableWhiteBoardというオブジェクトに貼ろうとしているのですが,下の画像のようなエラーがでます.
TextureをTexture2Dに変換しているつもりなのですが,エラーを見る限りcastがうまくいっていないようです.
知識の少ない状態での質問で申し訳ないですが,ご存知の方対処法を教えていただきたいです.
また,別の手段でも構いませんので,やろうとしていることを実装できるような方法があれば教えてください.
よろしくお願いします.
C# Webカム映像取得部
1using UnityEngine; 2using System.Collections; 3public class WebCamTex : MonoBehaviour 4{ 5 public int Width = 1920; 6 public int Height = 1080; 7 public int FPS = 30; 8 public WebCamTexture webcamTexture; 9 public Color32[] color32; 10 11 void Start() 12 { 13 WebCamDevice[] devices = WebCamTexture.devices; 14 webcamTexture = new WebCamTexture(devices[0].name, Width, Height, FPS); 15 GetComponent<Renderer>().material.mainTexture = webcamTexture; 16 webcamTexture.Play(); 17 } 18 19 void Update() 20 { 21 if (Input.GetKeyDown(KeyCode.Space) || Input.touchCount > 0) 22 { 23 color32 = webcamTexture.GetPixels32(); 24 Texture2D texture = new Texture2D(webcamTexture.width, webcamTexture.height); 25 GameObject.Find("DistributableWhiteBoard").GetComponent<Renderer>().material.mainTexture = texture; 26 27 texture.SetPixels32(color32); 28 29 texture.Apply(); 30 } 31 } 32} 33
C#
1using UnityEngine; 2using Photon.Pun; 3using Photon.Realtime; 4using ExitGames.Client.Photon; 5using UniRx; 6 7namespace TextureSharing 8{ 9 public class TextureBroadcastComponent : MonoBehaviourPunCallbacks, IOnEventCallback 10 { 11 [SerializeField] 12 int messagePerSecond = 100; // 100 Messages / Second 13 14 int bytePerMessage = 1000; // 1KBytes / Message 15 16 Texture2D texture; // ★ Readable texture ★ 17 18 bool isReceiving; 19 byte[] receiveBuffer; 20 int totalDataSize; 21 int currentReceivedDataSize; 22 int receivedMessageCount; 23 24 void Start() 25 { 26 texture = (Texture2D)GetComponent<Renderer>().material.mainTexture; 27 try 28 { 29 texture.GetPixels32(); 30 } 31 catch(UnityException e) 32 { 33 Debug.LogError("!! This texture is not readable !!"); 34 } 35 } 36 37 #region sender methods 38 39 public void BroadcastTexture() 40 { 41 byte[] rawTextureData = texture.EncodeToPNG(); 42 43 int width = texture.width; 44 int height = texture.height; 45 int dataSize = rawTextureData.Length; 46 int viewId = this.photonView.ViewID; 47 48 Debug.Log("*************************"); 49 Debug.Log(" BroadcastTexture"); 50 Debug.Log(" Texture size: " + width + "x" + height + " = " + width*height + "px"); 51 Debug.Log(" RawTextureData: " + rawTextureData.Length + "bytes"); 52 Debug.Log("*************************"); 53 54 StreamTextureDataToOtherClients(rawTextureData, width, height, dataSize, viewId); 55 } 56 57以下略
![イメージ説明](1a399dcba3da30463832ed9062fbf213.png)
あなたの回答
tips
プレビュー