| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.Assertions; |
| | 4 | |
|
| | 5 | | namespace DCL.Components |
| | 6 | | { |
| | 7 | | public class LoadWrapper_GLTF : LoadWrapper |
| | 8 | | { |
| | 9 | | static readonly bool VERBOSE = false; |
| | 10 | |
|
| | 11 | | RendereableAssetLoadHelper loadHelper; |
| | 12 | |
|
| | 13 | | public ContentProvider customContentProvider; |
| | 14 | |
|
| | 15 | | private Action<Rendereable> successWrapperEvent; |
| | 16 | | private Action<Exception> failWrapperEvent; |
| | 17 | |
|
| | 18 | | public override void Load(string targetUrl, Action<LoadWrapper> OnSuccess, Action<LoadWrapper, Exception> OnFail |
| | 19 | | { |
| 89 | 20 | | if (loadHelper != null) |
| | 21 | | { |
| 2 | 22 | | loadHelper.Unload(); |
| | 23 | |
|
| 2 | 24 | | if (VERBOSE) |
| 0 | 25 | | Debug.Log("Forgetting not null loader..."); |
| | 26 | | } |
| | 27 | |
|
| 89 | 28 | | alreadyLoaded = false; |
| 89 | 29 | | Assert.IsFalse(string.IsNullOrEmpty(targetUrl), "url is null!!"); |
| | 30 | |
|
| | 31 | |
|
| 89 | 32 | | if (customContentProvider == null) |
| 0 | 33 | | loadHelper = new RendereableAssetLoadHelper(this.entity.scene.contentProvider, entity.scene.sceneData.ba |
| | 34 | | else |
| 89 | 35 | | loadHelper = new RendereableAssetLoadHelper(customContentProvider, entity.scene.sceneData.baseUrlBundles |
| | 36 | |
|
| 89 | 37 | | loadHelper.settings.forceGPUOnlyMesh = true; |
| 89 | 38 | | loadHelper.settings.parent = entity.meshRootGameObject.transform; |
| | 39 | |
|
| 89 | 40 | | if (initialVisibility == false) |
| | 41 | | { |
| 0 | 42 | | loadHelper.settings.visibleFlags = AssetPromiseSettings_Rendering.VisibleFlags.INVISIBLE; |
| 0 | 43 | | } |
| | 44 | | else |
| | 45 | | { |
| 89 | 46 | | if (useVisualFeedback) |
| 1 | 47 | | loadHelper.settings.visibleFlags = AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TRANSITI |
| | 48 | | else |
| 88 | 49 | | loadHelper.settings.visibleFlags = AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITHOUT_TRANS |
| | 50 | | } |
| | 51 | |
|
| 89 | 52 | | this.entity.OnCleanupEvent -= OnEntityCleanup; |
| 89 | 53 | | this.entity.OnCleanupEvent += OnEntityCleanup; |
| | 54 | |
|
| 157 | 55 | | successWrapperEvent = x => OnSuccessWrapper(OnSuccess); |
| 92 | 56 | | failWrapperEvent = error => OnFailWrapper(OnFail, error); |
| | 57 | |
|
| 89 | 58 | | loadHelper.OnSuccessEvent += successWrapperEvent; |
| 89 | 59 | | loadHelper.OnFailEvent += failWrapperEvent; |
| 89 | 60 | | loadHelper.Load(targetUrl); |
| 89 | 61 | | } |
| | 62 | |
|
| | 63 | | private void OnFailWrapper(Action<LoadWrapper, Exception> OnFail, Exception exception) |
| | 64 | | { |
| 3 | 65 | | alreadyLoaded = true; |
| 3 | 66 | | loadHelper.OnSuccessEvent -= successWrapperEvent; |
| 3 | 67 | | loadHelper.OnFailEvent -= failWrapperEvent; |
| 3 | 68 | | this.entity.OnCleanupEvent -= OnEntityCleanup; |
| 3 | 69 | | OnFail?.Invoke(this, exception); |
| 3 | 70 | | } |
| | 71 | |
|
| | 72 | | private void OnSuccessWrapper(Action<LoadWrapper> OnSuccess) |
| | 73 | | { |
| 68 | 74 | | alreadyLoaded = true; |
| 68 | 75 | | loadHelper.OnSuccessEvent -= successWrapperEvent; |
| 68 | 76 | | loadHelper.OnFailEvent -= failWrapperEvent; |
| | 77 | |
|
| 68 | 78 | | loadHelper.loadedAsset.ownerId = entity.entityId; |
| 68 | 79 | | DataStore.i.sceneWorldObjects.AddRendereable(entity.scene.sceneData.id, loadHelper.loadedAsset); |
| 68 | 80 | | OnSuccess?.Invoke(this); |
| 68 | 81 | | } |
| | 82 | |
|
| 2 | 83 | | public void OnEntityCleanup(ICleanableEventDispatcher source) { Unload(); } |
| | 84 | |
|
| | 85 | | public override void Unload() |
| | 86 | | { |
| 20 | 87 | | if ( loadHelper.loadedAsset != null ) |
| | 88 | | { |
| 15 | 89 | | DataStore.i.sceneWorldObjects.RemoveRendereable(entity.scene.sceneData.id, loadHelper.loadedAsset); |
| | 90 | | } |
| | 91 | |
|
| 20 | 92 | | loadHelper.Unload(); |
| 20 | 93 | | this.entity.OnCleanupEvent -= OnEntityCleanup; |
| 20 | 94 | | loadHelper.OnSuccessEvent -= successWrapperEvent; |
| 20 | 95 | | loadHelper.OnFailEvent -= failWrapperEvent; |
| 20 | 96 | | alreadyLoaded = false; |
| 20 | 97 | | } |
| | 98 | |
|
| 0 | 99 | | public override string ToString() { return $"LoadWrapper ... {loadHelper.ToString()}"; } |
| | 100 | | } |
| | 101 | | } |