diff --git a/Assets/PerfectWorld/Scripts/World/AddressableObject.cs b/Assets/PerfectWorld/Scripts/World/AddressableObject.cs index ae97a06678..f0ad2fabd1 100644 --- a/Assets/PerfectWorld/Scripts/World/AddressableObject.cs +++ b/Assets/PerfectWorld/Scripts/World/AddressableObject.cs @@ -1,6 +1,8 @@ using BrewMonster.Scripts; using Cysharp.Threading.Tasks; using UnityEngine; +using System; + #if UNITY_EDITOR using UnityEditor; @@ -35,7 +37,7 @@ namespace BrewMonster } - public async UniTask LoadAsset() + public async UniTask LoadAsset(Action actDone = null) { if (string.IsNullOrEmpty(assetPath)) { @@ -50,7 +52,7 @@ namespace BrewMonster _isLoading = true; int requestId = ++_loadRequestId; var model = await AddressableManager.Instance.LoadPrefabAsync(assetPath); - + actDone?.Invoke(); // Object might have been destroyed while awaiting. if (this == null || gameObject == null) { diff --git a/Assets/PerfectWorld/Scripts/World/LitModelHolder.cs b/Assets/PerfectWorld/Scripts/World/LitModelHolder.cs index 8651386e9a..70038d877f 100644 --- a/Assets/PerfectWorld/Scripts/World/LitModelHolder.cs +++ b/Assets/PerfectWorld/Scripts/World/LitModelHolder.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -288,10 +289,12 @@ public class LitModelHolder : MonoSingleton private void ProcessLoadAndUnloadObjects() { + _currentIdxAsset = 0; + _maxIdxAsset = _candidatesForLoading.Count; // load the objects that are in the loading range long enough. (_candidateWaitTimeSeconds) foreach (var kvp in _candidatesForLoading) { - kvp.Key.LoadAsset().Forget(); + kvp.Key.LoadAsset(CallBackAssetLoadingDone).Forget(); _loadedObjects.Add(kvp.Key); } _candidatesForLoading.Clear(); @@ -307,6 +310,28 @@ public class LitModelHolder : MonoSingleton _objectsToUnload.Clear(); } + int _currentIdxAsset = 0; // The current counter for loaded assets. + int _maxIdxAsset = 0; // Limit the number of assets that have finished loading. + + /// + /// isLitToReady is a condition used by _hostPlayer to wait until the Lit assets have finished loading. + /// This function counts the number of assets successfully loaded from the Addressable system, + /// and once the required number is reached, it sets _hostPlayer.isLitToReady = true. + /// + private void CallBackAssetLoadingDone() + { + _currentIdxAsset++; + if (_currentIdxAsset >= _maxIdxAsset) + { + if (_hostPlayer != null) + { + _hostPlayer.isLitToReady = true; + } + _currentIdxAsset = -1; + _maxIdxAsset = 0; + } + } + /// /// Get the Host Player instance. /// @@ -444,4 +469,4 @@ public class LitModelHolder : MonoSingleton $"- anchorsWithMoreThanOneChild={multiChildCount}"); } #endif -} \ No newline at end of file +} diff --git a/Assets/PerfectWorld/Scripts/World/TerrainHolder.cs b/Assets/PerfectWorld/Scripts/World/TerrainHolder.cs index d7b6142b0c..8cfe0ed009 100644 --- a/Assets/PerfectWorld/Scripts/World/TerrainHolder.cs +++ b/Assets/PerfectWorld/Scripts/World/TerrainHolder.cs @@ -174,6 +174,11 @@ namespace BrewMonster } _needToProcessLoadAndUnload = true; + //if (_hostPlayer != null) + //{ + // Debug.LogError("ProcessLoadAndUnload Terain _hostPlayer.isTerrainToReady = false"); + // _hostPlayer.isTerrainToReady = false; + //} } } @@ -183,9 +188,11 @@ namespace BrewMonster /// private void ProcessLoadAndUnload() { + _currentIdxAsset = 0; + _maxIdxAsset = _candidatesForLoading.Count; for (int i = 0; i < _candidatesForLoading.Count; i++) { - _candidatesForLoading[i].LoadAsset().Forget(); + _candidatesForLoading[i].LoadAsset(CallBackAssetLoadingDone).Forget(); } _candidatesForLoading.Clear(); @@ -196,6 +203,28 @@ namespace BrewMonster _objectsToUnload.Clear(); } + int _currentIdxAsset = 0; // The current counter for loaded assets. + int _maxIdxAsset = 0; // Limit the number of assets that have finished loading. + + /// + /// isLitToReady is a condition used by _hostPlayer to wait until the Terrain assets have finished loading. + /// This function counts the number of assets successfully loaded from the Addressable system, + /// and once the required number is reached, it sets _hostPlayer.isLitToReady = true. + /// + private void CallBackAssetLoadingDone() + { + _currentIdxAsset++; + if (_currentIdxAsset >= _maxIdxAsset) + { + if (_hostPlayer != null) + { + _hostPlayer.isTerrainToReady = true; + } + _currentIdxAsset = -1; + _maxIdxAsset = 0; + } + } + /// /// Main-thread anchor for terrain streaming: host position, or navigate clone when force-navigate is active (same idea as LitModelHolder). /// 地形流式锚点:普通用宿主;强制导航用导航克隆(与 LitModelHolder 一致)。 diff --git a/Assets/Scripts/CECHostPlayer.World.cs b/Assets/Scripts/CECHostPlayer.World.cs index c7839afe91..ac98b65b39 100644 --- a/Assets/Scripts/CECHostPlayer.World.cs +++ b/Assets/Scripts/CECHostPlayer.World.cs @@ -230,6 +230,8 @@ namespace BrewMonster m_MoveCtrl.SetHostLastPos(EC_Utility.ToA3DVECTOR3(vPos)); m_MoveCtrl.SetLastSevPos(EC_Utility.ToA3DVECTOR3(vPos)); + isLitToReady = false; + isTerrainToReady = false; // Update camera if available // 如果可用则更新相机 // UpdateFollowCamera(false, 10); // Uncomment if UpdateFollowCamera method exists diff --git a/Assets/Scripts/CECHostPlayer.cs b/Assets/Scripts/CECHostPlayer.cs index af988a5e55..9f29163251 100644 --- a/Assets/Scripts/CECHostPlayer.cs +++ b/Assets/Scripts/CECHostPlayer.cs @@ -169,6 +169,8 @@ namespace BrewMonster Ray ray; RaycastHit[] hits = new RaycastHit[5]; bool isDataAwaitToReady = false; + public bool isTerrainToReady = false; + public bool isLitToReady = false; private BaseVfxObject m_pSelectedGFX; private BaseVfxObject m_pHoverGFX; @@ -395,7 +397,7 @@ namespace BrewMonster { base.Update(); - if (!isDataAwaitToReady) + if (!isDataAwaitToReady || !isTerrainToReady || !isLitToReady) { return; }