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