| | 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 | |
|
| 92 | 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 | | { |
| 18 | 32 | | if (gltfPromise != null) |
| 18 | 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; |
| 92 | 74 | | float loadFinishTime = float.MaxValue; |
| | 75 | | #endif |
| | 76 | |
|
| 92 | 77 | | public RendereableAssetLoadHelper(ContentProvider contentProvider, string bundlesContentUrl) |
| | 78 | | { |
| 92 | 79 | | this.contentProvider = contentProvider; |
| 92 | 80 | | this.bundlesContentUrl = bundlesContentUrl; |
| 92 | 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 | | { |
| 92 | 88 | | Assert.IsFalse(string.IsNullOrEmpty(targetUrl), "url is null!!"); |
| | 89 | | #if UNITY_EDITOR |
| 92 | 90 | | loadStartTime = Time.realtimeSinceStartup; |
| | 91 | | #endif |
| | 92 | |
|
| 92 | 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: |
| 13 | 100 | | LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent); |
| 13 | 101 | | break; |
| | 102 | | case LoadingType.DEFAULT: |
| | 103 | | case LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK: |
| 158 | 104 | | LoadAssetBundle(targetUrl, OnSuccessEvent, exception => LoadGltf(targetUrl, OnSuccessEvent, OnFailEv |
| | 105 | | break; |
| | 106 | | } |
| 79 | 107 | | } |
| | 108 | |
|
| | 109 | | public void Unload() |
| | 110 | | { |
| 79 | 111 | | UnloadAB(); |
| 79 | 112 | | UnloadGLTF(); |
| 79 | 113 | | } |
| | 114 | |
|
| | 115 | | void UnloadAB() |
| | 116 | | { |
| 79 | 117 | | if ( abPromise != null ) |
| | 118 | | { |
| 0 | 119 | | AssetPromiseKeeper_AB_GameObject.i.Forget(abPromise); |
| | 120 | | } |
| 79 | 121 | | } |
| | 122 | |
|
| | 123 | | void UnloadGLTF() |
| | 124 | | { |
| 79 | 125 | | if ( gltfPromise != null ) |
| | 126 | | { |
| 79 | 127 | | AssetPromiseKeeper_GLTF.i.Forget(gltfPromise); |
| | 128 | | } |
| 79 | 129 | | } |
| | 130 | |
|
| | 131 | | void LoadAssetBundle(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail) |
| | 132 | | { |
| 79 | 133 | | if (abPromise != null) |
| | 134 | | { |
| 0 | 135 | | UnloadAB(); |
| 0 | 136 | | if (VERBOSE) |
| 0 | 137 | | Debug.Log("Forgetting not null promise..." + targetUrl); |
| | 138 | | } |
| | 139 | |
|
| 79 | 140 | | string bundlesBaseUrl = useCustomContentServerUrl ? customContentServerUrl : bundlesContentUrl; |
| | 141 | |
|
| 79 | 142 | | if (string.IsNullOrEmpty(bundlesBaseUrl)) |
| | 143 | | { |
| 79 | 144 | | OnFailWrapper(OnFail, new Exception("bundlesBaseUrl is null")); |
| 79 | 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 | | meshToTriangleCount = x.meshToTriangleCount |
| | 166 | | }; |
| | 167 | |
|
| 0 | 168 | | OnSuccessWrapper(r, OnSuccess); |
| 0 | 169 | | }; |
| | 170 | |
|
| 0 | 171 | | abPromise.OnFailEvent += (x, exception) => OnFailWrapper(OnFail, exception); |
| | 172 | |
|
| 0 | 173 | | AssetPromiseKeeper_AB_GameObject.i.Keep(abPromise); |
| 0 | 174 | | } |
| | 175 | |
|
| | 176 | | void LoadGltf(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail) |
| | 177 | | { |
| 92 | 178 | | if (gltfPromise != null) |
| | 179 | | { |
| 0 | 180 | | UnloadGLTF(); |
| | 181 | |
|
| 0 | 182 | | if (VERBOSE) |
| 0 | 183 | | Debug.Log("Forgetting not null promise... " + targetUrl); |
| | 184 | | } |
| | 185 | |
|
| 92 | 186 | | if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash)) |
| | 187 | | { |
| 0 | 188 | | OnFailWrapper(OnFail, new Exception($"Content provider does not contains url {targetUrl}")); |
| 0 | 189 | | return; |
| | 190 | | } |
| | 191 | |
|
| 92 | 192 | | gltfPromise = new AssetPromise_GLTF(contentProvider, targetUrl, hash); |
| 92 | 193 | | gltfPromise.settings = settings; |
| | 194 | |
|
| 92 | 195 | | gltfPromise.OnSuccessEvent += (x) => |
| | 196 | | { |
| 82 | 197 | | var r = new Rendereable |
| | 198 | | { |
| | 199 | | container = x.container, |
| | 200 | | totalTriangleCount = x.totalTriangleCount, |
| | 201 | | meshes = x.meshes, |
| | 202 | | renderers = x.renderers, |
| | 203 | | meshToTriangleCount = x.meshToTriangleCount |
| | 204 | | }; |
| | 205 | |
|
| 82 | 206 | | OnSuccessWrapper(r, OnSuccess); |
| 82 | 207 | | }; |
| 95 | 208 | | gltfPromise.OnFailEvent += (asset, exception) => OnFailWrapper(OnFail, exception); |
| | 209 | |
|
| 92 | 210 | | AssetPromiseKeeper_GLTF.i.Keep(gltfPromise); |
| 92 | 211 | | } |
| | 212 | |
|
| | 213 | | private void OnFailWrapper(Action<Exception> OnFail, Exception exception) |
| | 214 | | { |
| | 215 | | #if UNITY_EDITOR |
| 82 | 216 | | loadFinishTime = Time.realtimeSinceStartup; |
| | 217 | | #endif |
| | 218 | |
|
| 82 | 219 | | OnFail?.Invoke(exception); |
| 82 | 220 | | ClearEvents(); |
| 82 | 221 | | } |
| | 222 | |
|
| | 223 | | private void OnSuccessWrapper(Rendereable loadedAsset, 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 | | } |