You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
2.3 KiB

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;
using EduCoderTool;
using Newtonsoft.Json;
public class TestWebConnecter : MonoBehaviour
{
public Text text_rec;
public InputField inputField;
public Button button;
public MeshRenderer cubeMesh;
// Start is called before the first frame update
void Start()
{
WebConnecter.Singleton.dataHandle += ReceiveWebMsg;
button.onClick.AddListener(() =>
{
string str = inputField.text;
Debug.Log("send msg to web " + str);
WebConnecter.Singleton.SendDataToWeb(str);
});
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Root root = new Root();
root.state = "start";
root.data = "Blue";
string data = JsonConvert.SerializeObject(root);
ReceiveWebMsg(data);
}
if (Input.GetKeyDown(KeyCode.T))
{
TestJsonData testJson = new TestJsonData();
testJson.info = new TestInfo("jack",52,65);
testJson.key = "9523";
string data = JsonConvert.SerializeObject(testJson);
Debug.LogFormat("测试发送json数据到web端{0}",data);
WebConnecter.Singleton.SendDataToWeb(data);
}
}
void ReceiveWebMsg(string data)
{
text_rec.text = data;
Root root = JsonConvert.DeserializeObject<Root>(data);
if (root != null)
{
if(root.data == "Red")
{
cubeMesh.material.color = Color.red;
}
if(root.data == "Blue")
{
cubeMesh.material.color = Color.blue;
}
if(root.data == "White")
{
cubeMesh.material.color = Color.white;
}
}
}
}
public class TestJsonData
{
public string key;
public TestInfo info;
}
public class TestInfo
{
public string name;
public int age;
public float weight;
public TestInfo(string name,int age,float weight)
{
this.name = name;
this.age = age;
this.weight = weight;
}
}
public class Root
{
public string state;
public string data;
}