| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using DCL.Shaders; |
| | 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 | |
|
| | 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 | |
|
| 38 | 58 | | private bool limitTimeBudget => CommonScriptableObjects.rendererState.Get(); |
| | 59 | |
|
| | 60 | | public void Start() |
| | 61 | | { |
| 43 | 62 | | if (assetBundlesLoadingCoroutine != null) |
| 37 | 63 | | return; |
| | 64 | |
|
| 6 | 65 | | assetBundlesLoadingCoroutine = CoroutineStarter.Start(LoadAssetBundlesCoroutine()); |
| 6 | 66 | | } |
| | 67 | |
|
| | 68 | | public void Stop() |
| | 69 | | { |
| 35 | 70 | | if (assetBundlesLoadingCoroutine == null) |
| 29 | 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, Transform containerTransform, Action onSuccess, Action<Except |
| | 81 | | { |
| 22 | 82 | | CheckForReprioritizeAwaitingAssets(); |
| | 83 | |
|
| 22 | 84 | | AssetBundleInfo assetBundleToLoad = new AssetBundleInfo(asset, containerTransform, onSuccess, onFail); |
| | 85 | |
|
| 22 | 86 | | float distanceFromPlayer = GetDistanceFromPlayer(containerTransform); |
| | 87 | |
|
| 22 | 88 | | if (distanceFromPlayer <= MAX_SQR_DISTANCE_FOR_QUICK_LOADING) |
| 22 | 89 | | highPriorityLoadQueue.Enqueue(assetBundleToLoad); |
| | 90 | | else |
| 0 | 91 | | lowPriorityLoadQueue.Enqueue(assetBundleToLoad); |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | private IEnumerator LoadAssetBundlesCoroutine() |
| | 95 | | { |
| 2438 | 96 | | while (true) |
| | 97 | | { |
| 2466 | 98 | | while (highPriorityLoadQueue.Count > 0) |
| | 99 | | { |
| 22 | 100 | | float time = Time.realtimeSinceStartup; |
| | 101 | |
|
| 22 | 102 | | assetBundleInfoToLoad = highPriorityLoadQueue.Dequeue(); |
| 22 | 103 | | yield return LoadAssetBundle(assetBundleInfoToLoad); |
| | 104 | |
|
| 22 | 105 | | if (IsLoadBudgetTimeReached(time)) |
| | 106 | | { |
| 0 | 107 | | yield return WaitForSkippedFrames(SKIPPED_FRAMES_AFTER_BUDGET_TIME_IS_REACHED_FOR_NEARBY_ASSETS) |
| 0 | 108 | | time = Time.realtimeSinceStartup; |
| | 109 | | } |
| | 110 | | } |
| | 111 | |
|
| 2444 | 112 | | while (lowPriorityLoadQueue.Count > 0 && highPriorityLoadQueue.Count == 0) |
| | 113 | | { |
| 0 | 114 | | float time = Time.realtimeSinceStartup; |
| | 115 | |
|
| 0 | 116 | | assetBundleInfoToLoad = lowPriorityLoadQueue.Dequeue(); |
| 0 | 117 | | yield return LoadAssetBundle(assetBundleInfoToLoad); |
| | 118 | |
|
| 0 | 119 | | if (IsLoadBudgetTimeReached(time)) |
| | 120 | | { |
| 0 | 121 | | yield return WaitForSkippedFrames(SKIPPED_FRAMES_AFTER_BUDGET_TIME_IS_REACHED_FOR_DISTANT_ASSETS |
| 0 | 122 | | time = Time.realtimeSinceStartup; |
| | 123 | | } |
| | 124 | | } |
| | 125 | |
|
| 2444 | 126 | | yield return null; |
| | 127 | | } |
| | 128 | | } |
| | 129 | |
|
| | 130 | | private IEnumerator LoadAssetBundle(AssetBundleInfo assetBundleInfo) |
| | 131 | | { |
| 22 | 132 | | if (!assetBundleInfo.asset.IsValid()) |
| | 133 | | { |
| 0 | 134 | | assetBundleInfo.onFail?.Invoke(new Exception("Asset bundle is null")); |
| 0 | 135 | | yield break; |
| | 136 | | } |
| | 137 | |
|
| 22 | 138 | | AssetBundleRequest abRequest = assetBundleInfo.asset.LoadAllAssetsAsync(); |
| | 139 | |
|
| 3192 | 140 | | while (!abRequest.isDone) { yield return null; } |
| | 141 | |
|
| 22 | 142 | | loadedAssetsByName = abRequest.allAssets.ToList(); |
| | 143 | |
|
| 178 | 144 | | foreach (var loadedAsset in loadedAssetsByName) |
| | 145 | | { |
| 67 | 146 | | string ext = "any"; |
| | 147 | |
|
| 74 | 148 | | if (loadedAsset is Texture) { ext = "png"; } |
| 60 | 149 | | else if (loadedAsset is Material material) |
| | 150 | | { |
| 13 | 151 | | ShaderUtils.UpgradeMaterial_2020_To_2021(material); |
| 13 | 152 | | ext = "mat"; |
| | 153 | | } |
| 55 | 154 | | else if (loadedAsset is Animation || loadedAsset is AnimationClip) { ext = "nim"; } |
| 52 | 155 | | else if (loadedAsset is GameObject) { ext = "glb"; } |
| | 156 | |
|
| 67 | 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 IEnumerator WaitForSkippedFrames(int skippedFramesBetweenLoadings) |
| | 181 | | { |
| 0 | 182 | | for (int i = 0; i < skippedFramesBetweenLoadings; i++) { yield return null; } |
| 0 | 183 | | } |
| | 184 | |
|
| | 185 | | private void CheckForReprioritizeAwaitingAssets() |
| | 186 | | { |
| 22 | 187 | | if (lowPriorityLoadQueue.Count == 0 || |
| | 188 | | (Time.realtimeSinceStartup - lastQueuesReprioritizationTime) < TIME_BETWEEN_REPRIORITIZATIONS) |
| 22 | 189 | | return; |
| | 190 | |
|
| 0 | 191 | | while (lowPriorityLoadQueue.Count > 0 && GetDistanceFromPlayer(lowPriorityLoadQueue.Peek().containerTransfor |
| | 192 | | { |
| 0 | 193 | | highPriorityLoadQueue.Enqueue(lowPriorityLoadQueue.Dequeue()); |
| 0 | 194 | | lastQueuesReprioritizationTime = Time.realtimeSinceStartup; |
| | 195 | | } |
| 0 | 196 | | } |
| | 197 | |
|
| | 198 | | private float GetDistanceFromPlayer(Transform containerTransform) |
| | 199 | | { |
| 22 | 200 | | return (containerTransform != null && limitTimeBudget) ? Vector3.SqrMagnitude(containerTransform.position - |
| | 201 | | } |
| | 202 | | } |
| | 203 | | } |