using DefaultNamespace; using DG.Tweening; using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using System.Runtime.ConstrainedExecution; using UnityEngine; using static InterfaceManager; public class Carinformation : MonoBehaviour { /// /// 小车ID /// public string ID; /// /// 小车名字 /// public string Name; /// /// 需要渲染的箱子0 /// public GameObject BoxID; /// /// 普通实箱 /// public Material Commonfruitbox; /// /// 普通空箱 /// public Material Normalairbox; /// /// 特殊实箱 /// public Material Specialkernelbox; /// /// 特殊空箱 /// public Material Specialbox; /// /// 是否初始化 /// private bool isInit = false; /// /// 小车速度 /// public float Speed = 3.5f; void Start() { } void Update() { } /// /// 点位,初始发,是否装箱子,是否卸箱子 /// /// public void Setbox(Carequipment carequipment) { string pointName = carequipment.CarState_Car_Row + "-" + carequipment.CarState_Car_Column + "-" + carequipment.CarState_Car_Layer; if (MqttManager.Instance.GetTransformByName(pointName)) { Transform currentPoint = MqttManager.Instance.GetTransformByName(pointName); if (currentPoint && !isInit) { SetPoint(currentPoint); if (carequipment.CarState_CarPlt == "2" && currentPoint.childCount == 0 && transform.childCount == 0 && carequipment.JobState_BoxCode != "0") { Initbox(carequipment.JobState_BoxCode, carequipment.CarState_Car_Layer); } isInit = true; } else { Designateddisplacement(currentPoint.position, Speed); } if (carequipment.CarState_CarPlt == "2") { Encasement(currentPoint); } else if (carequipment.CarState_CarPlt == "1") { UnloadBox(currentPoint); } } } /// /// 小车移动动画 /// /// /// public void Designateddisplacement(Vector3 dis, float speed) { Debug.Log("小车运动了"); float times = Vector3.Distance(transform.position, dis) / speed; transform.DOMove(dis, times).SetEase(Ease.InOutQuad); } /// /// 初始化赋值 /// /// public void SetPoint(Transform point) { transform.position = point.position; } /// /// 初始化箱子把箱子放到小车上并判断生成箱子颜色 /// /// public void Initbox(string boxdata, string layer) { string url = Initializes + boxdata; Debug.Log("箱子ID" + boxdata); Debug.Log("箱子请求路径" + url); StartCoroutine(Getstring(url, (data) => { Debug.Log("初始发对应的箱子数据" + data); ShelfBoxModel shelfBoxModel = JsonUtility.FromJson(data); if (shelfBoxModel != null && BoxID) { GameObject box = Instantiate(BoxID, transform); Boxinformation boxinformation = box.AddComponent(); if (boxinformation != null) { boxinformation.ID = shelfBoxModel.result[0].id; boxinformation.type = shelfBoxModel.result[0].type; boxinformation.locationId = shelfBoxModel.result[0].locationId; boxinformation.description = shelfBoxModel.result[0].description; boxinformation.locationState = shelfBoxModel.result[0].locationState; boxinformation.storageState = shelfBoxModel.result[0].storageState; boxinformation.layer = shelfBoxModel.result[0].layer; boxinformation.row = shelfBoxModel.result[0].row; boxinformation.column = shelfBoxModel.result[0].column; boxinformation.specialFlag = shelfBoxModel.result[0].specialFlag; boxinformation.palletNum = shelfBoxModel.result[0].palletNum; boxinformation.itemType = shelfBoxModel.result[0].itemType; boxinformation.isSpecial = shelfBoxModel.result[0].isSpecial; switch (shelfBoxModel.result[0].isSpecial) { case 0: if (shelfBoxModel.result[0].itemType == "EmptyBox") { Renderer renderer = box.GetComponent(); Renderer renderer1 = box.transform.GetChild(0).GetComponent(); if (renderer != null) { renderer.material = Specialbox; } if (renderer1 != null) { renderer1.material = Specialbox; } } else { Renderer renderer2 = box.GetComponent(); Renderer renderer3 = box.transform.GetChild(0).GetComponent(); if (renderer2 != null) { renderer2.material = Specialkernelbox; } if (renderer3 != null) { renderer3.material = Specialkernelbox; } } break; case 1: if (shelfBoxModel.result[0].itemType == "EmptyBox") { Renderer renderer = box.GetComponent(); Renderer renderer1 = box.transform.GetChild(0).GetComponent(); if (renderer != null) { renderer.material = Normalairbox; } if (renderer1 != null) { renderer1.material = Normalairbox; } } else { Renderer renderer2 = box.GetComponent(); Renderer renderer3 = box.transform.GetChild(0).GetComponent(); if (renderer2 != null) { renderer2.material = Commonfruitbox; } if (renderer3 != null) { renderer3.material = Commonfruitbox; } } break; default: break; } Collider triggerCollider = GetComponent(); // 确保触发器物体有碰撞体 if (triggerCollider != null) { // 计算触发器物体顶部的世界坐标 Vector3 triggerPosition = triggerCollider.bounds.center; Vector3 triggerTop = triggerPosition + Vector3.up * (triggerCollider.bounds.extents.y); // 设置物体的位置 box.gameObject.transform.localEulerAngles = new Vector3(90, 0, 0); box.gameObject.transform.position = triggerTop; box.gameObject.transform.SetParent(transform, false); Debug.Log("箱子位置初始化"); MqttManager.Instance.Deletebox(boxinformation.ID, boxinformation); } else { Debug.LogError("触发器没有碰撞体"); } } if (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); } } })); } /// /// 执行上箱 /// /// public void Encasement(Transform point) { if (point.childCount > 0) { Collider collider = point.GetChild(0).gameObject.GetComponent(); if (collider != null) { collider.isTrigger = true; } } } /// /// 执行卸箱 /// public void UnloadBox(Transform point) { if (transform.childCount > 0) { GameObject box = transform.GetChild(0).gameObject; if (box != null) { box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y + 0.2f, 0.2f).SetEase(Ease.InOutQuad).OnComplete(() => { if (box.GetComponent()) { Collider collider = box.GetComponent(); if (collider != null) { collider.isTrigger = false; box.gameObject.transform.SetParent(null); } box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y - 0.62f, 0.6f).SetEase(Ease.InOutQuad).OnComplete(() => { if (point) { box.gameObject.transform.SetParent(point); } }); } }); } } } }