using System; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using Script; using UnityEngine; public class AccidentManager : MonoBehaviour { public GameObject uis; private string triggerName; public GameObject[] baotai; public CarGearControl carGearControl; public Animation zuiwei; public Animator diandongcheren; public Mesh mesh; public MeshFilter meshFilter; public Animator animator; // 将Animator组件拖拽到这个字段 private bool isPlaying = false; public bool isOpen = true; public void Close() { for (int i = 0; i < uis.transform.Find("UI").childCount; i++) { uis.transform.Find("UI").GetChild(i).gameObject.SetActive(false); } for (int i = 0; i < baotai.Length; i++) { baotai[i].SetActive(false); } zuiwei.gameObject.SetActive(false); diandongcheren.gameObject.SetActive(false); meshFilter.mesh = mesh; } public async Task Tr(Transform other, bool b = false) { if (isOpen) return; triggerName = other.name; if (other.transform.tag == "People") { Close(); uis.SetActive(true); uis.transform.Find("UI").Find("撞倒行人").gameObject.SetActive(true); } else if (other.name == "Box_Collider") { Close(); uis.SetActive(true); uis.transform.Find("UI").Find("汽车追尾").gameObject.SetActive(true); } else if (other.name == "爆胎") { for (int i = 0; i < baotai.Length; i++) { baotai[i].SetActive(true); } Close(); PlayAnimationSequence("MsgWinAnimOpen", "MsgWinAnimClose"); uis.transform.Find("UI").Find("爆胎").gameObject.SetActive(true); } else if (other.name == "礼让行人") { if (b) { Close(); PlayAnimationSequence("MsgWinAnimOpen", "MsgWinAnimClose"); uis.transform.Find("UI").Find("礼让行人").gameObject.SetActive(true); carGearControl.mingdi = false; } else { carGearControl.mingdi = true; } } else if (other.name == "隧道汽车追尾") { StartCoroutine(PlayAnimation()); } else if (other.transform.tag == "diandongche") { StartCoroutine(diandongche()); } else if (other.name == "刹车失灵") { Close(); PlayAnimationSequence("MsgWinAnimOpen", "MsgWinAnimClose"); uis.transform.Find("UI").Find("刹车失灵").gameObject.SetActive(true); } } IEnumerator dengdai() { zuiwei.gameObject.SetActive(true); zuiwei.Play("donghua"); // 等待动画播放完毕 yield return new WaitForSeconds(zuiwei["donghua"].length); Close(); PlayAnimationSequence("MsgWinAnimOpen", "MsgWinAnimClose"); uis.transform.Find("UI").Find("汽车追尾").gameObject.SetActive(true); // 在这里编写动画播放完之后的代码 // 例如: Debug.Log("动画播放完成"); } IEnumerator PlayAnimation() { zuiwei.gameObject.SetActive(true); zuiwei.Play("donghua"); // 等待动画播放完毕 yield return new WaitForSeconds(zuiwei["donghua"].length); Close(); PlayAnimationSequence("MsgWinAnimOpen", "MsgWinAnimClose"); uis.transform.Find("UI").Find("汽车追尾").gameObject.SetActive(true); // 在这里编写动画播放完之后的代码 // 例如: Debug.Log("动画播放完成"); } IEnumerator diandongche() { diandongcheren.gameObject.SetActive(true); diandongcheren.Play("diandongchedonghua"); // 等待动画播放完毕 yield return new WaitForSeconds(diandongcheren.GetCurrentAnimatorStateInfo(0).length); Close(); PlayAnimationSequence("MsgWinAnimOpen", "MsgWinAnimClose"); uis.transform.Find("UI").Find("电动车穿行").gameObject.SetActive(true); // 在这里编写动画播放完之后的代码 // 例如: Debug.Log("动画播放完成"); } private void OnTriggerEnter(Collider other) { // //MBOXManager.Instance.Bonk(); Tr(other.transform); // switch (other.name) // { // case "刹车失灵": // uis.transform.Find("刹车失灵").gameObject.SetActive(true); // break; // case "礼让行人": // uis.transform.Find("礼让行人").gameObject.SetActive(true); // break; // case "汽车追尾": // uis.transform.Find("汽车追尾").gameObject.SetActive(true); // break; // case "电动车穿行": // uis.transform.Find("电动车穿行").gameObject.SetActive(true); // break; // case "撞倒行人": // uis.transform.Find("撞倒行人").gameObject.SetActive(true); // break; // case "爆胎": // uis.transform.Find("爆胎").gameObject.SetActive(true); // break; // } } private void OnTriggerExit(Collider other) { // for (int i = 0; i < uis.transform.childCount; i++) // { // uis.transform.GetChild(i).gameObject.SetActive(false); // } // // for (int i = 0; i < baotai.Length; i++) // { // baotai[i].SetActive(false); // } // // // // uis.SetActive(false); } // 用于外部调用的方法,启动动画序列 public void PlayAnimationSequence(string firstAnimation, string secondAnimation) { Close(); if (!isPlaying) { Debug.Log("12312312312312312321"); StartCoroutine(PlayAnimations(firstAnimation, secondAnimation)); } } // 协程,按顺序播放动画,并在两个动画之间等待 private IEnumerator PlayAnimations(string firstAnimation, string secondAnimation) { isPlaying = true; // 播放第一个动画 animator.Play(firstAnimation); // 等待第一个动画播放完毕 yield return new WaitUntil(() => !animator.GetCurrentAnimatorStateInfo(0).IsName(firstAnimation)); // 等待2秒 yield return new WaitForSeconds(5); // 播放第二个动画 animator.Play(secondAnimation); // 等待第二个动画播放完毕 yield return new WaitUntil(() => !animator.GetCurrentAnimatorStateInfo(0).IsName(secondAnimation)); yield return new WaitForSeconds(20); isPlaying = false; } }