実現したいこと
udp通信を用いて、MATLABからunityへ画像データを送り受信した画像データをもとにunity側で画像を表示させる。
現在卒業研究で、udp通信を用いてMATLABからunityに画像データを送信してunity側で画像を表示させるというプログラムを書いているのですが、表示させることができません。どこが悪いのかもよくわからないので、どのようにしたら画像を表示することができるかご教授お願いいたします。
*画像サイズは横が291、縦が380です。また、受信したバイト配列のデータの長さは512です。
試したこと(Texture2Dの大きさを変える)
エラーメッセージ
該当のソースコード
c#
1**udp.cs(unity)** 2 3using UnityEngine; 4using UnityEngine.UI; 5using System.Net.Sockets; 6using System.Net; 7using System.IO; 8using System; 9 10public class udp : MonoBehaviour 11{ 12 public int port = 51000; 13 public RawImage rawImage; 14 15 private UdpClient udpClient; 16 17 void Start() 18 { 19 udpClient = new UdpClient(port); 20 udpClient.BeginReceive(ReceiveCallback, null); 21 } 22 23 private void ReceiveCallback(IAsyncResult result) 24 { 25 IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0); 26 byte[] receivedData = udpClient.EndReceive(result, ref endPoint); //配列の大きさの指定をしてみる。 27 28 Debug.Log("Received data size: " + receivedData.Length); 29 30 // 受信したデータをテクスチャに変換 31 Texture2D receivedTexture = new Texture2D(291, 380); 32 receivedTexture.LoadImage(receivedData); 33 34 // メインスレッドでUIの更新を行う 35 UnityMainThreadDispatcher.Instance().Enqueue(() => 36 { 37 // RawImageにテクスチャをセット 38 rawImage.texture = receivedTexture; 39 }); 40 41 // 再度受信を開始 42 udpClient.BeginReceive(ReceiveCallback, null); 43 } 44 45 void OnDisable() 46 { 47 if (udpClient != null) 48 { 49 udpClient.Close(); 50 } 51 } 52} 53 54 55**UnityMainThreadDispatcher.cs(unity)** 56 57using UnityEngine; 58using System.Collections; 59using System.Collections.Generic; 60using System; 61 62public class UnityMainThreadDispatcher : MonoBehaviour 63{ 64 private static readonly Queue<Action> _executionQueue = new Queue<Action>(); 65 66 public void Update() 67 { 68 lock (_executionQueue) 69 { 70 while (_executionQueue.Count > 0) 71 { 72 _executionQueue.Dequeue().Invoke(); 73 74 } 75 } 76 } 77 78 public void Enqueue(IEnumerator action) 79 { 80 lock (_executionQueue) 81 { 82 _executionQueue.Enqueue(() => { 83 StartCoroutine(action); 84 }); 85 } 86 } 87 88 public void Enqueue(Action action) 89 { 90 lock (_executionQueue) 91 { 92 _executionQueue.Enqueue(action); 93 } 94 } 95 96 private static UnityMainThreadDispatcher _instance = null; 97 98 public static bool Exists() 99 { 100 return _instance != null; 101 } 102 103 public static UnityMainThreadDispatcher Instance() 104 { 105 if (!Exists()) 106 { 107 throw new Exception("UnityMainThreadDispatcher does not exist."); 108 } 109 return _instance; 110 } 111 112 void Awake() 113 { 114 if (_instance == null) 115 { 116 _instance = this; 117 DontDestroyOnLoad(this.gameObject); 118 119 } 120 } 121 122 void OnDestroy() 123 { 124 _instance = null; 125 } 126} 127 128 129**MATLAB** 130clear all; 131 132% 送信する画像のパス 133imagePath = 'C:\Users\sog4-\My project (1)\Assets\Scripts\MicrosoftTeams-image.png'; 134 135img = imread(imagePath); 136 137% 画像データを1次元ベクトルに変換 138img_vector = img(:); 139 140% 送信先のIPアドレスとポート番号 141ipAddress = '127.0.0.1'; 142port = 51000; 143 144% UDPオブジェクトの作成 145udpSender = udp(ipAddress, port, 'OutputBufferSize', numel(img_vector)); 146fopen(udpSender); 147 148% 画像データをUDPで送信 149 150fwrite(udpSender, img_vector, 'uint8'); 151 152% UDPオブジェクトを閉じる 153fclose(udpSender); 154delete(udpSender); 155