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