using System; using System.Collections.Generic; using System.Linq; using TMPro; using UnityEngine; using UnityEngine.UI; using YogiGameCore.ComponentEx; using YogiGameCore.Utils; public class GamePanel : MonoBehaviour { [SerializeField] private Main main; [SerializeField] private UberButton selectComponentConfigBtnPrefab; [SerializeField] private Transform selectComponentConfigParent; [SerializeField] private UberButton selectComponentPrefab; [SerializeField] private Transform selectComponentParent; [SerializeField] private SearchView searchView; [SerializeField] private TMPTypewriter typewriterPrefab; [SerializeField] private Transform componentTextParent; private Dictionary spawnedTypewriters; [SerializeField] private Button uiTriggerBtn; [SerializeField] private GameObject uiCanvas; private void ClearAllComponentDisplayText() { Debug.Log("清空车辆所有文本信息"); componentTextParent.DestroyChildren(); spawnedTypewriters = new Dictionary(); } private void UpdateComponentDisplayText() { var carConfig = main.CurrentCar; for (var i = 0; i < carConfig.车辆组件.Count; i++) { CarComponentConfig carComponentConfig = carConfig.车辆组件[i]; int selectComponentIndex = carComponentConfig.SelectComponentIndex; ComponentConfig selectedComponent = carComponentConfig.车辆组件合集[selectComponentIndex]; if (!spawnedTypewriters.TryGetValue(carComponentConfig.部件类型名, out TMPTypewriter typewriter)) { typewriter = GameObject.Instantiate(typewriterPrefab, componentTextParent); spawnedTypewriters.Add(carComponentConfig.部件类型名, typewriter); } if (!typewriter.GetDisplayText().Equals(selectedComponent.组件描述)) { typewriter.StopAllCoroutines(); typewriter.PlayAnimation(selectedComponent.组件描述); } } } public void ResetCurrentComponent() { var btns = selectComponentParent.GetComponentsInChildren(); if (btns.Length <= 0) return; btns[0].SetHold(true); } private void Awake() { ClearAllComponentDisplayText(); this.main.OnCarChanged += OnMainOnOnCarChanged; this.main.OnCarComponentConfigChanged += OnCarComponentConfigChanged; this.main.OnSwitchComponent += UpdateComponentDisplayText; uiTriggerBtn.onClick.AddListener(() => { uiCanvas.SetActive(!uiCanvas.activeSelf); }); } private void Start() { var cars = main.AllCars.Cast().ToList(); searchView.SetUp(cars, (obj) => { return ((CarConfig)obj).车名; }, (result) => { var carConfig = result.As(); var index = cars.IndexOf(carConfig); main.DisplayCar(index); }); } private void OnCarComponentConfigChanged(CarComponentConfig config) { selectComponentParent.DestroyChildren(); for (var index = 0; index < config.车辆组件合集.Count; index++) { var component = config.车辆组件合集[index]; var componentName = component.部件名; var btn = SpawnSelectComponentBtn(componentName, index); Debug.Log($"SelectComponentIndex:{config.SelectComponentIndex}"); if (config.SelectComponentIndex == index) { btn.SetHold(true); } } } private UberButton SpawnSelectComponentBtn(string componentName, int index) { var btn = GameObject.Instantiate(selectComponentPrefab, selectComponentParent); btn.GetComponentInChildren().text = componentName; btn.OnToggleValueChanged += v => { if (!v) return; this.main.SwitchComponent(index); }; return btn; } void OnMainOnOnCarChanged(CarConfig config) { selectComponentConfigParent.DestroyChildren(); for (var index = 0; index < config.车辆组件.Count; index++) { var carComponentConfig = config.车辆组件[index]; var index1 = index; var btn = SpawnSelectComponentConfigBtn(carComponentConfig.部件类型名, (v) => { if (!v) return; main.SwitchComponentConfig(index1); }); if (main.GetCurrentCarComponentConfigIndex == index1) btn.SetHold(true); } ClearAllComponentDisplayText(); UpdateComponentDisplayText(); } private UberButton SpawnSelectComponentConfigBtn(string displayText, Action clickAction) { var btn = GameObject.Instantiate(selectComponentConfigBtnPrefab, selectComponentConfigParent); var textMeshProUGUI = btn.GetComponentInChildren(); textMeshProUGUI.text = displayText; btn.OnToggleValueChanged += clickAction; return btn; } }