質問編集履歴

6

文字の修正

2015/11/10 14:36

投稿

XLanceX
XLanceX

スコア10

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  言語はC#です。
6
6
 
7
- Debug.Log()については表示が確認できました。
7
+ 11/10 Debug.Log()については表示が確認できました。
8
8
 
9
9
 
10
10
 

5

進展部分の追記

2015/11/10 14:36

投稿

XLanceX
XLanceX

スコア10

test CHANGED
File without changes
test CHANGED
@@ -2,97 +2,153 @@
2
2
 
3
3
 
4
4
 
5
+ 言語はC#です。
6
+
7
+ Debug.Log()については表示が確認できました。
8
+
9
+
10
+
11
+ Debug.Log()の内容をTextに表示しようとすると、
12
+
13
+
14
+
5
- 言語はC#でVisualStudioでデバックし受信確認したものをUnityに組み込み編集しています。
15
+ > get_isActiveAndEnabled can only be called from the main thread.
16
+
17
+ Constructors and field initializers will be executed from the loading thread when loading a scene.
18
+
19
+ Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
20
+
21
+
22
+
23
+ というエラーが出ます。
6
24
 
7
25
 
8
26
 
9
27
  ```ここに言語を入力
10
28
 
11
- using UnityEngine;
29
+ private UdpClient receivingUdpClient;
12
30
 
13
- using System.Collections;
31
+ private IPEndPoint remoteIpEndPoint;
14
-
15
- using UnityEngine.UI;
16
-
17
- using System;
18
-
19
- using System.Net;
20
-
21
- using System.Net.Sockets;
22
32
 
23
33
 
24
34
 
25
- public class ON_OFF : MonoBehaviour
35
+ private Thread thread;
26
36
 
37
+
38
+
39
+ public Text text;
40
+
41
+
42
+
43
+ // port number
44
+
45
+ private int receivePort = 3610;
46
+
47
+
48
+
49
+ // Use this for initialization
50
+
51
+ void Start()
52
+
27
- {
53
+ {
54
+
55
+ try
56
+
57
+ {
58
+
59
+ receivingUdpClient = new UdpClient(receivePort);
60
+
61
+ }
62
+
63
+ catch (Exception e)
64
+
65
+ {
66
+
67
+ Debug.Log(e.ToString());
68
+
69
+ }
70
+
71
+
72
+
73
+ remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 3610);
74
+
75
+
76
+
77
+ // start the thread for receiving signals
78
+
79
+ thread = new Thread(new ThreadStart(ReceiveDataBytes));
80
+
81
+ thread.Start();
82
+
83
+
84
+
85
+ // debug
86
+
87
+ Debug.Log("Thread started");
88
+
89
+ }
28
90
 
29
91
 
30
92
 
31
93
 
32
94
 
33
-
34
-
35
- void Start()
95
+ void ReceiveDataBytes()
36
96
 
37
97
  {
38
98
 
99
+
100
+
101
+ while (true)
102
+
103
+ {
39
104
 
40
105
 
41
106
 
107
+ Debug.Log("Threading inside while");
108
+
109
+ // NOTE!: This blocks execution until a new message is received
110
+
111
+ Byte[] receivedBytes = receivingUdpClient.Receive(ref remoteIpEndPoint);
112
+
113
+ string receivedStringData = BitConverter.ToString(receivedBytes).Replace("-", string.Empty);
114
+
115
+
116
+
117
+ Debug.Log("受信データ: " + receivedStringData);
118
+
119
+ text.text = receivedStringData;
120
+
121
+
122
+
123
+ Thread.Sleep(8);
124
+
125
+ }
126
+
127
+
42
128
 
43
129
  }
44
130
 
45
131
 
46
132
 
47
- public void Receive()
133
+ void CloseClient()
48
134
 
49
135
  {
50
136
 
137
+ thread.Abort();
51
138
 
139
+ receivingUdpClient.Close();
52
140
 
53
- for (;;)
54
-
55
- {
141
+ }
56
-
57
- while (true)
58
-
59
- {
60
-
61
- var sender = new IPEndPoint(IPAddress.Any, 3610);
62
-
63
- var UDPsender = new UdpClient(sender);
64
142
 
65
143
 
66
144
 
67
- byte[] ReceiveData = UDPsender.Receive(ref sender);
145
+ void OnApplicationQuit()
68
146
 
147
+ {
69
148
 
70
-
71
- string data = BitConverter.ToString(ReceiveData).Replace("-", string.Empty);
72
-
73
-
74
-
75
- UDPsender.Close();
149
+ CloseClient();
76
-
77
-
78
-
79
- GetComponent<Text>().text = "受信データ:" + data;
80
-
81
-
82
-
83
-
84
-
85
- }
86
-
87
-
88
-
89
-
90
-
91
- }
92
150
 
93
151
  }
94
-
95
- }
96
152
 
97
153
  ```
98
154
 
@@ -100,8 +156,4 @@
100
156
 
101
157
 
102
158
 
103
-
104
-
105
-
106
-
107
159
  よろしくお願いします。

4

編集方法の追加

2015/11/10 14:34

投稿

XLanceX
XLanceX

スコア10

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- 言語はC#です。
5
+ 言語はC#でVisualStudioでデバックし受信確認したものをUnityに組み込み編集しています。
6
6
 
7
7
 
8
8
 

3

ソースコード追加

2015/11/04 06:23

