Unity text box parses and reads JSON data from mqtt server

This content is about how to display the "value" in the JSON key-value pair in the text box control of Unity3D with the data received from the mqtt server.

need:

1.GameObject——>UI——>Text (put the text box control of Unity 3D in the scene)

Namespace reference: using UnityEngine.UI;

2. Newtonsoft plugin

Namespace reference: Newtonsoft.Json.Linq;

3. MQTT communication requirements:

(1) http://www.hslcommunication.cn/ Hugong Technology Download Resource Library

(2) Drag the two DLL files into Unity's scripts folder (Newtonsoft is added differently for Unity version 2021 and above, otherwise it seems that an error will be reported, and Unity already comes with it)

 

code:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

using HslCommunication.MQTT;

using System.Text;

using System.Diagnostics;

using System;

using System.IO;

using Newtonsoft.Json.Linq;

public class text_transfer : MonoBehaviour

{

    private MqttClient mqttClient;

    public  Text timeText; //Definition in unity

    public string TimeText;

    // Start is called before the first frame update

    void Start()

    {

        timeText = GameObject.Find("Canvas/Text").GetComponent<Text>();

        mqttClient = new MqttClient(new MqttConnectionOptions()

        {

            ClientId = "ABC" ,                      // The unique ID information of the client

            IpAddress = "192.168.1.126" ,               // the address of the mqtt server

        });

        // connect to the server

        HslCommunication.OperateResult connect = mqttClient.ConnectServer();

        if (connect.IsSuccess)

        {

            // connection succeeded

            UnityEngine.Debug.Log( "connected successfully" );

        }

        else

        {

            // The connection failed, you need to reconnect after a while

            UnityEngine.Debug.Log( "Connection failed" );

        }

        // then add the subscription

        HslCommunication.OperateResult sub = mqttClient.SubscribeMessage("test");

        if (sub.IsSuccess)

        {

            // SUCCESSFUL SUBSCRIBE

            UnityEngine.Debug.Log( "Subscription succeeded" );

        }

        else

        {

            // Subscription failed

            UnityEngine.Debug.Log( "Subscription failed" );

        }

        // Subscription example

        mqttClient.OnMqttMessageReceived += (MqttClient client, string topic, byte[] payload) =>

        {

            TimeText = Encoding.UTF8.GetString(payload);

            UnityEngine.Debug.Log("Time:" + DateTime.Now.ToString());

            UnityEngine.Debug.Log("Topic:" + topic);

            UnityEngine.Debug.Log("Payload:" + Encoding.UTF8.GetString(payload));

            //timeText.text = TimeText;

        };      

    }

    private void Update()

    {

        string json = TimeText;

        JObject obj = JObject.Parse(json);

        timeText.text = obj[ "speed 1" ].ToString(); //Assign the value of the corresponding key to the text box

        UnityEngine.Debug.Log(timeText.text); //Print the content of the value in the text box

        //UnityEngine.Debug.Log(Convert.ToInt64(timeText.text));

    }

}

1. Define the text box function

2. Receive JSON data packet

3. Analysis

 

4. Multiple text boxes can be used to receive data corresponding to requirements, or all data can be displayed in one text box

A single text box displays:

 

 

 Corresponding display of multiple text boxes: set more text box variables and repeat the previous operation

Guess you like

Origin blog.csdn.net/weixin_57716672/article/details/127526414