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