using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using YogiGameCore.Utils.MonoExtent; public class Main : MonoBehaviour { [SerializeField] private GameConfig config; public List AllCars => config.Cars; private int currentCarIndex; public CarConfig CurrentCar => config.Cars[currentCarIndex]; /// /// 当前部件集合索引 /// private int currentCarComponentConfigIndex = -1; public int GetCurrentCarComponentConfigIndex => currentCarComponentConfigIndex; /// /// 获得当前聚焦的部件合集 /// private CarComponentConfig currentCarComponents { get { if (CurrentCar.车辆组件.Count <= currentCarComponentConfigIndex) return null; return CurrentCar.车辆组件[currentCarComponentConfigIndex]; } } public UnityEvent OnCarNameChanged, OnCarConfigChanged, OnCarComponentHighlight; public event Action OnCarChanged; public event Action OnCarComponentConfigChanged; public event Action OnSwitchComponent; private void HideAllCars() { StopAllCoroutines(); foreach (var configCar in config.Cars) { // SetCarActiveCoroutine() SetCarActive(configCar.车辆基础物体,false); // configCar.车辆基础物体.SetActive(false); foreach (var carComponentConfig in configCar.车辆组件) { foreach (var component in carComponentConfig.车辆组件合集) { SetCarActive(component.物体,false); // component.物体.SetActive(false); } } } } [Button] public void ResetCurrentCar() { for (var index = 0; index < this.CurrentCar.车辆组件.Count; index++) { var carComponentConfig = this.CurrentCar.车辆组件[index]; carComponentConfig.SelectComponentIndex = 0; for (var j = 0; j < carComponentConfig.车辆组件合集.Count; j++) { var go = carComponentConfig.车辆组件合集[j].物体; SetCarActive(go, j == 0); // carComponentConfig.车辆组件合集[j].物体.SetActive(j == 0); } } SwitchComponent(0); } [Button] public void PrevCar() { SetCarDisplay(currentCarIndex, false); currentCarIndex--; if (currentCarIndex < 0) currentCarIndex = config.Cars.Count - 1; SetCarDisplay(currentCarIndex, true); } [Button] public void NextCar() { SetCarDisplay(currentCarIndex, false); currentCarIndex++; if (currentCarIndex >= config.Cars.Count) currentCarIndex = 0; SetCarDisplay(currentCarIndex, true); } [Button] public void DisplayCar(int carIndex) { SetCarDisplay(currentCarIndex, false); currentCarIndex = carIndex; SetCarDisplay(currentCarIndex, true); } private void SetCarDisplay(int carIndex, bool isActive) { if (carIndex == -1) return; var configCar = config.Cars[carIndex]; // configCar.车辆基础物体.SetActive(isActive); SetCarActive(configCar.车辆基础物体,isActive); foreach (var carComponentConfig in configCar.车辆组件) { if (isActive) { if (carComponentConfig.车辆组件合集.Count <= carComponentConfig.SelectComponentIndex) continue; var go = carComponentConfig.车辆组件合集[carComponentConfig.SelectComponentIndex].物体; SetCarActive(go,true); // carComponentConfig.车辆组件合集[carComponentConfig.SelectComponentIndex].物体.SetActive(true); } else { foreach (var component in carComponentConfig.车辆组件合集) { // component.物体.SetActive(false); SetCarActive(component.物体,false); } } } if (isActive) { UpdateCarName(); OnCarChanged?.Invoke(this.CurrentCar); SwitchComponentConfig(0); } } /// /// 更新车名 /// private void UpdateCarName() { this.OnCarNameChanged.Invoke(this.CurrentCar.车名); } /// /// 更新部件名字 /// private void UpdateCarConfigsName() { this.OnCarConfigChanged.Invoke(this.currentCarComponents.部件类型名); } /// /// 切换不同的部件 /// /// [Button] public void SwitchComponentConfig(int carComponentConfigIndex) { if (this.CurrentCar.车辆组件.Count <= carComponentConfigIndex) return; this.currentCarComponentConfigIndex = carComponentConfigIndex; UpdateCarConfigsName(); OnCarComponentConfigChanged?.Invoke(this.currentCarComponents); } [Button] public void SwitchComponent(int componentIndex) { if (currentCarComponents.车辆组件合集.Count <= componentIndex) return; int originIndex = currentCarComponents.SelectComponentIndex; SetCurrentCarConfigDisplay(originIndex, false); int newIndex = currentCarComponents.SelectComponentIndex = componentIndex; SetCurrentCarConfigDisplay(newIndex, true); // 更新选择的部件名 ComponentConfig componentConfig = currentCarComponents.车辆组件合集[componentIndex]; OnCarComponentHighlight.Invoke(componentConfig.部件名); OnSwitchComponent?.Invoke(componentConfig, componentIndex); } private void SetCurrentCarConfigDisplay(int componentIndex, bool isDisplay) { if (componentIndex == -1) return; var go = currentCarComponents.车辆组件合集[componentIndex].物体; // go.SetActive(isDisplay); SetCarActive(go, isDisplay); } private void SetCarActive(GameObject go, bool isActive) { go.SetActive(isActive); // Debug.Log($"SetCarActive ({go.name}) ({isActive})"); // this.StartCoroutine(SetCarActiveCoroutine(go, isActive, .5f)); } private IEnumerator SetCarActiveCoroutine(GameObject go, bool isActive, float time) { var renderers = go.GetComponentsInChildren(); var propertyToID = Shader.PropertyToID("_ClipScaleV"); float timer = 0; float min = -1f; float max = 1.0f; while (timer < time) { timer += Time.deltaTime; yield return null; var evl = timer / time; float result = isActive ? Mathf.Lerp(max, min, evl) : Mathf.Lerp(min, max, evl); foreach (var r in renderers) r.material.SetFloat(propertyToID, result); } } private void Awake() { HideAllCars(); } private void Start() { currentCarIndex = -1; NextCar(); } }