U3D_TobaccoWarehouseISMDTSy.../Assets/Scripts/YL/Fetchbox.cs

85 lines
3.1 KiB
C#

using System;
using System.Collections;
using UnityEngine;
using static InterfaceManager;
using DefaultNamespace;
using System.Collections.Generic;
public class Fetchbox : MonoBehaviour
{
/// <summary>
/// 获取箱子接口路径
/// </summary>
private string url;
/// <summary>
/// 烟箱子
/// </summary>
public GameObject box;
/// <summary>
/// 获取箱子接口
/// </summary>
public ShelfBoxModel shelfBoxModel = new ShelfBoxModel();
/// <summary>
/// 每个箱子的信息
/// </summary>
public List<Boxinformation> boxinformationList = new List<Boxinformation>();
private void Awake()
{
url += Boxface;
StartCoroutine(Getstring(url, (data) =>
{
Debug.Log(data);
Boxdata(data);
}));
}
/// <summary>
/// 解析箱子的数据
/// </summary>
public void Boxdata(string data)
{
if (data != null)
{
shelfBoxModel = JsonUtility.FromJson<ShelfBoxModel>(data);
for (int i = 0; i < shelfBoxModel.result.Count; i++)
{
GameObject Box = Instantiate(box);
Boxinformation boxinformation = Box.AddComponent<Boxinformation>();
string boxname = $"{shelfBoxModel.result[i].row}-{shelfBoxModel.result[i].column}-{shelfBoxModel.result[i].layer}";
Debug.Log(boxname);
GameObject parentObject = GameObject.Find(boxname);
if (parentObject)
{
Box.transform.SetParent(parentObject.transform, false);
boxinformation.ID = shelfBoxModel.result[i].id;
boxinformation.type = shelfBoxModel.result[i].type;
boxinformation.locationId = shelfBoxModel.result[i].locationId;
boxinformation.description = shelfBoxModel.result[i].description;
boxinformation.locationState = shelfBoxModel.result[i].locationState;
boxinformation.storageState = shelfBoxModel.result[i].storageState;
boxinformation.layer = shelfBoxModel.result[i].layer;
boxinformation.row = shelfBoxModel.result[i].row;
boxinformation.column = shelfBoxModel.result[i].column;
boxinformation.specialFlag = shelfBoxModel.result[i].specialFlag;
boxinformation.palletNum = shelfBoxModel.result[i].palletNum;
boxinformation.itemType = shelfBoxModel.result[i].itemType;
boxinformation.isSpecial = shelfBoxModel.result[i].isSpecial;
boxinformationList.Add(boxinformation);
if (shelfBoxModel.result[i].layer.Equals(1))
{
MqttManager.Instance.firstbox.Add(Box);
}
else
{
MqttManager.Instance.secondbox.Add(Box);
}
}
else
{
Destroy(Box);
Debug.LogWarning($"未找到名为 {boxname} 的父对象。物体将被删除。");
}
}
}
}
}