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