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