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