using System.Collections; using System.Collections.Generic; using UnityEngine; public class DiaoZhuangAnimCtrl : MonoBehaviour { float speed = 0; [SerializeField] Animation thisAnimation; public GameObject diaoju1; public GameObject gaiban; Transform gaibanDefultParent; public GameObject fadongji; public Transform diaoGou; Transform fadongjiDefultParent; // Start is called before the first frame update void Start() { thisAnimation = GetComponent(); gaibanDefultParent = gaiban.transform.parent; fadongjiDefultParent = fadongji.transform.parent; //SetSpeed(2); //Play(); } public void SetSpeed(float speed) { if (this.speed == speed) return;//只执行一次避免重复执行 this.speed = speed; if (speed > 0) { Debug.Log("正常播放"); thisAnimation["Take 001"].time = 0; } else { Debug.Log("倒序播放"); thisAnimation["Take 001"].time = thisAnimation["Take 001"].clip.length; } } public void Play() { thisAnimation["Take 001"].speed = speed; thisAnimation.CrossFade("Take 001"); } public void DiaoGaiBan() { if (speed > 0) { Debug.Log("从发动机吊起盖板"); UpDiaoHuan(); gaiban.transform.parent = diaoGou; } else { Debug.Log("盖板安装吊装完成"); gaiban.transform.SetParent(gaibanDefultParent); DownDiaoHuan(); gaiban.GetComponent().DiaoZhuangFinished(); } } private void DownDiaoHuan() { gaiban.transform.Find("dh1").transform.localEulerAngles = new Vector3(0, 0, 0); gaiban.transform.Find("dh2").transform.localEulerAngles = new Vector3(0, 0, 0); gaiban.transform.Find("dh3").transform.localEulerAngles = new Vector3(0, 0, 0); } private void UpDiaoHuan() { gaiban.transform.Find("dh1").transform.localEulerAngles = new Vector3(0, -45f, 0); gaiban.transform.Find("dh2").transform.localEulerAngles = new Vector3(0, 45f, 0); gaiban.transform.Find("dh3").transform.localEulerAngles = new Vector3(0, 45f, 0); } public void SetGaiBanOnFloor() { if (speed > 0) { Debug.Log("盖板拆卸吊装完成"); diaoju1.gameObject.SetActive(false); gaiban.transform.SetParent(null); DownDiaoHuan(); gaiban.GetComponent().DiaoZhuangFinished(); Invoke("StopAnim", 1f); } else { Debug.Log("从地面吊起盖板"); diaoju1.gameObject.SetActive(true); gaiban.transform.parent = diaoGou; UpDiaoHuan(); } } public void DiaoFaDongJi() { if (speed > 0) { Debug.Log("拆卸环节吊起发动机"); fadongji.transform.SetParent(diaoGou.transform); } else { Debug.Log("安装环节吊装发动机完成"); fadongji.transform.SetParent(fadongjiDefultParent); fadongji.GetComponent().DiaoZhuangFinished(); Invoke("StopAnim", 2f); } } public void SetFaDongJiOnFloor() { if (speed > 0) { fadongji.GetComponent().DiaoZhuangFinished(); } else//安装时不做任何处理 { } } private void StopAnim() { thisAnimation["Take 001"].speed = 0; thisAnimation.CrossFade("Take 001"); } #if UNITY_EDITOR private void Update() { //if (speed > 0 && !thisAnimation.isPlaying) //{ // speed = 0; // Debug.Log("拆卸动画播放完成"); //} //if (Input.GetKeyDown(KeyCode.Q)) //{ // //Debug.Log("开始播放动画"); // ////SetSpeed(-2); // //Play(); // SetSpeed(1); //} //if (Input.GetKeyDown(KeyCode.Space)) //{ // Debug.Log("开始播放动画"); // //SetSpeed(-2); // Play(); //} //if (Input.GetKeyDown(KeyCode.W)) //{ // //Debug.Log("开始播放动画"); // ////SetSpeed(-2); // //Play(); // SetSpeed(-1); //} ////if (speed < 0 && !thisAnimation.isPlaying) ////{ //// speed = 0; //// Debug.Log("安装动画播放完成"); ////} } #endif }