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

195 lines
8.0 KiB
C#

using System;
using System.Collections;
using UnityEngine;
using static InterfaceManager;
using DefaultNamespace;
using System.Collections.Generic;
using cakeslice;
using System.Runtime.ConstrainedExecution;
public class Fetchbox : MonoBehaviour
{
/// <summary>
/// 获取箱子接口路径
/// </summary>
private string url;
/// <summary>
/// 烟箱子
/// </summary>
public GameObject box;
/// <summary>
/// 获取箱子接口
/// </summary>
public ShelfBoxModel shelfBoxModel = new ShelfBoxModel();
/// <summary>
/// 普通实箱
/// </summary>
public Material Commonfruitbox;
/// <summary>
/// 普通空箱
/// </summary>
public Material Normalairbox;
/// <summary>
/// 特殊实箱
/// </summary>
public Material Specialkernelbox;
/// <summary>
/// 特殊空箱
/// </summary>
public Material Specialbox;
private void Start()
{
Invoke("Numberofboxes", 0.5f);
//Numberofboxes();
}
public void Numberofboxes()
{
Debug.Log("获取过了");
url += Boxface;
StartCoroutine(Getstring(url, (data) =>
{
Boxdata(data);
}));
}
void Update()
{
//if (Input.GetMouseButtonDown(2))
//{
// if (MqttManager.Instance.boxinformationList.Count > 0)
// {
// for (int i = 0; i < MqttManager.Instance.boxinformationList.Count; i++)
// {
// Debug.Log("劲来了");
// if (MqttManager.Instance.boxinformationList[i].ID == "XSCS_01_010100100400100")
// {
// Debug.Log("删除");
// Destroy(MqttManager.Instance.boxinformationList[i].gameObject);
// MqttManager.Instance.boxinformationList.Remove(MqttManager.Instance.boxinformationList[i]);
// }
// }
// }
//}
}
/// <summary>
/// 解析箱子的数据
/// </summary>
public void Boxdata(string data)
{
if (data != null)
{
//Debug.Log(data);
shelfBoxModel = JsonUtility.FromJson<ShelfBoxModel>(data);
for (int i = 0; i < shelfBoxModel.result.Count; i++)
{
GameObject Box = Instantiate(box);
Boxinformation boxinformation = Box.GetComponent<Boxinformation>();
//Outline outline = Box.AddComponent<Outline>();
//outline.color = 1;
//GameObject childbox = Box.transform.GetChild(0).gameObject;
//Outline putline2 = childbox.AddComponent<Outline>();
//putline2.color = 1;
string boxname = $"{shelfBoxModel.result[i].row}-{shelfBoxModel.result[i].column}-{shelfBoxModel.result[i].layer}";
//Debug.Log(boxname);
GameObject parentObject = GameObject.Find(boxname);
if (parentObject)
{
//MqttManager.Instance.boxinpots.Add(boxname);
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;
switch (shelfBoxModel.result[i].isSpecial)
{
case 0:
if (shelfBoxModel.result[i].itemType == "EmptyBox")
{
Renderer renderer = Box.GetComponent<Renderer>();
Renderer renderer1 = Box.transform.GetChild(0).GetComponent<Renderer>();
if (renderer != null)
{
renderer.material = Specialbox;
}
if (renderer1 != null)
{
renderer1.material = Specialbox;
}
}
else
{
Renderer renderer2 = Box.GetComponent<Renderer>();
Renderer renderer3 = Box.transform.GetChild(0).GetComponent<Renderer>();
if (renderer2 != null)
{
renderer2.material = Specialkernelbox;
}
if (renderer3 != null)
{
renderer3.material = Specialkernelbox;
}
}
break;
case 1:
if (shelfBoxModel.result[i].itemType == "EmptyBox")
{
Renderer renderer = Box.GetComponent<Renderer>();
Renderer renderer1 = Box.transform.GetChild(0).GetComponent<Renderer>();
if (renderer != null)
{
renderer.material = Normalairbox;
}
if (renderer1 != null)
{
renderer1.material = Normalairbox;
}
}
else
{
Renderer renderer2 = Box.GetComponent<Renderer>();
Renderer renderer3 = Box.transform.GetChild(0).GetComponent<Renderer>();
if (renderer2 != null)
{
renderer2.material = Commonfruitbox;
}
if (renderer3 != null)
{
renderer3.material = Commonfruitbox;
}
}
break;
default:
break;
}
MqttManager.Instance.boxinformationList.Add(boxinformation);
if (shelfBoxModel.result[i].layer.Equals(1))
{
GameObject childbox = Box.transform.GetChild(0).gameObject;
MqttManager.Instance.firstbox.Add(childbox);
MqttManager.Instance.firstbox.Add(Box);
}
else
{
GameObject childbox = Box.transform.GetChild(0).gameObject;
MqttManager.Instance.secondbox.Add(childbox);
MqttManager.Instance.secondbox.Add(Box);
}
}
else
{
//Destroy(Box);
//Debug.LogWarning($"未找到名为 {boxname} 的父对象。物体将被删除。");
}
}
}
}
}