【发布时间】:2019-05-03 17:39:49
【问题描述】:
我正在尝试构建一个简单的 MQTT 应用程序。我想用 Unity 在 3dText 中显示收到的 MQTT 消息。
我将字符串赋值放入 Update() 函数中,但显然当收到消息时 Update() 函数已停止。当我单击编辑器中的某个位置时,Update() 函数会唤醒并更新字符串。
C#
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Net;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using uPLibrary.Networking.M2Mqtt.Utility;
using uPLibrary.Networking.M2Mqtt.Exceptions;
using System;
public class mqttTest : MonoBehaviour {
private MqttClient client;
public TextMesh mytext=null;
string msg;
// Use this for initialization
void Start () {
// create client instance
client = new MqttClient(IPAddress.Parse("192.168.83.128"),1883 , false , null );
// register to message received
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId);
client.Subscribe(new string[] { "test" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
}
void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
msg = System.Text.Encoding.UTF8.GetString(e.Message);
Debug.Log("Received: " + msg );
}
// Update is called once per frame
void Update () {
Debug.Log("update");
mytext.text = msg;
}
}
这个停止是由于开发环境还是事件机制?
【问题讨论】: