数字孪生的搭建,并利用mqtt实现物联网设备通信、控制
1.unity 使用C#脚本实现MQTT通信
(6条消息) MQTT学习(四)--使用m2mqtt在Unity3D中实现MQTT客户端_lordwish的专栏-CSDN博客
C#代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Text;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
public class Move : MonoBehaviour
{
private MqttClient client;
void Start()
{
client = new MqttClient("your_ip");
client.Connect("user_name");
client.Subscribe(new string[] { "topic" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
client.Subscribe(new string[] { "xsd" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.P))
{
client.Publish("xsd", Encoding.UTF8.GetBytes("VW180 = 1"));
print("已发送开锁信息指令");
}
}
private void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
string tmp = System.Text.Encoding.UTF8.GetString(e.Message);
Debug.Log("实时信息反馈" + tmp);
}
}
2.unity 场景搭建资源
(6条消息) Unity免费的优质场景资源_wzpddh的博客-CSDN博客_unity场景
人气免费资源 - Asset Store (unity.com)
3.第三人称小车控制
导入官方资源包即可。注意在windows-pakage manager中下载导入。在别的包类型管理中可导入别的系统包。
4.遇到的问题
4.1子线程无法访问主线程
将对unity中物体属性的修改应该放到Updata()等默认执行线程中,代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Net;
using System.Text;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
public class LOCKUI : MonoBehaviour
{
public Button button_lock;
public GameObject panel_lock;
public Button button_exit;
public Text text;
private MqttClient client;
public string message;
//public Text realtime_info;
// Start is called before the first frame update
private void Start()
{
button_lock.onClick.AddListener(button_lock_Click);
button_exit.onClick.AddListener(button_exit_Click);
client = new MqttClient("your_ip");
print(client.Connect("user_name"));
client.Subscribe(new string[] { "xsd" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
client.Subscribe(new string[] { "test" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
}
private void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
message = System.Text.Encoding.UTF8.GetString(e.Message);
Debug.Log("实时信息反馈" + message);
}
private void button_lock_Click()
{
client.Publish("xsd", Encoding.UTF8.GetBytes("VW180 = 1"));
message = "已发送开锁信息指令";
}
private void button_exit_Click()
{
panel_lock.SetActive(false);
}
private void Update()
{
text.text = message;
}
}
4.2 物体属性无法修改
注意脚本生命周期中默认执行函数的大小写!!!
4.3可以发送mqtt消息,但无法打开柜子
柜子网关配置连接异常
4.4world space模式下的canvas调整大小
一般调整像素大小以适应场景。画布拥有的是Rectangle Transform
4.5list map形式的字符串转list map格式
使用Json.NET - Newtonsoft,但是通过VS包管理器导入后不支持unity。由于unity(目前是用unity5.5)目前最高支持.net framework 版本是3.5,所以无法直接通过NuGet导入Newtonsoft.Json来使用。可以访问GitHub下载相应的支持Unity的Newtonsoft.Json资源包,导入即可使用。(https://github.com/SaladLab/Json.Net.Unity3D/releases)
{"DeviceID":"121211","guanhao1":"1","height_data1":"19069","temp1":"197","hum1":"612","dp1":"125","guanhao2":"2","height_data2":"8652","temp2":"197","hum2":"612","dp2":"125","guanhao3":"3","height_data3":"29060","temp3":"197","hum3":"612","dp3":"125","guanhao4":"4","height_data4":"8602","temp4":"197","hum4":"612","dp4":"125","guanhao5":"5","height_data5":"8605","temp5":"197","hum5":"612","dp5":"125","guanhao6":"6","height_data6":"10000","temp6":"197","hum6":"612","dp6":"125","guanhao7":"7","height_data7":"23863","temp7":"197","hum7":"612","dp7":"125","guanhao8":"8","height_data8":"0","temp8":"197","hum8":"612","dp8":"125","Lock9":"9","StateOn_Off9":"1","temp9":"197","hum9":"612","dp9":"125"}
导入后使用Var Object = JsonConvert.DeserializeObject
public class CharacterListItem
{
public int Id { get; set; }
public string Name { get; set; }
public int Level { get; set; }
public string Class { get; set; }
public string Sex { get; set; }
}
4.6 GUI无法触发
脚本依附的GUI上层还有别的GUI, OnMouseDown不可以触发
4.7eventTrigger
给对应UI添加eventTrigger组件,拖动绑定物体,绑定物体中脚本里的函数(需要public)
4.8使用UMP读取rtmp流
(11条消息) Unity3d使用UMP插件播放网络视频流(rtsp、rtmp、onvif)_zouxin_88的专栏-CSDN博客
1.下载资源包并导入。将Assets\UniversalMediaPlayer\Prefabs\UniversalMediaPlayer.prefab拖入到场景中。
2.在UI的Canvas下创建RawImage,并添加ARVideoCanvasHelper组件。
在RawImage组件中,将Material设为为ARVideoCanvasMaterial。
在ARVideoCanvasHelper组件中, 将MediaPlayer设为UniversalMediaPlayer
3.选中UniversalMediaPlayer,在UniversalMediaPlayer组件中,设置Renddering GameObjects数组,个数为1,Elelement 0为RawImage。填入Path to video file为播放地址。