using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using Cysharp.Threading.Tasks; using DefaultNamespace; using DG.Tweening; using Newtonsoft.Json; using UnityEngine; using UnityEngine.UIElements; public class MqttManager : MonoSingleton { /// /// 所有设备的请求头 /// public List devices = new List(); /// /// 换电池装置 /// public List qcbdequipments = new List(); /// /// 查询提升机 /// public List elvequipments = new List(); /// /// 拆码垛机 /// public List dpmequipments = new List(); /// /// 输送机设备 /// public List convoyorequipments = new List(); /// /// 输入机设备 /// public List exportations = new List(); /// /// 小车设备 /// public List carequipments = new List(); /// /// 查询式落地机 /// public List feequipments = new List(); /// /// 查训加去盖信息 /// public List cmequipments = new List(); /// /// 消息队列 /// public ConcurrentQueue messageQueue = new ConcurrentQueue(); /// /// 第一层箱子 /// public List firstbox = new List(); /// /// 第二层箱子 /// public List secondbox = new List(); /// /// 小车位移的速度 /// public float speed = 3.5f; /// /// 需要渲染的箱子0 /// public GameObject BoxID; [Obsolete] private void Awake() { //启动MQTT连接 Application.ExternalCall("OpenHtmlMQTT"); ProcessQueue().Forget(); } private void Start() { } /// /// 接受MQTT消息 /// /// public void Message(string message) { // Debug.Log($"接收到的MQTT消息---->{message}"); messageQueue.Enqueue(message); // 将消息存入队列 } private async UniTaskVoid ProcessQueue() { while (true) { if (messageQueue.TryDequeue(out string message)) { // 处理消息 Debug.Log($"剩余:{messageQueue.Count}条数据,取数据-----> {message}"); //await UniTask.Delay(10); Allequipment str = JsonConvert.DeserializeObject(message); //Debug.Log("获取到了数据"); //Debug.Log(str.device + " " + str.message); devices.Add(str); string data = str.device.Substring(0, 2); switch (data) { case "qc": Qcbd qcbd = JsonConvert.DeserializeObject(str.message); qcbdequipments.Add(qcbd); break; case "el": Elvequipment elvequipment = JsonConvert.DeserializeObject(str.message); elvequipments.Add(elvequipment); break; case "dp": Dpmequipment dpmequipment = JsonConvert.DeserializeObject(str.message); dpmequipments.Add(dpmequipment); break; case "co": Convoyorequipment convoyorequipment = JsonConvert.DeserializeObject(str.message); string con = convoyorequipment.Id.Substring(0, 2); //Debug.Log(con); if (con == "54") { convoyorequipments.Add(convoyorequipment); } else { exportations.Add(convoyorequipment); } break; case "ca": Debug.Log(str.message); //Carequipment carequipment = JsonConvert.DeserializeObject(str.message); switch (str.device) { case "car1": Carequipment carequipment = JsonConvert.DeserializeObject(str.message); //string carname = str.device; Debug.Log(str.device); GameObject cargame = GameObject.Find("car1"); if (cargame) { Debug.Log("第一个小车运动"); Carpoint(cargame, carequipment); } break; case "car2": Carequipment carequipment2 = JsonConvert.DeserializeObject(str.message); //string carname = str.device; Debug.Log(str.device); GameObject cargame2 = GameObject.Find("car2"); if (cargame2) { Debug.Log("第二个小车运动"); Carpoint2(cargame2, carequipment2); } break; case "car3": Carequipment carequipment3 = JsonConvert.DeserializeObject(str.message); //string carname = str.device; Debug.Log(str.device); GameObject cargame3 = GameObject.Find("car3"); if (cargame3) { Debug.Log("第三个小车运动"); Carpoint3(cargame3, carequipment3); } break; case "car4": Carequipment carequipment4 = JsonConvert.DeserializeObject(str.message); //string carname = str.device; Debug.Log(str.device); GameObject cargame4 = GameObject.Find("car4"); if (cargame4) { Debug.Log("第四个小车运动"); Carpoint4(cargame4, carequipment4); } break; case "car5": Carequipment carequipment5 = JsonConvert.DeserializeObject(str.message); //string carname = str.device; Debug.Log(str.device); GameObject cargame5 = GameObject.Find("car5"); if (cargame5) { Debug.Log("第五个小车运动"); Carpoint5(cargame5, carequipment5); } break; case "car6": Carequipment carequipment6 = JsonConvert.DeserializeObject(str.message); //string carname = str.device; Debug.Log(str.device); GameObject cargame6 = GameObject.Find("car6"); if (cargame6) { Debug.Log("第六个小车运动"); Carpoint6(cargame6, carequipment6); } break; case "car7": Carequipment carequipment7 = JsonConvert.DeserializeObject(str.message); //string carname = str.device; Debug.Log(str.device); GameObject cargame7 = GameObject.Find("car7"); if (cargame7) { Debug.Log("第七个小车运动"); Carpoint7(cargame7, carequipment7); } break; case "car8": Carequipment carequipment8 = JsonConvert.DeserializeObject(str.message); //string carname = str.device; Debug.Log(str.device); GameObject cargame8 = GameObject.Find("car8"); if (cargame8) { Debug.Log("第八个小车运动"); Carpoint8(cargame8, carequipment8); } break; default: break; } //string carname = str.device; //Debug.Log(str.device); //GameObject cargame = GameObject.Find(carname); //if (cargame) //{ // Carpoint(cargame, carequipment); //} break; case "fe": Feequipment feequipment = JsonConvert.DeserializeObject(str.message); feequipments.Add(feequipment); break; case "cm": Cmequipment cmequipment = JsonConvert.DeserializeObject(str.message); cmequipments.Add(cmequipment); break; default: break; } await UniTask.Delay(10); } else { Debug.Log("等待数据"); await UniTask.Delay(1000); } } } /// /// 第一个小车 /// int index = 1; public void Carpoint(GameObject car, Carequipment carequipment) { Debug.Log(carequipment.id); if (car) { Carinformation carbox = car.GetComponent(); carbox.BoxCode = carequipment.CarState_BoxCode; string point = carequipment.CarState_Car_Row + "-" + carequipment.CarState_Car_Column + "-" + carequipment.CarState_Car_Layer; Debug.Log("打印小车点位" + point); if (GameObject.Find(point)) { GameObject displacement = GameObject.Find(point); if (displacement != null && index < 2) { car.transform.position = displacement.transform.position; index++; } else if (displacement != null) { Debug.Log(index); Designateddisplacement(car, displacement.transform.position, speed); Debug.Log("小车位移到指定点位"); } } if (carequipment.CarState_CarPlt == "2") { if (car.gameObject.transform.childCount == 0 && carequipment.CarState_BoxCode != "0") { if (BoxID) { GameObject box = Instantiate(BoxID, car.gameObject.transform); Boxinformation boxinformation = box.AddComponent(); boxinformation.ID = carequipment.CarState_BoxCode; Debug.Log("小车身上没有箱子,克隆出来一个箱子" + carequipment.CarState_BoxCode); if (box.gameObject != null) { // 获取触发器物体的碰撞体 Collider triggerCollider = car.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(car.gameObject.transform, false); Debug.Log("箱子位置初始化"); } else { Debug.LogError("触发器没有碰撞体"); } } if (carequipment.CarState_Car_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 if (carequipment.CarState_CarPlt == "1") { Debug.Log("小车身上没有箱子"); if (car.gameObject.transform.childCount > 0) { GameObject box = car.gameObject.transform.GetChild(0).gameObject; box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y + 0.2f, 0.4f).SetEase(Ease.InOutQuad).OnComplete(() => { if (box.GetComponent()) { Collider collider = box.GetComponent(); if (collider) { collider.enabled = false; Debug.Log("失活了箱子触发器"); box.gameObject.transform.SetParent(null); Debug.Log("把箱子父物体取消"); } Debug.Log("卸箱动画向上提"); box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y - 0.62f, 1f).SetEase(Ease.InOutQuad).OnComplete(() => { Debug.Log("卸箱动画向下提"); }); } }); } } //else //{ // BoxCollider cars = car.GetComponent(); //} //car.transform.DOMove(displacement.transform.position, Vector3.Distance(car.transform.position, displacement.transform.position) / speed).SetEase(Ease.InOutQuad); } } /// /// 第二个小车 /// /// /// int index2 = 1; public void Carpoint2(GameObject car, Carequipment carequipment) { Debug.Log(carequipment.id); if (car) { Carinformation carbox = car.GetComponent(); carbox.BoxCode = carequipment.CarState_BoxCode; string point = carequipment.CarState_Car_Row + "-" + carequipment.CarState_Car_Column + "-" + carequipment.CarState_Car_Layer; Debug.Log("打印小车点位" + point); if (GameObject.Find(point)) { GameObject displacement = GameObject.Find(point); if (displacement != null && index2 < 2) { car.transform.position = displacement.transform.position; index2++; } else if (displacement != null) { Debug.Log(index3); Designateddisplacement(car, displacement.transform.position, speed); Debug.Log("小车位移到指定点位"); } } if (carequipment.CarState_CarPlt == "2") { if (car.gameObject.transform.childCount == 0 && carequipment.CarState_BoxCode != "0") { if (BoxID) { GameObject box = Instantiate(BoxID, car.gameObject.transform); Boxinformation boxinformation = box.AddComponent(); boxinformation.ID = carequipment.CarState_BoxCode; Debug.Log("小车身上没有箱子,克隆出来一个箱子" + carequipment.CarState_BoxCode); if (box.gameObject != null) { // 获取触发器物体的碰撞体 Collider triggerCollider = car.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(car.gameObject.transform, false); Debug.Log("箱子位置初始化"); } else { Debug.LogError("触发器没有碰撞体"); } } if (carequipment.CarState_Car_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 if (carequipment.CarState_CarPlt == "1") { Debug.Log("小车身上没有箱子"); if (car.gameObject.transform.childCount > 0) { GameObject box = car.gameObject.transform.GetChild(0).gameObject; box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y + 0.2f, 0.4f).SetEase(Ease.InOutQuad).OnComplete(() => { if (box.GetComponent()) { Collider collider = box.GetComponent(); if (collider) { collider.enabled = false; Debug.Log("失活了箱子触发器"); box.gameObject.transform.SetParent(null); Debug.Log("把箱子父物体取消"); } Debug.Log("卸箱动画向上提"); box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y - 0.62f, 1f).SetEase(Ease.InOutQuad).OnComplete(() => { Debug.Log("卸箱动画向下提"); }); } }); } } //else //{ // BoxCollider cars = car.GetComponent(); //} //car.transform.DOMove(displacement.transform.position, Vector3.Distance(car.transform.position, displacement.transform.position) / speed).SetEase(Ease.InOutQuad); } } /// /// 第三个小车 /// /// /// int index3 = 1; public void Carpoint3(GameObject car, Carequipment carequipment) { Debug.Log(carequipment.id); if (car) { Carinformation carbox = car.GetComponent(); carbox.BoxCode = carequipment.CarState_BoxCode; string point = carequipment.CarState_Car_Row + "-" + carequipment.CarState_Car_Column + "-" + carequipment.CarState_Car_Layer; Debug.Log("打印小车点位" + point); if (GameObject.Find(point)) { GameObject displacement = GameObject.Find(point); if (displacement != null && index3 < 2) { car.transform.position = displacement.transform.position; index3++; } else if (displacement != null) { Debug.Log(index3); Designateddisplacement(car, displacement.transform.position, speed); Debug.Log("小车位移到指定点位"); } } if (carequipment.CarState_CarPlt == "2") { if (car.gameObject.transform.childCount == 0 && carequipment.CarState_BoxCode != "0") { if (BoxID) { GameObject box = Instantiate(BoxID, car.gameObject.transform); Boxinformation boxinformation = box.AddComponent(); boxinformation.ID = carequipment.CarState_BoxCode; Debug.Log("小车身上没有箱子,克隆出来一个箱子" + carequipment.CarState_BoxCode); if (box.gameObject != null) { // 获取触发器物体的碰撞体 Collider triggerCollider = car.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(car.gameObject.transform, false); Debug.Log("箱子位置初始化"); } else { Debug.LogError("触发器没有碰撞体"); } } if (carequipment.CarState_Car_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 if (carequipment.CarState_CarPlt == "1") { Debug.Log("小车身上没有箱子"); if (car.gameObject.transform.childCount > 0) { GameObject box = car.gameObject.transform.GetChild(0).gameObject; box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y + 0.2f, 0.4f).SetEase(Ease.InOutQuad).OnComplete(() => { if (box.GetComponent()) { Collider collider = box.GetComponent(); if (collider) { collider.enabled = false; Debug.Log("失活了箱子触发器"); box.gameObject.transform.SetParent(null); Debug.Log("把箱子父物体取消"); } Debug.Log("卸箱动画向上提"); box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y - 0.62f, 1f).SetEase(Ease.InOutQuad).OnComplete(() => { Debug.Log("卸箱动画向下提"); }); } }); } } //else //{ // BoxCollider cars = car.GetComponent(); //} //car.transform.DOMove(displacement.transform.position, Vector3.Distance(car.transform.position, displacement.transform.position) / speed).SetEase(Ease.InOutQuad); } } /// /// 第四个小车 /// /// /// int index4 = 1; public void Carpoint4(GameObject car, Carequipment carequipment) { Debug.Log(carequipment.id); if (car) { Carinformation carbox = car.GetComponent(); carbox.BoxCode = carequipment.CarState_BoxCode; string point = carequipment.CarState_Car_Row + "-" + carequipment.CarState_Car_Column + "-" + carequipment.CarState_Car_Layer; Debug.Log("打印小车点位" + point); if (GameObject.Find(point)) { GameObject displacement = GameObject.Find(point); if (displacement != null && index4 < 2) { car.transform.position = displacement.transform.position; index4++; } else if (displacement != null) { Debug.Log(index4); Designateddisplacement(car, displacement.transform.position, speed); Debug.Log("小车位移到指定点位"); } } if (carequipment.CarState_CarPlt == "2") { if (car.gameObject.transform.childCount == 0 && carequipment.CarState_BoxCode != "0") { if (BoxID) { GameObject box = Instantiate(BoxID, car.gameObject.transform); Boxinformation boxinformation = box.AddComponent(); boxinformation.ID = carequipment.CarState_BoxCode; Debug.Log("小车身上没有箱子,克隆出来一个箱子" + carequipment.CarState_BoxCode); if (box.gameObject != null) { // 获取触发器物体的碰撞体 Collider triggerCollider = car.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(car.gameObject.transform, false); Debug.Log("箱子位置初始化"); } else { Debug.LogError("触发器没有碰撞体"); } } if (carequipment.CarState_Car_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 if (carequipment.CarState_CarPlt == "1") { Debug.Log("小车身上没有箱子"); if (car.gameObject.transform.childCount > 0) { GameObject box = car.gameObject.transform.GetChild(0).gameObject; box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y + 0.2f, 0.4f).SetEase(Ease.InOutQuad).OnComplete(() => { if (box.GetComponent()) { Collider collider = box.GetComponent(); if (collider) { collider.enabled = false; Debug.Log("失活了箱子触发器"); box.gameObject.transform.SetParent(null); Debug.Log("把箱子父物体取消"); } Debug.Log("卸箱动画向上提"); box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y - 0.62f, 1f).SetEase(Ease.InOutQuad).OnComplete(() => { Debug.Log("卸箱动画向下提"); }); } }); } } //else //{ // BoxCollider cars = car.GetComponent(); //} //car.transform.DOMove(displacement.transform.position, Vector3.Distance(car.transform.position, displacement.transform.position) / speed).SetEase(Ease.InOutQuad); } } /// /// 第五个小车 /// /// /// int index5 = 1; public void Carpoint5(GameObject car, Carequipment carequipment) { Debug.Log(carequipment.id); if (car) { Carinformation carbox = car.GetComponent(); carbox.BoxCode = carequipment.CarState_BoxCode; string point = carequipment.CarState_Car_Row + "-" + carequipment.CarState_Car_Column + "-" + carequipment.CarState_Car_Layer; Debug.Log("打印小车点位" + point); if (GameObject.Find(point)) { GameObject displacement = GameObject.Find(point); if (displacement != null && index5 < 2) { car.transform.position = displacement.transform.position; index5++; } else if (displacement != null) { Debug.Log(index5); Designateddisplacement(car, displacement.transform.position, speed); Debug.Log("小车位移到指定点位"); } } if (carequipment.CarState_CarPlt == "2") { if (car.gameObject.transform.childCount == 0 && carequipment.CarState_BoxCode != "0") { if (BoxID) { GameObject box = Instantiate(BoxID, car.gameObject.transform); Boxinformation boxinformation = box.AddComponent(); boxinformation.ID = carequipment.CarState_BoxCode; Debug.Log("小车身上没有箱子,克隆出来一个箱子" + carequipment.CarState_BoxCode); if (box.gameObject != null) { // 获取触发器物体的碰撞体 Collider triggerCollider = car.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(car.gameObject.transform, false); Debug.Log("箱子位置初始化"); } else { Debug.LogError("触发器没有碰撞体"); } } if (carequipment.CarState_Car_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 if (carequipment.CarState_CarPlt == "1") { Debug.Log("小车身上没有箱子"); if (car.gameObject.transform.childCount > 0) { GameObject box = car.gameObject.transform.GetChild(0).gameObject; box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y + 0.2f, 0.4f).SetEase(Ease.InOutQuad).OnComplete(() => { if (box.GetComponent()) { Collider collider = box.GetComponent(); if (collider) { collider.enabled = false; Debug.Log("失活了箱子触发器"); box.gameObject.transform.SetParent(null); Debug.Log("把箱子父物体取消"); } Debug.Log("卸箱动画向上提"); box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y - 0.62f, 1f).SetEase(Ease.InOutQuad).OnComplete(() => { Debug.Log("卸箱动画向下提"); }); } }); } } //else //{ // BoxCollider cars = car.GetComponent(); //} //car.transform.DOMove(displacement.transform.position, Vector3.Distance(car.transform.position, displacement.transform.position) / speed).SetEase(Ease.InOutQuad); } } /// /// 第六个小车 /// /// /// int index6 = 1; public void Carpoint6(GameObject car, Carequipment carequipment) { Debug.Log(carequipment.id); if (car) { Carinformation carbox = car.GetComponent(); carbox.BoxCode = carequipment.CarState_BoxCode; string point = carequipment.CarState_Car_Row + "-" + carequipment.CarState_Car_Column + "-" + carequipment.CarState_Car_Layer; Debug.Log("打印小车点位" + point); if (GameObject.Find(point)) { GameObject displacement = GameObject.Find(point); if (displacement != null && index6 < 2) { car.transform.position = displacement.transform.position; index6++; } else if (displacement != null) { Debug.Log(index6); Designateddisplacement(car, displacement.transform.position, speed); Debug.Log("小车位移到指定点位"); } } if (carequipment.CarState_CarPlt == "2") { if (car.gameObject.transform.childCount == 0 && carequipment.CarState_BoxCode != "0") { if (BoxID) { GameObject box = Instantiate(BoxID, car.gameObject.transform); Boxinformation boxinformation = box.AddComponent(); boxinformation.ID = carequipment.CarState_BoxCode; Debug.Log("小车身上没有箱子,克隆出来一个箱子" + carequipment.CarState_BoxCode); if (box.gameObject != null) { // 获取触发器物体的碰撞体 Collider triggerCollider = car.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(car.gameObject.transform, false); Debug.Log("箱子位置初始化"); } else { Debug.LogError("触发器没有碰撞体"); } } if (carequipment.CarState_Car_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 if (carequipment.CarState_CarPlt == "1") { Debug.Log("小车身上没有箱子"); if (car.gameObject.transform.childCount > 0) { GameObject box = car.gameObject.transform.GetChild(0).gameObject; box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y + 0.2f, 0.4f).SetEase(Ease.InOutQuad).OnComplete(() => { if (box.GetComponent()) { Collider collider = box.GetComponent(); if (collider) { collider.enabled = false; Debug.Log("失活了箱子触发器"); box.gameObject.transform.SetParent(null); Debug.Log("把箱子父物体取消"); } Debug.Log("卸箱动画向上提"); box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y - 0.62f, 1f).SetEase(Ease.InOutQuad).OnComplete(() => { Debug.Log("卸箱动画向下提"); }); } }); } } //else //{ // BoxCollider cars = car.GetComponent(); //} //car.transform.DOMove(displacement.transform.position, Vector3.Distance(car.transform.position, displacement.transform.position) / speed).SetEase(Ease.InOutQuad); } } /// /// 第七个小车 /// /// /// int index7 = 1; public void Carpoint7(GameObject car, Carequipment carequipment) { Debug.Log(carequipment.id); if (car) { Carinformation carbox = car.GetComponent(); carbox.BoxCode = carequipment.CarState_BoxCode; string point = carequipment.CarState_Car_Row + "-" + carequipment.CarState_Car_Column + "-" + carequipment.CarState_Car_Layer; Debug.Log("打印小车点位" + point); if (GameObject.Find(point)) { GameObject displacement = GameObject.Find(point); if (displacement != null && index7 < 2) { car.transform.position = displacement.transform.position; index7++; } else if (displacement != null) { Debug.Log(index7); Designateddisplacement(car, displacement.transform.position, speed); Debug.Log("小车位移到指定点位"); } } if (carequipment.CarState_CarPlt == "2") { if (car.gameObject.transform.childCount == 0 && carequipment.CarState_BoxCode != "0") { if (BoxID) { GameObject box = Instantiate(BoxID, car.gameObject.transform); Boxinformation boxinformation = box.AddComponent(); boxinformation.ID = carequipment.CarState_BoxCode; Debug.Log("小车身上没有箱子,克隆出来一个箱子" + carequipment.CarState_BoxCode); if (box.gameObject != null) { // 获取触发器物体的碰撞体 Collider triggerCollider = car.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(car.gameObject.transform, false); Debug.Log("箱子位置初始化"); } else { Debug.LogError("触发器没有碰撞体"); } } if (carequipment.CarState_Car_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 if (carequipment.CarState_CarPlt == "1") { Debug.Log("小车身上没有箱子"); if (car.gameObject.transform.childCount > 0) { GameObject box = car.gameObject.transform.GetChild(0).gameObject; box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y + 0.2f, 0.4f).SetEase(Ease.InOutQuad).OnComplete(() => { if (box.GetComponent()) { Collider collider = box.GetComponent(); if (collider) { collider.enabled = false; Debug.Log("失活了箱子触发器"); box.gameObject.transform.SetParent(null); Debug.Log("把箱子父物体取消"); } Debug.Log("卸箱动画向上提"); box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y - 0.62f, 1f).SetEase(Ease.InOutQuad).OnComplete(() => { Debug.Log("卸箱动画向下提"); }); } }); } } //else //{ // BoxCollider cars = car.GetComponent(); //} //car.transform.DOMove(displacement.transform.position, Vector3.Distance(car.transform.position, displacement.transform.position) / speed).SetEase(Ease.InOutQuad); } } /// /// 第八个小车 /// /// /// int index8 = 1; public void Carpoint8(GameObject car, Carequipment carequipment) { Debug.Log(carequipment.id); if (car) { Carinformation carbox = car.GetComponent(); carbox.BoxCode = carequipment.CarState_BoxCode; string point = carequipment.CarState_Car_Row + "-" + carequipment.CarState_Car_Column + "-" + carequipment.CarState_Car_Layer; Debug.Log("打印小车点位" + point); if (GameObject.Find(point)) { GameObject displacement = GameObject.Find(point); if (displacement != null && index8 < 2) { car.transform.position = displacement.transform.position; index8++; } else if (displacement != null) { Debug.Log(index8); Designateddisplacement(car, displacement.transform.position, speed); Debug.Log("小车位移到指定点位"); } } if (carequipment.CarState_CarPlt == "2") { if (car.gameObject.transform.childCount == 0 && carequipment.CarState_BoxCode != "0") { if (BoxID) { GameObject box = Instantiate(BoxID, car.gameObject.transform); Boxinformation boxinformation = box.AddComponent(); boxinformation.ID = carequipment.CarState_BoxCode; Debug.Log("小车身上没有箱子,克隆出来一个箱子" + carequipment.CarState_BoxCode); if (box.gameObject != null) { // 获取触发器物体的碰撞体 Collider triggerCollider = car.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(car.gameObject.transform, false); Debug.Log("箱子位置初始化"); } else { Debug.LogError("触发器没有碰撞体"); } } if (carequipment.CarState_Car_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 if (carequipment.CarState_CarPlt == "1") { Debug.Log("小车身上没有箱子"); if (car.gameObject.transform.childCount > 0) { GameObject box = car.gameObject.transform.GetChild(0).gameObject; box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y + 0.2f, 0.4f).SetEase(Ease.InOutQuad).OnComplete(() => { if (box.GetComponent()) { Collider collider = box.GetComponent(); if (collider) { collider.enabled = false; Debug.Log("失活了箱子触发器"); box.gameObject.transform.SetParent(null); Debug.Log("把箱子父物体取消"); } Debug.Log("卸箱动画向上提"); box.gameObject.transform.DOMoveY(box.gameObject.transform.position.y - 0.62f, 1f).SetEase(Ease.InOutQuad).OnComplete(() => { Debug.Log("卸箱动画向下提"); }); } }); } } //else //{ // BoxCollider cars = car.GetComponent(); //} //car.transform.DOMove(displacement.transform.position, Vector3.Distance(car.transform.position, displacement.transform.position) / speed).SetEase(Ease.InOutQuad); } } /// /// 需要位移的地方 /// /// 小车 /// 位移到指定位置 /// 位移的速度 public void Designateddisplacement(GameObject car, Vector3 dis, float speed) { Debug.Log("小车运动了"); float times = Vector3.Distance(car.transform.position, dis) / speed; car.transform.DOMove(dis, times).SetEase(Ease.InOutQuad); } }