| | 1 | | using System.Linq; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Models; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL.Components |
| | 7 | | { |
| | 8 | | public class LoadWrapper_NFT : LoadWrapper |
| | 9 | | { |
| | 10 | | public NFTShapeLoaderController loaderController; |
| | 11 | |
|
| | 12 | | public bool withCollisions; |
| | 13 | | public Color backgroundColor; |
| | 14 | |
|
| | 15 | | private string assetUrl; |
| | 16 | | private Rendereable rendereable; |
| | 17 | |
|
| | 18 | | public override void Unload() |
| | 19 | | { |
| 8 | 20 | | CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChanged; |
| 8 | 21 | | loaderController.OnLoadingAssetSuccess -= OnLoadingAssetSuccess; |
| | 22 | |
|
| 8 | 23 | | Object.Destroy(loaderController); |
| | 24 | |
|
| 8 | 25 | | Utils.SafeDestroy(entity.meshRootGameObject); |
| 8 | 26 | | entity.meshesInfo.CleanReferences(); |
| | 27 | |
|
| 8 | 28 | | DataStore.i.sceneWorldObjects.RemoveRendereable(entity, rendereable); |
| 8 | 29 | | } |
| | 30 | |
|
| | 31 | | public override void Load(string src, System.Action<LoadWrapper> OnSuccess, System.Action<LoadWrapper> OnFail) |
| | 32 | | { |
| 10 | 33 | | if (string.IsNullOrEmpty(src)) |
| 0 | 34 | | return; |
| | 35 | |
|
| 10 | 36 | | if (loaderController == null) |
| 10 | 37 | | loaderController = entity.meshRootGameObject.GetComponent<NFTShapeLoaderController>(); |
| | 38 | |
|
| 10 | 39 | | if (loaderController == null) |
| | 40 | | { |
| 0 | 41 | | Debug.LogError("LoadWrapper_NFT >>> loaderController == null!"); |
| 0 | 42 | | return; |
| | 43 | | } |
| | 44 | |
|
| 10 | 45 | | loaderController.collider.enabled = withCollisions; |
| 10 | 46 | | loaderController.backgroundColor = backgroundColor; |
| | 47 | |
|
| 10 | 48 | | loaderController.OnLoadingAssetSuccess += OnLoadingAssetSuccess; |
| | 49 | |
|
| 10 | 50 | | assetUrl = src; |
| | 51 | |
|
| 10 | 52 | | if (CommonScriptableObjects.rendererState.Get()) |
| | 53 | | { |
| 10 | 54 | | LoadAsset(); |
| 10 | 55 | | } |
| | 56 | | else |
| | 57 | | { |
| 0 | 58 | | CommonScriptableObjects.rendererState.OnChange += OnRendererStateChanged; |
| | 59 | | } |
| | 60 | |
|
| | 61 | | // NOTE: frame meshes are already included in the build, there is no need lock renderer just to wait for the |
| 10 | 62 | | OnSuccess?.Invoke(this); |
| 10 | 63 | | } |
| | 64 | |
|
| | 65 | | void OnLoadingAssetSuccess() |
| | 66 | | { |
| 14 | 67 | | if (alreadyLoaded) |
| 7 | 68 | | return; |
| | 69 | |
|
| 7 | 70 | | alreadyLoaded = true; |
| | 71 | |
|
| 7 | 72 | | rendereable = Rendereable.CreateFromGameObject(entity.meshRootGameObject); |
| 7 | 73 | | DataStore.i.sceneWorldObjects.AddRendereable(entity, rendereable); |
| | 74 | |
|
| 7 | 75 | | entity.OnShapeUpdated?.Invoke(entity); |
| 7 | 76 | | entity.OnShapeLoaded?.Invoke(entity); |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | void LoadAsset() |
| | 80 | | { |
| 10 | 81 | | if ( loaderController != null ) |
| 10 | 82 | | loaderController.LoadAsset(assetUrl, true); |
| 10 | 83 | | } |
| | 84 | |
|
| | 85 | | void OnRendererStateChanged(bool current, bool previous) |
| | 86 | | { |
| 0 | 87 | | if (current) |
| | 88 | | { |
| 0 | 89 | | CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChanged; |
| 0 | 90 | | LoadAsset(); |
| | 91 | | } |
| 0 | 92 | | } |
| | 93 | | } |
| | 94 | | } |