| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class AssetBundlesLoader |
| | 10 | | { |
| | 11 | | private const float MAX_LOAD_BUDGET_TIME = 0.05f; |
| | 12 | | private const int SKIPPED_FRAMES_AFTER_BUDGET_TIME_IS_REACHED_FOR_NEARBY_ASSETS = 1; |
| | 13 | | private const int SKIPPED_FRAMES_AFTER_BUDGET_TIME_IS_REACHED_FOR_DISTANT_ASSETS = 5; |
| | 14 | | private const float MAX_SQR_DISTANCE_FOR_QUICK_LOADING = 6000f; |
| | 15 | | private const float TIME_BETWEEN_REPRIORITIZATIONS = 1f; |
| | 16 | |
|
| | 17 | | private struct AssetBundleInfo |
| | 18 | | { |
| | 19 | | public Asset_AB asset; |
| | 20 | | public AssetBundle assetBundle; |
| | 21 | | public Transform containerTransform; |
| | 22 | | public Action onSuccess; |
| | 23 | | public Action<Exception> onFail; |
| | 24 | |
|
| | 25 | | public AssetBundleInfo(Asset_AB asset, AssetBundle assetBundle, Transform containerTransform, Action onSucce |
| | 26 | | { |
| 20 | 27 | | this.asset = asset; |
| 20 | 28 | | this.assetBundle = assetBundle; |
| 20 | 29 | | this.containerTransform = containerTransform; |
| 20 | 30 | | this.onSuccess = onSuccess; |
| 20 | 31 | | this.onFail = onFail; |
| 20 | 32 | | } |
| | 33 | | } |
| | 34 | |
|
| | 35 | | private Coroutine assetBundlesLoadingCoroutine; |
| 1 | 36 | | private Queue<AssetBundleInfo> highPriorityLoadQueue = new Queue<AssetBundleInfo>(); |
| 1 | 37 | | private Queue<AssetBundleInfo> lowPriorityLoadQueue = new Queue<AssetBundleInfo>(); |
| | 38 | |
|
| 1 | 39 | | private Dictionary<string, int> loadOrderByExtension = new Dictionary<string, int>() |
| | 40 | | { |
| | 41 | | { "png", 0 }, |
| | 42 | | { "jpg", 1 }, |
| | 43 | | { "peg", 2 }, |
| | 44 | | { "bmp", 3 }, |
| | 45 | | { "psd", 4 }, |
| | 46 | | { "iff", 5 }, |
| | 47 | | { "mat", 6 }, |
| | 48 | | { "nim", 7 }, |
| | 49 | | { "ltf", 8 }, |
| | 50 | | { "glb", 9 } |
| | 51 | | }; |
| | 52 | |
|
| 1 | 53 | | private List<UnityEngine.Object> loadedAssetsByName = new List<UnityEngine.Object>(); |
| | 54 | | private float currentLoadBudgetTime = 0; |
| | 55 | | private AssetBundleInfo assetBundleInfoToLoad; |
| | 56 | | private float lastQueuesReprioritizationTime = 0; |
| | 57 | |
|
| 35 | 58 | | private bool limitTimeBudget => CommonScriptableObjects.rendererState.Get(); |
| | 59 | |
|
| | 60 | | public void Start() |
| | 61 | | { |
| 39 | 62 | | if (assetBundlesLoadingCoroutine != null) |
| 33 | 63 | | return; |
| | 64 | |
|
| 6 | 65 | | assetBundlesLoadingCoroutine = CoroutineStarter.Start(LoadAssetBundlesCoroutine()); |
| 6 | 66 | | } |
| | 67 | |
|
| | 68 | | public void Stop() |
| | 69 | | { |
| 20 | 70 | | if (assetBundlesLoadingCoroutine == null) |
| 14 | 71 | | return; |
| | 72 | |
|
| 6 | 73 | | CoroutineStarter.Stop(assetBundlesLoadingCoroutine); |
| 6 | 74 | | assetBundlesLoadingCoroutine = null; |
| | 75 | |
|
| 6 | 76 | | highPriorityLoadQueue.Clear(); |
| 6 | 77 | | lowPriorityLoadQueue.Clear(); |
| 6 | 78 | | } |
| | 79 | |
|
| | 80 | | public void MarkAssetBundleForLoad(Asset_AB asset, AssetBundle assetBundle, Transform containerTransform, Action |
| | 81 | | { |
| 20 | 82 | | CheckForReprioritizeAwaitingAssets(); |
| | 83 | |
|
| 20 | 84 | | AssetBundleInfo assetBundleToLoad = new AssetBundleInfo(asset, assetBundle, containerTransform, onSuccess, o |
| | 85 | |
|
| 20 | 86 | | float distanceFromPlayer = GetDistanceFromPlayer(containerTransform); |
| 20 | 87 | | if (distanceFromPlayer <= MAX_SQR_DISTANCE_FOR_QUICK_LOADING) |
| 7 | 88 | | highPriorityLoadQueue.Enqueue(assetBundleToLoad); |
| | 89 | | else |
| 13 | 90 | | lowPriorityLoadQueue.Enqueue(assetBundleToLoad); |
| 13 | 91 | | } |
| | 92 | |
|
| | 93 | | private IEnumerator LoadAssetBundlesCoroutine() |
| | 94 | | { |
| 221 | 95 | | while (true) |
| | 96 | | { |
| 234 | 97 | | while (highPriorityLoadQueue.Count > 0) |
| | 98 | | { |
| 7 | 99 | | float time = Time.realtimeSinceStartup; |
| | 100 | |
|
| 7 | 101 | | assetBundleInfoToLoad = highPriorityLoadQueue.Dequeue(); |
| 7 | 102 | | yield return LoadAssetBundle(assetBundleInfoToLoad); |
| | 103 | |
|
| 7 | 104 | | if (IsLoadBudgetTimeReached(time)) |
| | 105 | | { |
| 1 | 106 | | yield return WaitForSkippedFrames(SKIPPED_FRAMES_AFTER_BUDGET_TIME_IS_REACHED_FOR_NEARBY_ASSETS) |
| 1 | 107 | | time = Time.realtimeSinceStartup; |
| | 108 | | } |
| | 109 | | } |
| | 110 | |
|
| 240 | 111 | | while (lowPriorityLoadQueue.Count > 0 && highPriorityLoadQueue.Count == 0) |
| | 112 | | { |
| 13 | 113 | | float time = Time.realtimeSinceStartup; |
| | 114 | |
|
| 13 | 115 | | assetBundleInfoToLoad = lowPriorityLoadQueue.Dequeue(); |
| 13 | 116 | | yield return LoadAssetBundle(assetBundleInfoToLoad); |
| | 117 | |
|
| 13 | 118 | | if (IsLoadBudgetTimeReached(time)) |
| | 119 | | { |
| 4 | 120 | | yield return WaitForSkippedFrames(SKIPPED_FRAMES_AFTER_BUDGET_TIME_IS_REACHED_FOR_DISTANT_ASSETS |
| 4 | 121 | | time = Time.realtimeSinceStartup; |
| | 122 | | } |
| | 123 | | } |
| | 124 | |
|
| 227 | 125 | | yield return null; |
| | 126 | | } |
| | 127 | | } |
| | 128 | |
|
| | 129 | | private IEnumerator LoadAssetBundle(AssetBundleInfo assetBundleInfo) |
| | 130 | | { |
| 20 | 131 | | if (assetBundleInfo.assetBundle == null) |
| | 132 | | { |
| 0 | 133 | | assetBundleInfo.onFail?.Invoke(new Exception("Asset bundle is null")); |
| 0 | 134 | | yield break; |
| | 135 | | } |
| | 136 | |
|
| 20 | 137 | | AssetBundleRequest abRequest = assetBundleInfo.assetBundle.LoadAllAssetsAsync(); |
| | 138 | |
|
| 88 | 139 | | while (!abRequest.isDone) |
| | 140 | | { |
| 68 | 141 | | yield return null; |
| | 142 | | } |
| | 143 | |
|
| 20 | 144 | | loadedAssetsByName = abRequest.allAssets.ToList(); |
| | 145 | |
|
| 158 | 146 | | foreach (var loadedAsset in loadedAssetsByName) |
| | 147 | | { |
| 59 | 148 | | string ext = "any"; |
| | 149 | |
|
| 59 | 150 | | if (loadedAsset is Texture textureAsset) |
| | 151 | | { |
| 7 | 152 | | ext = "png"; |
| 7 | 153 | | } |
| 52 | 154 | | else if (loadedAsset is Material) |
| | 155 | | { |
| 12 | 156 | | ext = "mat"; |
| 12 | 157 | | } |
| 40 | 158 | | else if (loadedAsset is Animation || loadedAsset is AnimationClip) |
| | 159 | | { |
| 4 | 160 | | ext = "nim"; |
| 4 | 161 | | } |
| 36 | 162 | | else if (loadedAsset is GameObject) |
| | 163 | | { |
| 12 | 164 | | ext = "glb"; |
| | 165 | | } |
| | 166 | |
|
| 59 | 167 | | if (assetBundleInfo.asset?.assetsByExtension == null) |
| | 168 | | { |
| 0 | 169 | | Debug.LogWarning($"Found an unexpected Null Reference: {assetBundleInfo.asset} or {assetBundleInfo.a |
| 0 | 170 | | } |
| | 171 | | else |
| | 172 | | { |
| 59 | 173 | | if (!assetBundleInfo.asset.assetsByExtension.ContainsKey(ext)) |
| | 174 | | { |
| 44 | 175 | | assetBundleInfo.asset.assetsByExtension.Add(ext, new List<UnityEngine.Object>()); |
| | 176 | | } |
| | 177 | |
|
| 59 | 178 | | assetBundleInfo.asset.assetsByExtension[ext].Add(loadedAsset); |
| | 179 | | } |
| | 180 | | } |
| | 181 | |
|
| 20 | 182 | | loadedAssetsByName.Clear(); |
| 20 | 183 | | assetBundleInfo.onSuccess?.Invoke(); |
| 20 | 184 | | } |
| | 185 | |
|
| | 186 | | private bool IsLoadBudgetTimeReached(float startTime) |
| | 187 | | { |
| 20 | 188 | | if (limitTimeBudget) |
| | 189 | | { |
| 20 | 190 | | currentLoadBudgetTime += Time.realtimeSinceStartup - startTime; |
| 20 | 191 | | if (currentLoadBudgetTime > MAX_LOAD_BUDGET_TIME) |
| | 192 | | { |
| 5 | 193 | | currentLoadBudgetTime = 0f; |
| 5 | 194 | | return true; |
| | 195 | | } |
| | 196 | | } |
| | 197 | |
|
| 15 | 198 | | return false; |
| | 199 | | } |
| | 200 | |
|
| | 201 | | private IEnumerator WaitForSkippedFrames(int skippedFramesBetweenLoadings) |
| | 202 | | { |
| 52 | 203 | | for (int i = 0; i < skippedFramesBetweenLoadings; i++) |
| | 204 | | { |
| 21 | 205 | | yield return null; |
| | 206 | | } |
| 5 | 207 | | } |
| | 208 | |
|
| | 209 | | private void CheckForReprioritizeAwaitingAssets() |
| | 210 | | { |
| 20 | 211 | | if (lowPriorityLoadQueue.Count == 0 || |
| | 212 | | (Time.realtimeSinceStartup - lastQueuesReprioritizationTime) < TIME_BETWEEN_REPRIORITIZATIONS) |
| 20 | 213 | | return; |
| | 214 | |
|
| 0 | 215 | | while (lowPriorityLoadQueue.Count > 0 && GetDistanceFromPlayer(lowPriorityLoadQueue.Peek().containerTransfor |
| | 216 | | { |
| 0 | 217 | | highPriorityLoadQueue.Enqueue(lowPriorityLoadQueue.Dequeue()); |
| 0 | 218 | | lastQueuesReprioritizationTime = Time.realtimeSinceStartup; |
| | 219 | | } |
| 0 | 220 | | } |
| | 221 | |
|
| 20 | 222 | | private float GetDistanceFromPlayer(Transform containerTransform) { return (containerTransform != null && limitT |
| | 223 | | } |
| | 224 | | } |