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