using System; 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() { foreach (var configCar in config.Cars) { configCar.车辆基础物体.SetActive(false); foreach (var carComponentConfig in configCar.车辆组件) { foreach (var component in carComponentConfig.车辆组件合集) { 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++) { 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); foreach (var carComponentConfig in configCar.车辆组件) { if (isActive) { if (carComponentConfig.车辆组件合集.Count <= carComponentConfig.SelectComponentIndex) continue; carComponentConfig.车辆组件合集[carComponentConfig.SelectComponentIndex].物体.SetActive(true); } else { foreach (var component in carComponentConfig.车辆组件合集) { component.物体.SetActive(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; currentCarComponents.车辆组件合集[componentIndex].物体.SetActive(isDisplay); } private void Awake() { HideAllCars(); } private void Start() { currentCarIndex = -1; NextCar(); } }