-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDPImageReceiver.cs
More file actions
193 lines (164 loc) · 5.52 KB
/
UDPImageReceiver.cs
File metadata and controls
193 lines (164 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using UnityEngine;
using System.Collections;
public class UDPImageReceiver : MonoBehaviour
{
[Header("UDP Settings")]
public int localPort = 9999;
[Header("Display Settings")]
public Renderer imageRenderer; // 画像を表示するRenderer
public bool showInUI = true; // UIで表示するかどうか
private UdpClient udpClient;
private Thread receiveThread;
private bool isReceiving = false;
// 受信した画像データを格納
private byte[] latestImageData;
private bool hasNewImage = false;
// Texture2D for displaying
public Texture2D imageTexture;
void Start()
{
InitializeUDPReceiver();
}
void InitializeUDPReceiver()
{
try
{
udpClient = new UdpClient(localPort);
isReceiving = true;
receiveThread = new Thread(ReceiveData);
receiveThread.IsBackground = true;
receiveThread.Start();
Debug.Log($"UDP Receiver started on port {localPort}");
}
catch (Exception e)
{
Debug.LogError($"Failed to initialize UDP receiver: {e.Message}");
}
}
void ReceiveData()
{
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
while (isReceiving)
{
try
{
byte[] data = udpClient.Receive(ref remoteEndPoint);
if (data.Length > 4) // サイズ情報(4バイト)+ 画像データ
{
// サイズ情報を読み取り
int imageSize = BitConverter.ToInt32(data, 0);
if (data.Length >= 4 + imageSize)
{
// 画像データを抽出
byte[] imageData = new byte[imageSize];
Array.Copy(data, 4, imageData, 0, imageSize);
// メインスレッドで処理するためにキューに追加
lock (this)
{
latestImageData = imageData;
hasNewImage = true;
}
}
}
}
catch (Exception e)
{
if (isReceiving)
{
Debug.LogError($"UDP receive error: {e.Message}");
}
}
}
}
void Update()
{
// 新しい画像データがある場合、メインスレッドで処理
if (hasNewImage)
{
lock (this)
{
if (latestImageData != null)
{
ProcessReceivedImage(latestImageData);
hasNewImage = false;
}
}
}
}
void ProcessReceivedImage(byte[] imageData)
{
try
{
// 画像データを直接Texture2Dとして読み込み
if (imageTexture == null)
{
imageTexture = new Texture2D(2, 2, TextureFormat.RGBA32, false);
}
// PNGデータをTexture2Dに読み込み(透明度込み)
bool loaded = imageTexture.LoadImage(imageData);
if (loaded)
{
// 透明度をサポートするマテリアル設定
if (imageRenderer != null)
{
// 透明度をサポートするシェーダーに変更
if (imageRenderer.material.shader.name != "Sprites/Default")
{
imageRenderer.material.shader = Shader.Find("Sprites/Default");
}
imageRenderer.material.mainTexture = imageTexture;
// 透明度を有効にする
imageRenderer.material.color = Color.white;
}
Debug.Log($"透明度付き画像を受信・表示: {imageTexture.width}x{imageTexture.height}");
}
else
{
Debug.LogWarning("Failed to load PNG image data");
}
}
catch (Exception e)
{
Debug.LogError($"Error processing transparent image: {e.Message}");
}
}
void OnDestroy()
{
StopReceiving();
}
void OnApplicationQuit()
{
StopReceiving();
}
void StopReceiving()
{
isReceiving = false;
if (receiveThread != null && receiveThread.IsAlive)
{
receiveThread.Join(1000); // 1秒待機
}
if (udpClient != null)
{
udpClient.Close();
}
Debug.Log("UDP Receiver stopped");
}
// GUI for debugging
void OnGUI()
{
if (showInUI && imageTexture != null)
{
GUI.DrawTexture(new Rect(10, 10, 200, 200), imageTexture, ScaleMode.ScaleToFit);
}
GUI.Label(new Rect(10, 220, 200, 20), $"Port: {localPort}");
GUI.Label(new Rect(10, 240, 200, 20), $"Receiving: {isReceiving}");
if (imageTexture != null)
{
GUI.Label(new Rect(10, 260, 200, 20), $"Size: {imageTexture.width}x{imageTexture.height}");
}
}
}