| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using DCL.Shaders; |
| | 6 | | using System.Threading; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCL |
| | 10 | | { |
| | 11 | | public class AssetBundlesLoader |
| | 12 | | { |
| | 13 | | private const float MAX_LOAD_BUDGET_TIME = 0.05f; |
| | 14 | | private const int SKIPPED_FRAMES_AFTER_BUDGET_TIME_IS_REACHED_FOR_NEARBY_ASSETS = 1; |
| | 15 | | private const int SKIPPED_FRAMES_AFTER_BUDGET_TIME_IS_REACHED_FOR_DISTANT_ASSETS = 5; |
| | 16 | | private const float MAX_SQR_DISTANCE_FOR_QUICK_LOADING = 6000f; |
| | 17 | | private const float TIME_BETWEEN_REPRIORITIZATIONS = 1f; |
| | 18 | |
|
| | 19 | | private struct AssetBundleInfo |
| | 20 | | { |
| | 21 | | public Asset_AB asset; |
| | 22 | | public Transform containerTransform; |
| | 23 | | public Action onSuccess; |
| | 24 | | public Action<Exception> onFail; |
| | 25 | |
|
| | 26 | | public AssetBundleInfo(Asset_AB asset, Transform containerTransform, Action onSuccess, Action<Exception> onF |
| | 27 | | { |
| 22 | 28 | | this.asset = asset; |
| 22 | 29 | | this.containerTransform = containerTransform; |
| 22 | 30 | | this.onSuccess = onSuccess; |
| 22 | 31 | | this.onFail = onFail; |
| 22 | 32 | | } |
| | 33 | | } |
| | 34 | |
|
| 1 | 35 | | private Queue<AssetBundleInfo> highPriorityLoadQueue = new Queue<AssetBundleInfo>(); |
| 1 | 36 | | private Queue<AssetBundleInfo> lowPriorityLoadQueue = new Queue<AssetBundleInfo>(); |
| | 37 | |
|
| 1 | 38 | | private Dictionary<string, int> loadOrderByExtension = new Dictionary<string, int>() |
| | 39 | | { |
| | 40 | | { "png", 0 }, |
| | 41 | | { "jpg", 1 }, |
| | 42 | | { "peg", 2 }, |
| | 43 | | { "bmp", 3 }, |
| | 44 | | { "psd", 4 }, |
| | 45 | | { "iff", 5 }, |
| | 46 | | { "mat", 6 }, |
| | 47 | | { "nim", 7 }, |
| | 48 | | { "ltf", 8 }, |
| | 49 | | { "glb", 9 } |
| | 50 | | }; |
| | 51 | |
|
| 1 | 52 | | private List<UnityEngine.Object> loadedAssetsByName = new List<UnityEngine.Object>(); |
| | 53 | | private float currentLoadBudgetTime = 0; |
| | 54 | | private AssetBundleInfo assetBundleInfoToLoad; |
| | 55 | | private float lastQueuesReprioritizationTime = 0; |
| | 56 | |
|
| | 57 | | private CancellationTokenSource cancellationTokenSource; |
| | 58 | |
|
| 38 | 59 | | private bool limitTimeBudget => CommonScriptableObjects.rendererState.Get(); |
| | 60 | |
|
| | 61 | | public void Start() |
| | 62 | | { |
| 43 | 63 | | if (cancellationTokenSource != null) |
| 37 | 64 | | return; |
| | 65 | |
|
| 6 | 66 | | cancellationTokenSource = new CancellationTokenSource(); |
| 6 | 67 | | LoadAssetBundlesAsync(cancellationTokenSource.Token).SuppressCancellationThrow().Forget(); |
| 6 | 68 | | } |
| | 69 | |
|
| | 70 | | public void Stop() |
| | 71 | | { |
| 35 | 72 | | if (cancellationTokenSource == null) |
| 29 | 73 | | return; |
| | 74 | |
|
| 6 | 75 | | cancellationTokenSource.Cancel(); |
| 6 | 76 | | cancellationTokenSource.Dispose(); |
| 6 | 77 | | cancellationTokenSource = null; |
| | 78 | |
|
| 6 | 79 | | highPriorityLoadQueue.Clear(); |
| 6 | 80 | | lowPriorityLoadQueue.Clear(); |
| 6 | 81 | | } |
| | 82 | |
|
| | 83 | | public void MarkAssetBundleForLoad(Asset_AB asset, Transform containerTransform, Action onSuccess, Action<Except |
| | 84 | | { |
| 22 | 85 | | CheckForReprioritizeAwaitingAssets(); |
| | 86 | |
|
| 22 | 87 | | AssetBundleInfo assetBundleToLoad = new AssetBundleInfo(asset, containerTransform, onSuccess, onFail); |
| | 88 | |
|
| 22 | 89 | | float distanceFromPlayer = GetDistanceFromPlayer(containerTransform); |
| | 90 | |
|
| 22 | 91 | | if (distanceFromPlayer <= MAX_SQR_DISTANCE_FOR_QUICK_LOADING) |
| 22 | 92 | | highPriorityLoadQueue.Enqueue(assetBundleToLoad); |
| | 93 | | else |
| 0 | 94 | | lowPriorityLoadQueue.Enqueue(assetBundleToLoad); |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | private async UniTask LoadAssetBundlesAsync(CancellationToken cancellationToken) |
| | 98 | | { |
| 2348 | 99 | | while (!cancellationToken.IsCancellationRequested) |
| | 100 | | { |
| 2364 | 101 | | while (highPriorityLoadQueue.Count > 0) |
| | 102 | | { |
| 22 | 103 | | float time = Time.realtimeSinceStartup; |
| | 104 | |
|
| 22 | 105 | | assetBundleInfoToLoad = highPriorityLoadQueue.Dequeue(); |
| 66 | 106 | | await LoadAssetBundleAsync(assetBundleInfoToLoad, cancellationToken); |
| | 107 | |
|
| 22 | 108 | | if (IsLoadBudgetTimeReached(time)) |
| | 109 | | { |
| 0 | 110 | | await WaitForSkippedFrames(SKIPPED_FRAMES_AFTER_BUDGET_TIME_IS_REACHED_FOR_NEARBY_ASSETS); |
| | 111 | | } |
| | 112 | | } |
| | 113 | |
|
| 2342 | 114 | | while (lowPriorityLoadQueue.Count > 0 && highPriorityLoadQueue.Count == 0) |
| | 115 | | { |
| 0 | 116 | | float time = Time.realtimeSinceStartup; |
| | 117 | |
|
| 0 | 118 | | assetBundleInfoToLoad = lowPriorityLoadQueue.Dequeue(); |
| 0 | 119 | | await LoadAssetBundleAsync(assetBundleInfoToLoad, cancellationToken); |
| | 120 | |
|
| 0 | 121 | | if (IsLoadBudgetTimeReached(time)) |
| | 122 | | { |
| 0 | 123 | | await WaitForSkippedFrames(SKIPPED_FRAMES_AFTER_BUDGET_TIME_IS_REACHED_FOR_DISTANT_ASSETS); |
| | 124 | | } |
| | 125 | | } |
| | 126 | |
|
| 7026 | 127 | | await UniTask.Yield(); |
| | 128 | | } |
| 6 | 129 | | } |
| | 130 | |
|
| | 131 | | private async UniTask LoadAssetBundleAsync(AssetBundleInfo assetBundleInfo, CancellationToken ct) |
| | 132 | | { |
| 22 | 133 | | if (!assetBundleInfo.asset.IsValid()) |
| | 134 | | { |
| 0 | 135 | | assetBundleInfo.onFail?.Invoke(new Exception("Asset bundle is null")); |
| 0 | 136 | | return; |
| | 137 | | } |
| | 138 | |
|
| 22 | 139 | | AssetBundleRequest abRequest = assetBundleInfo.asset.LoadAllAssetsAsync(); |
| 66 | 140 | | await abRequest.WithCancellation(ct); |
| | 141 | |
|
| 22 | 142 | | loadedAssetsByName = abRequest.allAssets.ToList(); |
| | 143 | |
|
| 188 | 144 | | foreach (var loadedAsset in loadedAssetsByName) |
| | 145 | | { |
| 72 | 146 | | string ext = "any"; |
| | 147 | |
|
| 79 | 148 | | if (loadedAsset is Texture) { ext = "png"; } |
| 65 | 149 | | else if (loadedAsset is Material material) |
| | 150 | | { |
| 13 | 151 | | ShaderUtils.UpgradeMaterial_2020_To_2021(material); |
| 13 | 152 | | ext = "mat"; |
| | 153 | | } |
| 60 | 154 | | else if (loadedAsset is Animation || loadedAsset is AnimationClip) { ext = "nim"; } |
| 57 | 155 | | else if (loadedAsset is GameObject) { ext = "glb"; } |
| | 156 | |
|
| 72 | 157 | | assetBundleInfo.asset.AddAssetByExtension(ext, loadedAsset); |
| | 158 | | } |
| | 159 | |
|
| 22 | 160 | | loadedAssetsByName.Clear(); |
| 22 | 161 | | assetBundleInfo.onSuccess?.Invoke(); |
| 22 | 162 | | } |
| | 163 | |
|
| | 164 | | private bool IsLoadBudgetTimeReached(float startTime) |
| | 165 | | { |
| 22 | 166 | | if (limitTimeBudget) |
| | 167 | | { |
| 0 | 168 | | currentLoadBudgetTime += Time.realtimeSinceStartup - startTime; |
| | 169 | |
|
| 0 | 170 | | if (currentLoadBudgetTime > MAX_LOAD_BUDGET_TIME) |
| | 171 | | { |
| 0 | 172 | | currentLoadBudgetTime = 0f; |
| 0 | 173 | | return true; |
| | 174 | | } |
| | 175 | | } |
| | 176 | |
|
| 22 | 177 | | return false; |
| | 178 | | } |
| | 179 | |
|
| | 180 | | private UniTask WaitForSkippedFrames(int skippedFramesBetweenLoadings) => |
| 0 | 181 | | UniTask.DelayFrame(skippedFramesBetweenLoadings); |
| | 182 | |
|
| | 183 | | private void CheckForReprioritizeAwaitingAssets() |
| | 184 | | { |
| 22 | 185 | | if (lowPriorityLoadQueue.Count == 0 || |
| | 186 | | (Time.realtimeSinceStartup - lastQueuesReprioritizationTime) < TIME_BETWEEN_REPRIORITIZATIONS) |
| 22 | 187 | | return; |
| | 188 | |
|
| 0 | 189 | | while (lowPriorityLoadQueue.Count > 0 && GetDistanceFromPlayer(lowPriorityLoadQueue.Peek().containerTransfor |
| | 190 | | { |
| 0 | 191 | | highPriorityLoadQueue.Enqueue(lowPriorityLoadQueue.Dequeue()); |
| 0 | 192 | | lastQueuesReprioritizationTime = Time.realtimeSinceStartup; |
| | 193 | | } |
| 0 | 194 | | } |
| | 195 | |
|
| | 196 | | private float GetDistanceFromPlayer(Transform containerTransform) |
| | 197 | | { |
| 22 | 198 | | return (containerTransform != null && limitTimeBudget) ? Vector3.SqrMagnitude(containerTransform.position - |
| | 199 | | } |
| | 200 | | } |
| | 201 | | } |