diff --git a/Assets/Scripts/SelecScreenCharacter.cs b/Assets/Scripts/SelecScreenCharacter.cs index a60a9368e1..63c187e318 100644 --- a/Assets/Scripts/SelecScreenCharacter.cs +++ b/Assets/Scripts/SelecScreenCharacter.cs @@ -5,6 +5,7 @@ using BrewMonster.Scripts; using UnityEngine; using UnityEngine.UI; using BrewMonster; +using System.Collections; namespace BrewMonster.UI { @@ -25,6 +26,9 @@ namespace BrewMonster.UI private Action _onCreateCharacterComplete; private List _roleInfos; + private Coroutine _showModelReadyCoroutine; + private int _pendingShowModelRoleId = -1; + private void OnEnable() { _btnEnterGame.onClick.AddListener(OnClickedEnterGame); @@ -149,8 +153,37 @@ namespace BrewMonster.UI _selectingCharacterItemUI.SetFocus(true); _btnEnterGame.gameObject.SetActive(true); - if (playerModelPreview != null && characterItemUI.RoleInfo != null) - playerModelPreview.ShowPlayerModel(characterItemUI.RoleInfo.roleid); + if (playerModelPreview == null || characterItemUI.RoleInfo == null) + { + return; + } + + _pendingShowModelRoleId = characterItemUI.RoleInfo.roleid; + if (_showModelReadyCoroutine != null) + { + StopCoroutine(_showModelReadyCoroutine); + } + _showModelReadyCoroutine = StartCoroutine(ShowSelectedModelWhenReady(_pendingShowModelRoleId)); + } + + private IEnumerator ShowSelectedModelWhenReady(int roleId) + { + while (isActiveAndEnabled && playerModelPreview != null) + { + if (_pendingShowModelRoleId != roleId) + { + yield break; // Role changed, stop this coroutine + } + + if (playerModelPreview.playerModelIds != null && playerModelPreview.playerModelIds.Contains(roleId)) + { + playerModelPreview.ShowPlayerModel(roleId); + _showModelReadyCoroutine = null; + yield break; // Model is ready, show it and stop this coroutine + } + yield return null; + } + _showModelReadyCoroutine = null; } private void OnCreateCharacterClicked()