using DefaultNamespace; using System.Collections; using System.Collections.Generic; using TMPro; using Unity.VisualScripting; using UnityEngine; public enum Equipment { Battery, Car, Hoister, Hoister2 } public class ExplodeOnClick : MonoBehaviour { public Equipment equipment = Equipment.Battery; public GameObject parentObject; [Header("换电池设备")] public GameObject batteryobj; [Header("穿梭车")] public GameObject carobj; [Header("提升机1")] public GameObject hoister; [Header("提升机2")] public GameObject hoister2; [Header("需要生成的位置")] public Transform point; /// /// 展示对应设备 /// public List games = new List(); private Vector3[] originalPositions; // 存储原始位置 private bool isExploded = false; // 标识是否已爆炸 public List points; public List Replacebattery = new List(); private bool isp = false; private GameObject Opjgame; public List colidpoints; public bool Blowups = false; private void Awake() { parentObject = Instantiate(carobj, point); for (int i = 0; i < parentObject.transform.childCount; i++) { points.Add(parentObject.transform.GetChild(i).localPosition); Replacebattery.Add(parentObject.transform.GetChild(i).gameObject); } } /// /// 展示对应设备 /// public void Showshebi(string name) { if (games.Count > 0) { for (int i = 0; i < games.Count; i++) { if (games[i].name == name) { games[i].SetActive(true); } else { games[i].SetActive(false); } } } } /// /// 清理设备 /// /// /// public void Deletes(List game, List vectors) { if (game.Count > 0) { for (int i = 0; i < game.Count; i++) { if (game[i].gameObject) { game.Remove(game[i].gameObject); } } game.Clear(); } if (vectors.Count > 0) { vectors.Clear(); } } /// /// 换电池方法 /// public void Battery() { if (Equipment.Battery != equipment) { if (parentObject) { Destroy(parentObject); isExploded = false; isp = false; parentObject = Instantiate(batteryobj, point); Deletes(Replacebattery, points); for (int i = 0; i < parentObject.transform.childCount; i++) { points.Add(parentObject.transform.GetChild(i).localPosition); Replacebattery.Add(parentObject.transform.GetChild(i).gameObject); } equipment = Equipment.Battery; // Showshebi(parentObject.name+ "(Clone)"); } } else { Debug.Log("相同设备不需要切换"); } } /// /// 穿梭车 /// public void Shuttlebus() { if (Equipment.Car != equipment) { if (parentObject) { Destroy(parentObject); isExploded = false; isp = false; parentObject = Instantiate(carobj, point); Deletes(Replacebattery, points); for (int i = 0; i < parentObject.transform.childCount; i++) { points.Add(parentObject.transform.GetChild(i).localPosition); Replacebattery.Add(parentObject.transform.GetChild(i).gameObject); } equipment = Equipment.Car; //Showshebi(parentObject.name+ "(Clone)"); } } else { Debug.Log("相同设备不需要切换"); } } /// /// 提升机1 /// public void Hoister() { if (Equipment.Hoister != equipment) { if (parentObject) { Destroy(parentObject); isExploded = false; isp = false; parentObject = Instantiate(hoister, point); Deletes(Replacebattery, points); for (int i = 0; i < parentObject.transform.childCount; i++) { points.Add(parentObject.transform.GetChild(i).localPosition); Replacebattery.Add(parentObject.transform.GetChild(i).gameObject); } equipment = Equipment.Hoister; // Showshebi(parentObject.name + "(Clone)"); } } else { Debug.Log("相同设备不需要切换"); } } /// /// 提升机2 /// public void Hoister2() { if (Equipment.Hoister2 != equipment) { if (parentObject) { Destroy(parentObject); isExploded = false; isp = false; parentObject = Instantiate(hoister2, point); Deletes(Replacebattery, points); for (int i = 0; i < parentObject.transform.childCount; i++) { points.Add(parentObject.transform.GetChild(i).localPosition); Replacebattery.Add(parentObject.transform.GetChild(i).gameObject); } equipment = Equipment.Hoister2; // Showshebi(parentObject.name + "(Clone)"); } } else { Debug.Log("相同设备不需要切换"); } } void Update() { if (Input.GetMouseButtonDown(0) && isExploded) // 鼠标左键点击 { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { if (Namedata(hit.collider.gameObject.transform.parent.name)) { Debug.Log("有相同名字"); Debug.Log(hit.collider.gameObject.transform.parent.name); Showdata(hit.collider.gameObject.transform.parent.name); Opjgame = hit.collider.gameObject.transform.parent.gameObject; Blowups = true; Debug.Log("小图爆炸" + Blowups); Application.ExternalCall("modelStatus", Judgment()); } } } } public bool Judgment() { return Blowups; } /// /// 激活所有父物体 /// public void Show() { if (Replacebattery.Count > 0) { for (int i = 0; i < Replacebattery.Count; i++) { Replacebattery[i].gameObject.SetActive(true); } } } /// /// 子物体实现爆炸效果 /// public void Blowup() { if (!isp && Opjgame) { foreach (Transform child in Opjgame.transform) { colidpoints.Add(child.localPosition); //float moveDistance = CalculateMoveDistance(2); float moveDistance = Opjgame.GetComponent().value; Vector3 targetPosition = GetTargetPosition(child.transform, moveDistance); StartCoroutine(MoveToPosition(child, targetPosition)); } isp = true; } } /// /// 还原子物体 /// public void Restore() { if (isp && Opjgame) { for (int i = 0; i < Opjgame.transform.childCount; i++) { Transform child = Opjgame.transform.GetChild(i); StartCoroutine(MoveToPosition(child, colidpoints[i])); } isp = false; StartCoroutine(Bulkreduction()); Blowups = false; Debug.Log("小车还原" + Blowups); } } public void Explode() { if (!isExploded) { foreach (Transform child in parentObject.transform) { float moveDistance = CalculateMoveDistance(0); // 假设为第一层 Vector3 targetPosition = GetTargetPosition(child, moveDistance); // 这里可以实现动画效果,比如使用协程来移动物体 StartCoroutine(MoveToPosition(child, targetPosition)); } isExploded = true; // 标记为已爆炸 } } public void ResetPositions() // 复原位置的方法 { if (isExploded) { for (int i = 0; i < parentObject.transform.childCount; i++) { Transform child = parentObject.transform.GetChild(i); StartCoroutine(MoveToPosition(child, points[i])); } isExploded = false; // 标记为未爆炸 } } private IEnumerator MoveToPosition(Transform child, Vector3 targetPosition) { float elapsedTime = 0f; Vector3 startingPosition = child.localPosition; while (elapsedTime < 1f) // 1秒内完成移动 { child.localPosition = Vector3.Lerp(startingPosition, targetPosition, elapsedTime); elapsedTime += Time.deltaTime; yield return null; // 等待下一帧 } child.localPosition = targetPosition; // 确保最终位置 } private Vector3 GetTargetPosition(Transform child, float distance) { string direction = child.GetComponent()?.Direction; switch (direction) { case "前": return child.localPosition + new Vector3(0, 0, distance); case "后": return child.localPosition + new Vector3(0, 0, -distance); case "左": return child.localPosition + new Vector3(-distance, 0, 0); case "右": return child.localPosition + new Vector3(distance, 0, 0); case "上": return child.localPosition + new Vector3(0, distance, 0); case "下": return child.localPosition + new Vector3(0, -distance, 0); case "前上": return child.localPosition + new Vector3(0, distance, distance); case "前下": return child.localPosition + new Vector3(0, -distance, distance); case "后上": return child.localPosition + new Vector3(0, distance, -distance); case "后下": return child.localPosition + new Vector3(0, -distance, -distance); case "左上": return child.localPosition + new Vector3(-distance, distance, 0); case "左下": return child.localPosition + new Vector3(-distance, -distance, 0); case "右上": return child.localPosition + new Vector3(distance, distance, 0); case "右下": return child.localPosition + new Vector3(distance, -distance, 0); default: return child.localPosition; // 默认不移动 } } IEnumerator Bulkreduction() { yield return new WaitForSeconds(1.1f); if (Opjgame) { Opjgame = null; } if (colidpoints.Count > 0) { colidpoints.Clear(); } if (Replacebattery.Count > 0) { for (int i = 0; i < Replacebattery.Count; i++) { Replacebattery[i].gameObject.SetActive(true); } } } private float CalculateMoveDistance(int level) { if (level == 0) return 5; // 自定义第一层的移动距离 else if (level == 1) return 0.1f; // 第二层的移动距离 else if (level == 2) return 0.1f; // 第三层的移动距离 return 0f; // 更深层次不移动 } /// /// 判断链表是否有这个名字 /// /// /// private bool Namedata(string name) { if (Replacebattery.Count > 0) { for (int i = 0; i < Replacebattery.Count; i++) { if (Replacebattery[i].name == name) { return true; } } } return false; } /// /// 展示对应的子物体 /// /// public void Showdata(string name) { if (Replacebattery.Count > 0) { for (int i = 0; i < Replacebattery.Count; i++) { if (Replacebattery[i].name == name) { Replacebattery[i].SetActive(true); } else { Replacebattery[i].SetActive(false); } } } } }