using DefaultNamespace; using DG.Tweening; using HighlightPlus; using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using System.Runtime.ConstrainedExecution; using Unity.VisualScripting; using UnityEngine; using UnityEngine.UIElements; 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) { if (carequipment.CarState_Err01 != "" && carequipment.CarState_Err01 != "0") { HighlightEffect highlightEffect = transform.GetComponent(); highlightEffect.highlighted = true; } else { HighlightEffect highlightEffect = transform.GetComponent(); highlightEffect.highlighted = false; } 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 == "1" && currentPoint.childCount == 0 && transform.childCount == 0 && carequipment.JobState_BoxCode.Length > 0) { //小车初始化 Initbox(carequipment.JobState_BoxCode, carequipment.CarState_Car_Layer); } isInit = true; } else { Designateddisplacement(currentPoint.position, Speed); } if (carequipment.CarState_CarPlt == "1" && isInit) { if (carequipment.JobState_BoxCode != "0") { Encasement(carequipment.JobState_BoxCode, currentPoint); } } else if (carequipment.CarState_CarPlt == "2" && isInit) { 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 urls = Tasknumber + boxdata; StartCoroutine(Getstring(urls, (data) => { //Debug.Log("返回数据" + data); Initdata initdata = JsonUtility.FromJson(data); if (initdata != null && BoxID) { GameObject box = Instantiate(BoxID, transform); Boxtasknumber boxtasknumber = box.GetComponent(); boxtasknumber.ID = boxdata; //Debug.Log("小车初始化" + boxtasknumber.ID); switch (initdata.result[0].palletType) { case "Qbox": if (initdata.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; case "Box": if (initdata.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; 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(string id, Transform point) { if (point.childCount > 0) { GameObject box = point.GetChild(0).gameObject; if (true) { Boxtasknumber boxtasknumber = box.GetComponent(); Boxinformation boxinformation = box.GetComponent(); if (boxtasknumber && boxinformation) { Pointid(id, boxtasknumber, boxinformation); } } 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); } }); } }); } } } /// /// 接收箱子身上id /// /// public void Pointid(string id, Boxtasknumber boxtasknumber, Boxinformation boxinformation) { string url = Tasknumber + id; //Debug.Log("小车上箱子请求接口ID" + boxtasknumber.ID); StartCoroutine(Getstring(url, (data) => { //Debug.Log("小车上箱请求接口返回的数据" + data); Initdata initdata = JsonUtility.FromJson(data); boxinformation.locationId = initdata.result[0].dloc; boxtasknumber.ID = null; //Debug.Log("箱子上箱完毕小车身上箱子id" + boxtasknumber.ID + "小车身上locationId" + boxinformation.locationId); })); } }