| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.Assertions; |
| | 4 | |
|
| | 5 | | namespace DCL.Components |
| | 6 | | { |
| | 7 | | public class RendereableAssetLoadHelper |
| | 8 | | { |
| | 9 | | public event Action<Rendereable> OnSuccessEvent; |
| | 10 | | public event Action<Exception> OnFailEvent; |
| | 11 | |
|
| | 12 | | public enum LoadingType |
| | 13 | | { |
| | 14 | | ASSET_BUNDLE_WITH_GLTF_FALLBACK, |
| | 15 | | ASSET_BUNDLE_WITH_OLD_GLTF_FALLBACK, |
| | 16 | | ASSET_BUNDLE_ONLY, |
| | 17 | | GLTF_ONLY, |
| | 18 | | OLD_GLTF, |
| | 19 | | DEFAULT |
| | 20 | | } |
| | 21 | |
|
| | 22 | | private const string AB_GO_NAME_PREFIX = "AB:"; |
| | 23 | | private const string GLTF_GO_NAME_PREFIX = "GLTF:"; |
| | 24 | | private const string GLTFAST_GO_NAME_PREFIX = "GLTFast:"; |
| | 25 | | private const string FROM_ASSET_BUNDLE_TAG = "FromAssetBundle"; |
| | 26 | | private const string FROM_RAW_GLTF_TAG = "FromRawGLTF"; |
| | 27 | |
|
| | 28 | | public static bool VERBOSE = false; |
| | 29 | | public static bool useCustomContentServerUrl = false; |
| | 30 | | public static string customContentServerUrl; |
| | 31 | | public static LoadingType defaultLoadingType = LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK; |
| | 32 | |
|
| 82 | 33 | | public AssetPromiseSettings_Rendering settings = new(); |
| 298 | 34 | | public Rendereable loadedAsset { get; protected set; } |
| | 35 | |
|
| | 36 | | private readonly string bundlesContentUrl; |
| | 37 | | private readonly Func<bool> IsGltFastEnabled; |
| | 38 | | private readonly ContentProvider contentProvider; |
| | 39 | | private AssetPromise_GLTF gltfPromise; |
| | 40 | | private AssetPromise_GLTFast_Instance gltfastPromise; |
| | 41 | | private AssetPromise_AB_GameObject abPromise; |
| | 42 | | private string currentLoadingSystem; |
| | 43 | |
|
| | 44 | | public bool isFinished |
| | 45 | | { |
| | 46 | | get |
| | 47 | | { |
| 71 | 48 | | if (gltfPromise != null) |
| 71 | 49 | | return gltfPromise.state == AssetPromiseState.FINISHED; |
| | 50 | |
|
| 0 | 51 | | if (abPromise != null) |
| 0 | 52 | | return abPromise.state == AssetPromiseState.FINISHED; |
| | 53 | |
|
| 0 | 54 | | return true; |
| | 55 | | } |
| | 56 | | } |
| | 57 | |
|
| | 58 | | #if UNITY_EDITOR |
| | 59 | | public override string ToString() |
| | 60 | | { |
| 0 | 61 | | float loadTime = Mathf.Min(loadFinishTime, Time.realtimeSinceStartup) - loadStartTime; |
| | 62 | |
|
| 0 | 63 | | string result = "not loading"; |
| | 64 | |
|
| 0 | 65 | | if (gltfPromise != null) |
| | 66 | | { |
| 0 | 67 | | result = $"GLTF -> promise state = {gltfPromise.state} ({loadTime} load time)... waiting promises = {Ass |
| | 68 | |
|
| 0 | 69 | | if (gltfPromise.state == AssetPromiseState.WAITING) |
| 0 | 70 | | { result += $"\nmaster promise state... is blocked... {AssetPromiseKeeper_GLTF.i.GetMasterState(gltfProm |
| | 71 | | } |
| | 72 | |
|
| 0 | 73 | | if (abPromise != null) |
| 0 | 74 | | { result = $"ASSET BUNDLE -> promise state = {abPromise.ToString()} ({loadTime} load time)... waiting promis |
| | 75 | |
|
| 0 | 76 | | return result; |
| | 77 | | } |
| | 78 | |
|
| | 79 | | float loadStartTime = 0; |
| 82 | 80 | | float loadFinishTime = float.MaxValue; |
| | 81 | | #endif |
| | 82 | |
|
| 82 | 83 | | public RendereableAssetLoadHelper(ContentProvider contentProvider, string bundlesContentUrl, Func<bool> isGltFas |
| | 84 | | { |
| 82 | 85 | | this.contentProvider = contentProvider; |
| 82 | 86 | | this.bundlesContentUrl = bundlesContentUrl; |
| 82 | 87 | | this.IsGltFastEnabled = isGltFastEnabled; |
| 82 | 88 | | } |
| | 89 | |
|
| | 90 | | public void Load(string targetUrl, LoadingType forcedLoadingType = LoadingType.DEFAULT) |
| | 91 | | { |
| 88 | 92 | | Assert.IsFalse(string.IsNullOrEmpty(targetUrl), "url is null!!"); |
| | 93 | | #if UNITY_EDITOR |
| 88 | 94 | | loadStartTime = Time.realtimeSinceStartup; |
| | 95 | | #endif |
| 88 | 96 | | LoadingType finalLoadingType = forcedLoadingType == LoadingType.DEFAULT ? defaultLoadingType : forcedLoading |
| | 97 | |
|
| | 98 | | switch (finalLoadingType) |
| | 99 | | { |
| | 100 | | case LoadingType.ASSET_BUNDLE_ONLY: |
| 0 | 101 | | LoadAssetBundle(targetUrl, OnSuccessEvent, OnFailEvent, false); |
| 0 | 102 | | break; |
| | 103 | | case LoadingType.GLTF_ONLY: |
| 8 | 104 | | ProxyLoadGltf(targetUrl, false); |
| 8 | 105 | | break; |
| | 106 | | case LoadingType.OLD_GLTF: |
| 0 | 107 | | LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent, false); |
| 0 | 108 | | break; |
| | 109 | | case LoadingType.DEFAULT: |
| | 110 | | case LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK: |
| 160 | 111 | | LoadAssetBundle(targetUrl, OnSuccessEvent, exception => ProxyLoadGltf(targetUrl, false), true); |
| 80 | 112 | | break; |
| | 113 | | case LoadingType.ASSET_BUNDLE_WITH_OLD_GLTF_FALLBACK: |
| 0 | 114 | | LoadAssetBundle(targetUrl, OnSuccessEvent, exception => LoadGltf(targetUrl, OnSuccessEvent, OnFailEv |
| | 115 | | break; |
| | 116 | | } |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | private void ProxyLoadGltf(string targetUrl, bool hasFallback) |
| | 120 | | { |
| 88 | 121 | | if (IsGltFastEnabled()) |
| 0 | 122 | | LoadGLTFast(targetUrl, OnSuccessEvent, _ => |
| | 123 | | { |
| 0 | 124 | | if (VERBOSE) |
| 0 | 125 | | Debug.Log($"GLTFast failed to load for {targetUrl} so we are going to fallback into old gltf"); |
| 0 | 126 | | LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent, hasFallback); |
| 0 | 127 | | }, true); |
| | 128 | | else |
| 88 | 129 | | LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent, hasFallback); |
| 88 | 130 | | } |
| | 131 | |
|
| | 132 | | public void Unload() |
| | 133 | | { |
| 70 | 134 | | UnloadAB(); |
| 70 | 135 | | UnloadGLTF(); |
| 70 | 136 | | UnloadGLTFast(); |
| 70 | 137 | | } |
| | 138 | |
|
| | 139 | | void UnloadAB() |
| | 140 | | { |
| 70 | 141 | | if (abPromise != null) |
| 0 | 142 | | { AssetPromiseKeeper_AB_GameObject.i.Forget(abPromise); } |
| 70 | 143 | | } |
| | 144 | |
|
| | 145 | | void UnloadGLTF() |
| | 146 | | { |
| 76 | 147 | | if (gltfPromise != null) |
| 70 | 148 | | { AssetPromiseKeeper_GLTF.i.Forget(gltfPromise); } |
| 76 | 149 | | } |
| | 150 | |
|
| | 151 | | void UnloadGLTFast() |
| | 152 | | { |
| 70 | 153 | | if (gltfastPromise != null) |
| 0 | 154 | | { AssetPromiseKeeper_GLTFast_Instance.i.Forget(gltfastPromise); } |
| 70 | 155 | | } |
| | 156 | |
|
| | 157 | | void LoadAssetBundle(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail, bool hasFallback |
| | 158 | | { |
| 80 | 159 | | currentLoadingSystem = AB_GO_NAME_PREFIX; |
| | 160 | |
|
| 80 | 161 | | if (abPromise != null) |
| | 162 | | { |
| 0 | 163 | | UnloadAB(); |
| | 164 | |
|
| 0 | 165 | | if (VERBOSE) |
| 0 | 166 | | Debug.Log("Forgetting not null promise..." + targetUrl); |
| | 167 | | } |
| | 168 | |
|
| 80 | 169 | | string bundlesBaseUrl = useCustomContentServerUrl ? customContentServerUrl : bundlesContentUrl; |
| | 170 | |
|
| 80 | 171 | | if (string.IsNullOrEmpty(bundlesBaseUrl)) |
| | 172 | | { |
| 80 | 173 | | OnFailWrapper(OnFail, new Exception("bundlesBaseUrl is null"), hasFallback); |
| 80 | 174 | | return; |
| | 175 | | } |
| | 176 | |
|
| 0 | 177 | | if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash)) |
| | 178 | | { |
| 0 | 179 | | OnFailWrapper(OnFail, new Exception($"Content url does not contains {targetUrl}"), hasFallback); |
| 0 | 180 | | return; |
| | 181 | | } |
| | 182 | |
|
| 0 | 183 | | abPromise = new AssetPromise_AB_GameObject(bundlesBaseUrl, hash); |
| 0 | 184 | | abPromise.settings = this.settings; |
| | 185 | |
|
| 0 | 186 | | abPromise.OnSuccessEvent += (x) => |
| | 187 | | { |
| | 188 | | #if UNITY_EDITOR |
| 0 | 189 | | x.container.name = AB_GO_NAME_PREFIX + x.container.name; |
| | 190 | | #endif |
| 0 | 191 | | var r = new Rendereable() |
| | 192 | | { |
| | 193 | | container = x.container, |
| | 194 | | totalTriangleCount = x.totalTriangleCount, |
| | 195 | | meshes = x.meshes, |
| | 196 | | renderers = x.renderers, |
| | 197 | | materials = x.materials, |
| | 198 | | textures = x.textures, |
| | 199 | | meshToTriangleCount = x.meshToTriangleCount, |
| | 200 | | animationClipSize = x.animationClipSize, |
| | 201 | | animationClips = x.animationClips, |
| | 202 | | meshDataSize = x.meshDataSize |
| | 203 | | }; |
| | 204 | |
|
| 0 | 205 | | foreach (var someRenderer in r.renderers) |
| 0 | 206 | | someRenderer.tag = FROM_ASSET_BUNDLE_TAG; |
| | 207 | |
|
| 0 | 208 | | OnSuccessWrapper(r, OnSuccess); |
| 0 | 209 | | }; |
| | 210 | |
|
| 0 | 211 | | abPromise.OnFailEvent += (x, exception) => OnFailWrapper(OnFail, exception, hasFallback); |
| | 212 | |
|
| 0 | 213 | | AssetPromiseKeeper_AB_GameObject.i.Keep(abPromise); |
| 0 | 214 | | } |
| | 215 | |
|
| | 216 | | void LoadGltf(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail, bool hasFallback) |
| | 217 | | { |
| 88 | 218 | | currentLoadingSystem = GLTF_GO_NAME_PREFIX; |
| | 219 | |
|
| 88 | 220 | | if (gltfPromise != null) |
| | 221 | | { |
| 6 | 222 | | UnloadGLTF(); |
| | 223 | |
|
| 6 | 224 | | if (VERBOSE) |
| 0 | 225 | | Debug.Log("Forgetting not null promise... " + targetUrl); |
| | 226 | | } |
| | 227 | |
|
| 88 | 228 | | if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash)) |
| | 229 | | { |
| 0 | 230 | | OnFailWrapper(OnFail, new Exception($"Content provider does not contains url {targetUrl}"), hasFallback) |
| 0 | 231 | | return; |
| | 232 | | } |
| | 233 | |
|
| 88 | 234 | | gltfPromise = new AssetPromise_GLTF(contentProvider, targetUrl, hash); |
| 88 | 235 | | gltfPromise.settings = settings; |
| | 236 | |
|
| 88 | 237 | | gltfPromise.OnSuccessEvent += (Asset_GLTF x) => |
| | 238 | | { |
| | 239 | | #if UNITY_EDITOR |
| 81 | 240 | | x.container.name = GLTF_GO_NAME_PREFIX + x.container.name; |
| | 241 | | #endif |
| 81 | 242 | | var r = new Rendereable |
| | 243 | | { |
| | 244 | | container = x.container, |
| | 245 | | totalTriangleCount = x.totalTriangleCount, |
| | 246 | | meshes = x.meshes, |
| | 247 | | renderers = x.renderers, |
| | 248 | | materials = x.materials, |
| | 249 | | textures = x.textures, |
| | 250 | | meshToTriangleCount = x.meshToTriangleCount, |
| | 251 | | animationClipSize = x.animationClipSize, |
| | 252 | | meshDataSize = x.meshDataSize, |
| | 253 | | animationClips = x.animationClips |
| | 254 | | }; |
| | 255 | |
|
| 536 | 256 | | foreach (var someRenderer in r.renderers) |
| 187 | 257 | | someRenderer.tag = FROM_RAW_GLTF_TAG; |
| | 258 | |
|
| 81 | 259 | | OnSuccessWrapper(r, OnSuccess); |
| 81 | 260 | | }; |
| | 261 | |
|
| 91 | 262 | | gltfPromise.OnFailEvent += (asset, exception) => OnFailWrapper(OnFail, exception, hasFallback); |
| | 263 | |
|
| 88 | 264 | | AssetPromiseKeeper_GLTF.i.Keep(gltfPromise); |
| 88 | 265 | | } |
| | 266 | |
|
| | 267 | | private void LoadGLTFast(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail, bool hasFall |
| | 268 | | { |
| 0 | 269 | | currentLoadingSystem = GLTFAST_GO_NAME_PREFIX; |
| | 270 | |
|
| 0 | 271 | | if (gltfastPromise != null) |
| | 272 | | { |
| 0 | 273 | | UnloadGLTFast(); |
| | 274 | |
|
| 0 | 275 | | if (VERBOSE) |
| 0 | 276 | | Debug.Log("Forgetting not null promise... " + targetUrl); |
| | 277 | | } |
| | 278 | |
|
| 0 | 279 | | if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash)) |
| | 280 | | { |
| 0 | 281 | | OnFailWrapper(OnFail, new Exception($"Content provider does not contains url {targetUrl}"), hasFallback) |
| 0 | 282 | | return; |
| | 283 | | } |
| | 284 | |
|
| 0 | 285 | | gltfastPromise = new AssetPromise_GLTFast_Instance(targetUrl, hash, |
| | 286 | | Environment.i.platform.webRequest, contentProvider, settings); |
| | 287 | |
|
| 0 | 288 | | gltfastPromise.OnSuccessEvent += (Asset_GLTFast_Instance x) => |
| | 289 | | { |
| | 290 | | #if UNITY_EDITOR |
| 0 | 291 | | x.container.name = GLTFAST_GO_NAME_PREFIX + x.container.name; |
| | 292 | | #endif |
| 0 | 293 | | Rendereable r = x.ToRendereable(); |
| 0 | 294 | | foreach (var someRenderer in r.renderers) |
| 0 | 295 | | someRenderer.tag = FROM_RAW_GLTF_TAG; |
| | 296 | |
|
| 0 | 297 | | OnSuccessWrapper(r, OnSuccess); |
| 0 | 298 | | }; |
| | 299 | |
|
| 0 | 300 | | gltfastPromise.OnFailEvent += (asset, exception) => |
| | 301 | | { |
| 0 | 302 | | OnFailWrapper(OnFail, exception, hasFallback); |
| 0 | 303 | | }; |
| | 304 | |
|
| 0 | 305 | | AssetPromiseKeeper_GLTFast_Instance.i.Keep(gltfastPromise); |
| 0 | 306 | | } |
| | 307 | |
|
| | 308 | | private void OnFailWrapper(Action<Exception> OnFail, Exception exception, bool hasFallback) |
| | 309 | | { |
| | 310 | | #if UNITY_EDITOR |
| 83 | 311 | | loadFinishTime = Time.realtimeSinceStartup; |
| | 312 | | #endif |
| | 313 | |
|
| | 314 | | // If the entity is destroyed while loading, the exception is expected to be null and no error should be thr |
| 83 | 315 | | if (exception != null) |
| | 316 | | { |
| 80 | 317 | | if (!hasFallback) |
| 0 | 318 | | Debug.LogException(exception); |
| 80 | 319 | | else if (VERBOSE) |
| | 320 | | { |
| 0 | 321 | | Debug.Log($"Load Fail Detected, trying to use a fallback, " + |
| | 322 | | $"loading type was: {currentLoadingSystem} and error was: {exception.Message}"); |
| | 323 | | } |
| | 324 | | } |
| 83 | 325 | | OnFail?.Invoke(exception); |
| 83 | 326 | | ClearEvents(); |
| 83 | 327 | | } |
| | 328 | |
|
| | 329 | | private void OnSuccessWrapper(Rendereable loadedAsset, Action<Rendereable> OnSuccess) |
| | 330 | | { |
| | 331 | | #if UNITY_EDITOR |
| 81 | 332 | | loadFinishTime = Time.realtimeSinceStartup; |
| | 333 | | #endif |
| 81 | 334 | | if (VERBOSE) |
| | 335 | | { |
| 0 | 336 | | if (gltfPromise != null) |
| 0 | 337 | | Debug.Log($"GLTF Load(): target URL -> {gltfPromise.GetId()}. Success!"); |
| | 338 | | else |
| 0 | 339 | | Debug.Log($"AB Load(): target URL -> {abPromise.hash}. Success!"); |
| | 340 | | } |
| | 341 | |
|
| 81 | 342 | | this.loadedAsset = loadedAsset; |
| 81 | 343 | | OnSuccess?.Invoke(loadedAsset); |
| 81 | 344 | | ClearEvents(); |
| 81 | 345 | | } |
| | 346 | |
|
| | 347 | | public void ClearEvents() |
| | 348 | | { |
| 192 | 349 | | OnSuccessEvent = null; |
| 192 | 350 | | OnFailEvent = null; |
| 192 | 351 | | } |
| | 352 | | } |
| | 353 | | } |