投稿

XLanceX
XLanceX

スコア10

test CHANGED
File without changes
test CHANGED
@@ -6,4 +6,102 @@
6
6
 
7
7
 
8
8
 
9
+ ```ここに言語を入力
10
+
11
+ using UnityEngine;
12
+
13
+ using System.Collections;
14
+
15
+ using UnityEngine.UI;
16
+
17
+ using System;
18
+
19
+ using System.Net;
20
+
21
+ using System.Net.Sockets;
22
+
23
+
24
+
25
+ public class ON_OFF : MonoBehaviour
26
+
27
+ {
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+ void Start()
36
+
37
+ {
38
+
39
+
40
+
41
+
42
+
43
+ }
44
+
45
+
46
+
47
+ public void Receive()
48
+
49
+ {
50
+
51
+
52
+
53
+ for (;;)
54
+
55
+ {
56
+
57
+ while (true)
58
+
59
+ {
60
+
61
+ var sender = new IPEndPoint(IPAddress.Any, 3610);
62
+
63
+ var UDPsender = new UdpClient(sender);
64
+
65
+
66
+
67
+ byte[] ReceiveData = UDPsender.Receive(ref sender);
68
+
69
+
70
+
71
+ string data = BitConverter.ToString(ReceiveData).Replace("-", string.Empty);
72
+
73
+
74
+
75
+ UDPsender.Close();
76
+
77
+
78
+
79
+ GetComponent<Text>().text = "受信データ:" + data;
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
+
9
107
  よろしくお願いします。

2

質問内容

2015/11/04 06:07

投稿

XLanceX
XLanceX

スコア10

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- UnityでUDPデータを受信して、条件を付けてUIのTextに表示をしようと考えているのですが、UDP受信が関係する部分だけ表示することができません。
1
+ UnityでUDPデータを受信して、UIのTextに表示をしようと考えているのですが表示することができません。
2
2
 
3
3
 
4
4
 
@@ -6,170 +6,4 @@
6
6
 
7
7
 
8
8
 
9
- ```ここに言語を入力
10
-
11
- using UnityEngine;
12
-
13
- using System.Configuration;
14
-
15
- using UnityEngine.UI;
16
-
17
- using System;
18
-
19
- using System.Net;
20
-
21
- using System.Net.Sockets;
22
-
23
- using System.Collections.Generic;
24
-
25
-
26
-
27
- public class ON_OFF : MonoBehaviour
28
-
29
- {
30
-
31
- public Text text;
32
-
33
-
34
-
35
- void Start()
36
-
37
- {
38
-
39
-
40
-
41
-
42
-
43
- }
44
-
45
-
46
-
47
- public void Receive()
48
-
49
- {
50
-
51
- for (;;)
52
-
53
- {
54
-
55
- while (true)
56
-
57
- {
58
-
59
- var sender = new IPEndPoint(IPAddress.Any, 3610);
60
-
61
- var UDPsender = new UdpClient(sender);
62
-
63
-
64
-
65
- byte[] ReceiveData = UDPsender.Receive(ref sender);
66
-
67
-
68
-
69
- string data = BitConverter.ToString(ReceiveData).Replace("-", string.Empty);
70
-
71
-
72
-
73
- UDPsender.Close();
74
-
75
- }
76
-
77
- }
78
-
79
- }
80
-
81
-
82
-
83
- public void Text(string data)
84
-
85
- {
86
-
87
- Receive();
88
-
89
-
90
-
91
- string EHD1 = data.Substring(0, 2);
92
-
93
- string EHD2 = data.Substring(2, 2);
94
-
95
- string TID1 = data.Substring(4, 2);
96
-
97
- string TID2 = data.Substring(6, 2);
98
-
99
- string SEOJ1 = data.Substring(8, 2);
100
-
101
- string SEOJ2 = data.Substring(10, 2);
102
-
103
- string SEOJ3 = data.Substring(12, 2);
104
-
105
- string DEOJ1 = data.Substring(14, 2);
106
-
107
- string DEOJ2 = data.Substring(16, 2);
108
-
109
- string DEOJ3 = data.Substring(18, 2);
110
-
111
- string ESV = data.Substring(20, 2);
112
-
113
- string OPC = data.Substring(22, 2);
114
-
115
- string EPC = data.Substring(24, 2);
116
-
117
- string PDC = data.Substring(26, 2);
118
-
119
- string EDT1 = data.Substring(28, 2);
120
-
121
-
122
-
123
- if (DEOJ1.Equals("02") && DEOJ2.Equals("7C"))
124
-
125
- {
126
-
127
-
128
-
129
-
130
-
131
- if (EDT1.Equals("30"))
132
-
133
- {
134
-
135
- text.text = "ON";
136
-
137
-
138
-
139
- }
140
-
141
-
142
-
143
- else if (EDT1.Equals("31"))
144
-
145
- {
146
-
147
- text.text = "OFF";
148
-
149
- }
150
-
151
-
152
-
153
- }
154
-
155
-
156
-
157
- else
158
-
159
- {
160
-
161
- text.text = "Error";
162
-
163
- }
164
-
165
-
166
-
167
- }
168
-
169
- }
170
-
171
- ```
172
-
173
-
174
-
175
9
  よろしくお願いします。

1

2015/11/04 01:14

投稿

XLanceX
XLanceX

スコア10

test CHANGED
File without changes
test CHANGED
File without changes