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

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

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

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

UDP

UDP(User Datagram Protocol)とは、トランスポート層のプロトコルであり、コネクション型のデータサービスです。IPネットワーク上の別のホストにコンピュータのアプリケーションがメッセージを送ることができ、転送チャンネルやデータ経路を設定する必要はありません。TCPに比べて高速であるが、信頼性が薄いという特徴があります。

Unity

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

Q&A

解決済

3回答

4040閲覧

Unity5のテキスト表示について

XLanceX

総合スコア10

C#

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

UDP

UDP(User Datagram Protocol)とは、トランスポート層のプロトコルであり、コネクション型のデータサービスです。IPネットワーク上の別のホストにコンピュータのアプリケーションがメッセージを送ることができ、転送チャンネルやデータ経路を設定する必要はありません。TCPに比べて高速であるが、信頼性が薄いという特徴があります。

Unity

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

0グッド

0クリップ

投稿2015/11/03 18:00

編集2015/11/10 14:36

UnityでUDPデータを受信して、UIのTextに表示をしようと考えているのですが表示することができません。

言語はC#です。
11/10 Debug.Log()については表示が確認できました。

Debug.Log()の内容をTextに表示しようとすると、

get_isActiveAndEnabled can only be called from the main thread.

Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

というエラーが出ます。

private UdpClient receivingUdpClient; private IPEndPoint remoteIpEndPoint; private Thread thread; public Text text; // port number private int receivePort = 3610; // Use this for initialization void Start() { try { receivingUdpClient = new UdpClient(receivePort); } catch (Exception e) { Debug.Log(e.ToString()); } remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 3610); // start the thread for receiving signals thread = new Thread(new ThreadStart(ReceiveDataBytes)); thread.Start(); // debug Debug.Log("Thread started"); } void ReceiveDataBytes() { while (true) { Debug.Log("Threading inside while"); // NOTE!: This blocks execution until a new message is received Byte[] receivedBytes = receivingUdpClient.Receive(ref remoteIpEndPoint); string receivedStringData = BitConverter.ToString(receivedBytes).Replace("-", string.Empty); Debug.Log("受信データ: " + receivedStringData); text.text = receivedStringData; Thread.Sleep(8); } } void CloseClient() { thread.Abort(); receivingUdpClient.Close(); } void OnApplicationQuit() { CloseClient(); }

よろしくお願いします。

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

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

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

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

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

guest

回答3

0

こちらの方法で受け取りができるようです。
http://qiita.com/nenjiru/items/8fa8dfb27f55c0205651

Jake さんのは ugui での設定方法になります。
データを受け取った後に行うやつですね。

投稿2015/11/04 02:06

tanamochi

総合スコア83

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

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

XLanceX

2015/11/04 06:20

回答ありがとうございます。 ソースコードあげてなかったため、ソースコードをあげました。 送受信の確認はWireSharkでしているのですが、WireSharkを見た限りでは、受信はできているようです。
guest

0

自己解決

http://tsubakit1.hateblo.jp/entry/20121204/1354631826
↑のサイトを参考に、Spicy Pixelというアセットを利用することでテキストに表示することができました。

ありがとうございました。

using UnityEngine; using UnityEngine.UI; using System; using System.Collections; using System.Net; using System.Net.Sockets; using System.Threading; using SpicyPixel.Threading; using SpicyPixel.Threading.Tasks; public class UDPtest : ConcurrentBehaviour { private UdpClient receivingUdpClient; private IPEndPoint remoteIpEndPoint; private Thread thread; public Text text; private int receivePort = 3610; void Start() { try { receivingUdpClient = new UdpClient(receivePort); } catch (Exception e) { Debug.Log(e.ToString()); } remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 3610); thread = new Thread(new ThreadStart(ReceiveDataBytes)); thread.Start(); } void ReceiveDataBytes() { for (;;) { while (true) { Byte[] receivedBytes = receivingUdpClient.Receive(ref remoteIpEndPoint); string receivedStringData = BitConverter.ToString(receivedBytes).Replace("-", string.Empty); Debug.Log("受信データ: " + receivedStringData); taskFactory.StartNew(Receive(receivedStringData)); } } } IEnumerator Receive(string receivedStringData) { text.text = receivedStringData; yield return null; } void CloseClient() { thread.Abort(); receivingUdpClient.Close(); } void OnApplicationQuit() { CloseClient(); } }

投稿2015/11/16 12:28

XLanceX

総合スコア10

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

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

0

GameObject(空)
+Canvas
+Text
というオブジェクト構成と仮定して、CanvasにScriptを追加して動かしましたが、「テキストの変更」という事でしたらこれで動きます。

C#

1using UnityEngine; 2using UnityEngine.UI; 3using System.Collections; 4 5public class addText : MonoBehaviour { 6 // Use this for initialization 7 void Start () { 8 GetComponent<Text>().text = "テキスト"; 9 } 10 11}

これで問題の解決になりますか?

投稿2015/11/04 01:30

Jake

総合スコア289

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

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

XLanceX

2015/11/04 06:19 編集

回答ありがとうございます。 ソースコードあげてなかったため、ソースコードをあげました。 テキストに文字を表示することについてはJakeさんの方法でできたのですが、UDPで受信したデータをテキストに表示しようとするとうまくできませんでした。
Jake

2015/11/04 08:15

debug.log(data)ではどんな内容が出ますか?
XLanceX

2015/11/04 13:31 編集

Debug.Log(data);を追加したところ、 Console画面には何も表示されませんでした。
Jake

2015/11/04 14:33

という事は… BitConvert.ToStringの下りはただbyteを文字列にしてるだけのはずなので、接続自体がうまく行っていないのかな…? UDP部分はちょっと理解が浅いので、他の方にお任せします…!
XLanceX

2015/11/04 14:51

ありがとうございました。 接続部分も見直してみます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問