C#、unity 和 Mqtt
C#,unity and Mqtt
你好,我刚开始接触 C#。我正在使用 C# 统一学生项目。通常所有代码都适用于 private void ToggleUI (bool _value) 函数。一旦到达 private void Subscribe (),问题就开始了。当我编译我的代码时,我收到一个我不理解的错误“找不到类型或命名空间名称 MqttMsgPublishEventArgs”。这是有问题的代码。你能给我一些线索吗?
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class Waypoint1 : MonoBehaviour
{
private Image iconImg;
private Text distanceText;
public Transform player;
public Transform target;
public Camera cam;
public float closeEnoughDist;
private void Start()
{
iconImg=GetComponent<Image>();
distanceText=GetComponentInChildren<Text>();
}
private void Update()
{
if(target != null)
target.position = new Vector3(7f, 0, 5f);
GetDistance();
CheckOnScreen();
}
private void GetDistance()
{
float dist=Vector3.Distance(player.position, target.position);
distanceText.text=dist.ToString("f1") +"m";
if(dist<closeEnoughDist)
Destroy(gameObject);
}
private void CheckOnScreen()
{
float thing=Vector3.Dot((target.position-cam.transform.position).normalized, cam.transform.forward);
if(thing<=0)
ToggleUI(false);
else
ToggleUI(true);
transform.position=cam.WorldToScreenPoint(target.position);
}
private void ToggleUI(bool _value)
{
iconImg.enabled=_value;
distanceText.enabled=_value;
}
private void Subscribe()
{
// create client instance (both host name and IP address work nicely)
MqttClient client = new MqttClient("<10.214.0.163>");
// register to message received
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId);
// subscribe to the topic "unity" with QoS 2
client.Subscribe(new string[] { "unity" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
}
void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
// handle message received
Console.WriteLine("Received = " + Encoding.UTF8.GetString(e.Message) + " on topic " + e.Topic);
}
}
您缺少这些 using 语句:
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
这是一个示例项目,应该可以帮助您解决问题:
https://github.com/gpvigano/M2MqttUnity
你好,我刚开始接触 C#。我正在使用 C# 统一学生项目。通常所有代码都适用于 private void ToggleUI (bool _value) 函数。一旦到达 private void Subscribe (),问题就开始了。当我编译我的代码时,我收到一个我不理解的错误“找不到类型或命名空间名称 MqttMsgPublishEventArgs”。这是有问题的代码。你能给我一些线索吗?
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class Waypoint1 : MonoBehaviour
{
private Image iconImg;
private Text distanceText;
public Transform player;
public Transform target;
public Camera cam;
public float closeEnoughDist;
private void Start()
{
iconImg=GetComponent<Image>();
distanceText=GetComponentInChildren<Text>();
}
private void Update()
{
if(target != null)
target.position = new Vector3(7f, 0, 5f);
GetDistance();
CheckOnScreen();
}
private void GetDistance()
{
float dist=Vector3.Distance(player.position, target.position);
distanceText.text=dist.ToString("f1") +"m";
if(dist<closeEnoughDist)
Destroy(gameObject);
}
private void CheckOnScreen()
{
float thing=Vector3.Dot((target.position-cam.transform.position).normalized, cam.transform.forward);
if(thing<=0)
ToggleUI(false);
else
ToggleUI(true);
transform.position=cam.WorldToScreenPoint(target.position);
}
private void ToggleUI(bool _value)
{
iconImg.enabled=_value;
distanceText.enabled=_value;
}
private void Subscribe()
{
// create client instance (both host name and IP address work nicely)
MqttClient client = new MqttClient("<10.214.0.163>");
// register to message received
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId);
// subscribe to the topic "unity" with QoS 2
client.Subscribe(new string[] { "unity" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
}
void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
// handle message received
Console.WriteLine("Received = " + Encoding.UTF8.GetString(e.Message) + " on topic " + e.Topic);
}
}
您缺少这些 using 语句:
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
这是一个示例项目,应该可以帮助您解决问题: https://github.com/gpvigano/M2MqttUnity