| | 1 | | using UnityEngine; |
| | 2 | | using UnityEngine.Assertions; |
| | 3 | |
|
| | 4 | | namespace DCL.Components |
| | 5 | | { |
| | 6 | | public class RendereableAssetLoadHelper |
| | 7 | | { |
| | 8 | | public enum LoadingType |
| | 9 | | { |
| | 10 | | ASSET_BUNDLE_WITH_GLTF_FALLBACK, |
| | 11 | | ASSET_BUNDLE_ONLY, |
| | 12 | | GLTF_ONLY, |
| | 13 | | DEFAULT |
| | 14 | | } |
| | 15 | |
|
| | 16 | | public static bool VERBOSE = false; |
| | 17 | |
|
| | 18 | | public static bool useCustomContentServerUrl = false; |
| | 19 | | public static string customContentServerUrl; |
| | 20 | |
|
| | 21 | | public static LoadingType defaultLoadingType = LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK; |
| | 22 | |
|
| 93 | 23 | | public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering(); |
| | 24 | |
|
| 0 | 25 | | public Rendereable loadedAsset { get; protected set; } |
| | 26 | |
|
| | 27 | | public bool isFinished |
| | 28 | | { |
| | 29 | | get |
| | 30 | | { |
| 21 | 31 | | if (gltfPromise != null) |
| 21 | 32 | | return gltfPromise.state == AssetPromiseState.FINISHED; |
| | 33 | |
|
| 0 | 34 | | if (abPromise != null) |
| 0 | 35 | | return abPromise.state == AssetPromiseState.FINISHED; |
| | 36 | |
|
| 0 | 37 | | return true; |
| | 38 | | } |
| | 39 | | } |
| | 40 | |
|
| | 41 | | string bundlesContentUrl; |
| | 42 | | ContentProvider contentProvider; |
| | 43 | |
|
| | 44 | | AssetPromise_GLTF gltfPromise; |
| | 45 | | AssetPromise_AB_GameObject abPromise; |
| | 46 | |
|
| | 47 | | #if UNITY_EDITOR |
| | 48 | | public override string ToString() |
| | 49 | | { |
| 0 | 50 | | float loadTime = Mathf.Min(loadFinishTime, Time.realtimeSinceStartup) - loadStartTime; |
| | 51 | |
|
| 0 | 52 | | string result = "not loading"; |
| | 53 | |
|
| 0 | 54 | | if (gltfPromise != null) |
| | 55 | | { |
| 0 | 56 | | result = $"GLTF -> promise state = {gltfPromise.state} ({loadTime} load time)... waiting promises = {Ass |
| | 57 | |
|
| 0 | 58 | | if (gltfPromise.state == AssetPromiseState.WAITING) |
| | 59 | | { |
| 0 | 60 | | result += $"\nmaster promise state... is blocked... {AssetPromiseKeeper_GLTF.i.GetMasterState(gltfPr |
| | 61 | | } |
| | 62 | | } |
| | 63 | |
|
| 0 | 64 | | if (abPromise != null) |
| | 65 | | { |
| 0 | 66 | | result = $"ASSET BUNDLE -> promise state = {abPromise.ToString()} ({loadTime} load time)... waiting prom |
| | 67 | | } |
| | 68 | |
|
| 0 | 69 | | return result; |
| | 70 | | } |
| | 71 | |
|
| | 72 | | float loadStartTime = 0; |
| 93 | 73 | | float loadFinishTime = float.MaxValue; |
| | 74 | | #endif |
| | 75 | |
|
| 93 | 76 | | public RendereableAssetLoadHelper(ContentProvider contentProvider, string bundlesContentUrl) |
| | 77 | | { |
| 93 | 78 | | this.contentProvider = contentProvider; |
| 93 | 79 | | this.bundlesContentUrl = bundlesContentUrl; |
| 93 | 80 | | } |
| | 81 | |
|
| | 82 | | public event System.Action<Rendereable> OnSuccessEvent; |
| | 83 | | public event System.Action OnFailEvent; |
| | 84 | |
|
| | 85 | | public void Load(string targetUrl, LoadingType forcedLoadingType = LoadingType.DEFAULT) |
| | 86 | | { |
| 93 | 87 | | Assert.IsFalse(string.IsNullOrEmpty(targetUrl), "url is null!!"); |
| | 88 | | #if UNITY_EDITOR |
| 93 | 89 | | loadStartTime = Time.realtimeSinceStartup; |
| | 90 | | #endif |
| | 91 | |
|
| 93 | 92 | | LoadingType finalLoadingType = forcedLoadingType == LoadingType.DEFAULT ? defaultLoadingType : forcedLoading |
| | 93 | | switch (finalLoadingType) |
| | 94 | | { |
| | 95 | | case LoadingType.ASSET_BUNDLE_ONLY: |
| 0 | 96 | | LoadAssetBundle(targetUrl, OnSuccessEvent, OnFailEvent); |
| 0 | 97 | | break; |
| | 98 | | case LoadingType.GLTF_ONLY: |
| 14 | 99 | | LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent); |
| 14 | 100 | | break; |
| | 101 | | case LoadingType.DEFAULT: |
| | 102 | | case LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK: |
| 158 | 103 | | LoadAssetBundle(targetUrl, OnSuccessEvent, () => LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent)); |
| | 104 | | break; |
| | 105 | | } |
| 79 | 106 | | } |
| | 107 | |
|
| | 108 | | public void Unload() |
| | 109 | | { |
| 86 | 110 | | UnloadAB(); |
| 86 | 111 | | UnloadGLTF(); |
| 86 | 112 | | } |
| | 113 | |
|
| | 114 | | void UnloadAB() |
| | 115 | | { |
| 86 | 116 | | if ( abPromise != null ) |
| | 117 | | { |
| 0 | 118 | | AssetPromiseKeeper_AB_GameObject.i.Forget(abPromise); |
| | 119 | | } |
| 86 | 120 | | } |
| | 121 | |
|
| | 122 | | void UnloadGLTF() |
| | 123 | | { |
| 86 | 124 | | if ( gltfPromise != null ) |
| | 125 | | { |
| 86 | 126 | | AssetPromiseKeeper_GLTF.i.Forget(gltfPromise); |
| | 127 | | } |
| 86 | 128 | | } |
| | 129 | |
|
| | 130 | | void LoadAssetBundle(string targetUrl, System.Action<Rendereable> OnSuccess, System.Action OnFail) |
| | 131 | | { |
| 79 | 132 | | if (abPromise != null) |
| | 133 | | { |
| 0 | 134 | | UnloadAB(); |
| 0 | 135 | | if (VERBOSE) |
| 0 | 136 | | Debug.Log("Forgetting not null promise..." + targetUrl); |
| | 137 | | } |
| | 138 | |
|
| 79 | 139 | | string bundlesBaseUrl = useCustomContentServerUrl ? customContentServerUrl : bundlesContentUrl; |
| | 140 | |
|
| 79 | 141 | | if (string.IsNullOrEmpty(bundlesBaseUrl)) |
| | 142 | | { |
| 79 | 143 | | OnFailWrapper(null, OnFail); |
| 79 | 144 | | return; |
| | 145 | | } |
| | 146 | |
|
| 0 | 147 | | if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash)) |
| | 148 | | { |
| 0 | 149 | | OnFailWrapper(null, OnFail); |
| 0 | 150 | | return; |
| | 151 | | } |
| | 152 | |
|
| 0 | 153 | | abPromise = new AssetPromise_AB_GameObject(bundlesBaseUrl, hash); |
| 0 | 154 | | abPromise.settings = this.settings; |
| | 155 | |
|
| 0 | 156 | | abPromise.OnSuccessEvent += (x) => |
| | 157 | | { |
| 0 | 158 | | var r = new Rendereable() |
| | 159 | | { |
| | 160 | | container = x.container, |
| | 161 | | totalTriangleCount = x.totalTriangleCount, |
| | 162 | | meshes = x.meshes, |
| | 163 | | renderers = x.renderers, |
| | 164 | | meshToTriangleCount = x.meshToTriangleCount |
| | 165 | | }; |
| | 166 | |
|
| 0 | 167 | | OnSuccessWrapper(r, OnSuccess); |
| 0 | 168 | | }; |
| | 169 | |
|
| 0 | 170 | | abPromise.OnFailEvent += (x) => OnFailWrapper(x, OnFail); |
| | 171 | |
|
| 0 | 172 | | AssetPromiseKeeper_AB_GameObject.i.Keep(abPromise); |
| 0 | 173 | | } |
| | 174 | |
|
| | 175 | | void LoadGltf(string targetUrl, System.Action<Rendereable> OnSuccess, System.Action OnFail) |
| | 176 | | { |
| 93 | 177 | | if (gltfPromise != null) |
| | 178 | | { |
| 0 | 179 | | UnloadGLTF(); |
| | 180 | |
|
| 0 | 181 | | if (VERBOSE) |
| 0 | 182 | | Debug.Log("Forgetting not null promise... " + targetUrl); |
| | 183 | | } |
| | 184 | |
|
| 93 | 185 | | if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash)) |
| | 186 | | { |
| 0 | 187 | | OnFailWrapper(null, OnFail); |
| 0 | 188 | | return; |
| | 189 | | } |
| | 190 | |
|
| 93 | 191 | | gltfPromise = new AssetPromise_GLTF(contentProvider, targetUrl, hash); |
| 93 | 192 | | gltfPromise.settings = this.settings; |
| | 193 | |
|
| 93 | 194 | | gltfPromise.OnSuccessEvent += (x) => |
| | 195 | | { |
| 82 | 196 | | var r = new Rendereable() |
| | 197 | | { |
| | 198 | | container = x.container, |
| | 199 | | totalTriangleCount = x.totalTriangleCount, |
| | 200 | | meshes = x.meshes, |
| | 201 | | renderers = x.renderers, |
| | 202 | | meshToTriangleCount = x.meshToTriangleCount |
| | 203 | | }; |
| | 204 | |
|
| 82 | 205 | | OnSuccessWrapper(r, OnSuccess); |
| 82 | 206 | | }; |
| 97 | 207 | | gltfPromise.OnFailEvent += (x) => OnFailWrapper(x, OnFail); |
| | 208 | |
|
| 93 | 209 | | AssetPromiseKeeper_GLTF.i.Keep(gltfPromise); |
| 93 | 210 | | } |
| | 211 | |
|
| | 212 | | private void OnFailWrapper(Asset_WithPoolableContainer loadedAsset, System.Action OnFail) |
| | 213 | | { |
| | 214 | | #if UNITY_EDITOR |
| 83 | 215 | | loadFinishTime = Time.realtimeSinceStartup; |
| | 216 | | #endif |
| | 217 | |
|
| | 218 | |
|
| 83 | 219 | | OnFail?.Invoke(); |
| 83 | 220 | | ClearEvents(); |
| 83 | 221 | | } |
| | 222 | |
|
| | 223 | | private void OnSuccessWrapper(Rendereable loadedAsset, System.Action<Rendereable> OnSuccess) |
| | 224 | | { |
| | 225 | | #if UNITY_EDITOR |
| 82 | 226 | | loadFinishTime = Time.realtimeSinceStartup; |
| | 227 | | #endif |
| 82 | 228 | | if (VERBOSE) |
| | 229 | | { |
| 0 | 230 | | if (gltfPromise != null) |
| 0 | 231 | | Debug.Log($"GLTF Load(): target URL -> {gltfPromise.GetId()}. Success!"); |
| | 232 | | else |
| 0 | 233 | | Debug.Log($"AB Load(): target URL -> {abPromise.hash}. Success!"); |
| | 234 | | } |
| | 235 | |
|
| 82 | 236 | | this.loadedAsset = loadedAsset; |
| 82 | 237 | | OnSuccess?.Invoke(loadedAsset); |
| 82 | 238 | | ClearEvents(); |
| 82 | 239 | | } |
| | 240 | |
|
| | 241 | | public void ClearEvents() |
| | 242 | | { |
| 0 | 243 | | OnSuccessEvent = null; |
| 0 | 244 | | OnFailEvent = null; |
| 0 | 245 | | } |
| | 246 | | } |
| | 247 | | } |