Commit c123ec5a authored by 杨泽宇's avatar 杨泽宇

更新

parent c1c32f74
using System.Collections;
using UnityEngine.Networking;
using UnityEngine;
public class LoginManager : MonoBehaviour
{
public string ProductionlineID;
private RobotPositionData positionData;
[Header("收到服务器信息事件")]
public ReceiveMsgEvent<object, string> OnReceiveMsg = new ReceiveMsgEvent<object, string>();
[Space]
[Header("获取的信息")]
public Vector3 position;
private void Awake()
{
OnReceiveMsg.AddListener(_getData);
}
private void Start()
{
StartCoroutine(GetPositionData());
}
protected void _getData(object data, string parameter)
{
if (data.GetType().Equals(typeof(RobotPositionData)))
{
RobotPositionData robotPositionData = (RobotPositionData)data;
position = new Vector3(robotPositionData.data.x, robotPositionData.data.y, robotPositionData.data.h);
Debug.Log(position);
}
}
private IEnumerator GetPositionData()
{
while (true)
{
using (UnityWebRequest request = UnityWebRequest.Get(ProductionlineID))
{
// 设置请求头
request.SetRequestHeader("Content-Type", "application/json");
// 发送请求并等待响应
yield return request.SendWebRequest();
if (!string.IsNullOrEmpty(request.error))
{
Debug.LogError(request.error);
}
else
{
// 解析响应数据
string responseJson = request.downloadHandler.text;
Debug.Log("Response: " + responseJson);
// 使用 JsonUtility 解析 JSON 数据
positionData = JsonUtility.FromJson<RobotPositionData>(responseJson);
OnReceiveMsg.Invoke(positionData, "RobotPositionData");
}
}
yield return new WaitForSeconds(1);
}
}
}
// 定义机器人位置数据结构
[System.Serializable]
public class RobotPositionData
{
public int code;
public string message;
public RobotPositionDataDetails data;
}
[System.Serializable]
public class RobotPositionDataDetails
{
public float x;
public float y;
public float h;
}
\ No newline at end of file
fileFormatVersion: 2
guid: 61673f6590d0ff745805f9319090354c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine.Events;
public class ReceiveMsgEvent<T> : UnityEvent<T> { }
public class ReceiveMsgEvent<T, K> : UnityEvent<T, K> { }
public class ReceiveMsgEvent : UnityEvent { }
\ No newline at end of file
fileFormatVersion: 2
guid: 99540299064230543b55b3ba77c168c5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -5640,6 +5640,11 @@ PrefabInstance:
propertyPath: UID
value: 1158373593
objectReference: {fileID: 0}
- target: {fileID: 6601457687186396561, guid: 1728ef96ff7b1b44f943e3c988d53d15,
type: 3}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
- target: {fileID: 6954307607829673240, guid: 1728ef96ff7b1b44f943e3c988d53d15,
type: 3}
propertyPath: UID
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment