using UnityEngine; /// /// 二维指令动作 /// public class CarPartsDriver : MonoBehaviour { public static CarPartsDriver instance; private float driveTime = 0; /// /// 当前播放声音 /// private FunctionSync_Audio currentAudio; /// /// 当前声音时长 /// private float currentTime; private MechanismsDriver currentMsg; private bool isPlay = false; public int orderCode; public int _state; private void Awake() { instance = this; } // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if (isPlay) { if (currentMsg.name == "支腿") { PlayAudio(currentAudio, currentTime); Debug.Log("zhengche"); } else { currentAudio.SetAudio(AudioControlEnum.Stop); Debug.Log("qita"); } } if (Input.GetKeyDown(KeyCode.P)) { DriveMechanism(orderCode, _state); } } /// /// 接收动作 /// /// /// public void DriveMechanism(int orderCode, int _state) { foreach (var item in GetComponentsInChildren()) { if (item.reciveOrderType == (ReciveOrderType)orderCode) { item.driverState.myint = _state; item.driverState.SendSync(); if (item.sync_Audio != null) { switch (_state) { case 1: currentAudio = item.sync_Audio; currentMsg = item; currentTime = item.openTime; item.sync_Audio.SetAudio(AudioControlEnum.Play); isPlay = true; break; case -1: currentAudio = item.sync_Audio; currentMsg = item; currentTime = item.closeTime; item.sync_Audio.SetAudio(AudioControlEnum.Play); isPlay = true; break; default: break; } } } } } private void PlayAudio(FunctionSync_Audio sync_Audio, float time) { if (driveTime < time) { driveTime += Time.deltaTime; } if (driveTime >= time) { driveTime = 0; sync_Audio.SetAudio(AudioControlEnum.Stop); isPlay = false; } } private void PlayAudio(MechanismsDriver mechanismsDriver, FunctionSync_Audio sync_Audio, float time) { if (mechanismsDriver.limitCrank != null) { if (mechanismsDriver.limitCrank.Angle == mechanismsDriver.limitCrank.range.min || mechanismsDriver.limitCrank.Angle == mechanismsDriver.limitCrank.range.max) { sync_Audio.SetAudio(AudioControlEnum.Stop); isPlay = false; } } else { if (driveTime < time) { driveTime += Time.deltaTime; } if (driveTime >= time) { driveTime = 0; sync_Audio.SetAudio(AudioControlEnum.Stop); isPlay = false; } } } public void DriveMechanism(ReciveOrderType reciveOrderType, int _state) { foreach (var item in GetComponentsInChildren()) { if (item.reciveOrderType == reciveOrderType) { item.InitMechanisms(_state); } } } /// /// 停止 /// public void StopMechanism() { foreach (var item in GetComponentsInChildren()) { item.driverState.myint = 0; item.driverState.SendSync(); if (item.sync_Audio != null) { isPlay = false; item.sync_Audio.SetAudio(AudioControlEnum.Stop); } } } }