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