using System.Linq; using System.Threading.Tasks; using UnityEngine; public class CarControl : MonoBehaviour { private float speed = 7; public bool canRotate = false; // Start is called before the first frame update void Start() { if (Bootstrap.Instance != null) { Bootstrap.Instance.eventCenter.AddEventListener(Enum_EventType.ChangeCarSwitch, ChangeCarSwitch); Bootstrap.Instance.eventCenter.AddEventListener(Enum_EventType.CanRotate, CanRotate); } ResetCar(); } // Update is called once per frame void Update() { if (canRotate) { transform.Rotate(transform.forward, speed * Time.deltaTime, Space.World); } } public GameObject[] allCars; public GameObject testcar; public async void ChangeCarSwitch() { ResetCar(); switch (GetawayMqttClient.licensePlateData.VehicleBrand) { case "BYDE6": case "±ÈÑǵÏE6": testcar = allCars[1]; break; case "BYDËÎ": case "±ÈÑǵÏ": testcar = allCars[0]; break; } testcar.SetActive(true); if (testcar == null) { Debug.Log($"testcar is null, return"); return; } await Task.Delay(2000); float a = 4f; while (a <= 9) { a += 0.1f; for (int i = 0; i < testcar.GetComponentsInChildren().Length; i++) { for (int j = 0; j < testcar.GetComponentsInChildren()[i].materials.Length; j++) { testcar.GetComponentsInChildren()[i].materials[j].SetFloat("_Distance", a); } } await Task.Delay(50); } Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.ChangeChildrenMat, false); Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.StepSwitching); } public void CanRotate() { canRotate = !canRotate; } public async void StepSwitching() { await Task.Delay(50); } void ResetCar() { if (allCars.Count() <= 0) return; for (int i = 0; i < allCars.Length; i++) { allCars[i].SetActive(false); } } private void OnDisable() { if (Bootstrap.Instance != null) { Bootstrap.Instance.eventCenter.RemoveEventListener(Enum_EventType.ChangeCarSwitch, ChangeCarSwitch); Bootstrap.Instance.eventCenter.RemoveEventListener(Enum_EventType.CanRotate, CanRotate); } ResetCar(); } }