| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Configuration; |
| | 6 | | using UnityEngine; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using DCL.Models; |
| | 9 | |
|
| | 10 | | namespace DCL |
| | 11 | | { |
| | 12 | | public class AssetPromise_AB_GameObject : AssetPromise_WithUrl<Asset_AB_GameObject> |
| | 13 | | { |
| 39 | 14 | | public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering(); |
| | 15 | | AssetPromise_AB subPromise; |
| | 16 | | Coroutine loadingCoroutine; |
| | 17 | |
|
| 39 | 18 | | public AssetPromise_AB_GameObject(string contentUrl, string hash) : base(contentUrl, hash) |
| | 19 | | { |
| 39 | 20 | | } |
| | 21 | |
|
| | 22 | | protected override void OnLoad(Action OnSuccess, Action OnFail) |
| | 23 | | { |
| 15 | 24 | | loadingCoroutine = CoroutineStarter.Start(LoadingCoroutine(OnSuccess, OnFail)); |
| 15 | 25 | | } |
| | 26 | |
|
| | 27 | | protected override bool AddToLibrary() |
| | 28 | | { |
| 10 | 29 | | if (!library.Add(asset)) |
| | 30 | | { |
| 0 | 31 | | return false; |
| | 32 | | } |
| | 33 | |
|
| 10 | 34 | | if (settings.forceNewInstance) |
| | 35 | | { |
| 2 | 36 | | asset = (library as AssetLibrary_AB_GameObject).GetCopyFromOriginal(asset.id); |
| 2 | 37 | | } |
| | 38 | | else |
| | 39 | | { |
| 8 | 40 | | asset = library.Get(asset.id); |
| | 41 | | } |
| | 42 | |
|
| | 43 | | //NOTE(Brian): Call again this method because we are replacing the asset. |
| 10 | 44 | | settings.ApplyBeforeLoad(asset.container.transform); |
| | 45 | |
|
| 10 | 46 | | return true; |
| | 47 | | } |
| | 48 | |
|
| | 49 | | protected override void OnReuse(Action OnSuccess) |
| | 50 | | { |
| 24 | 51 | | asset.renderers = asset.container.GetComponentsInChildren<Renderer>(true).ToList(); |
| 24 | 52 | | asset.Show(OnSuccess); |
| 24 | 53 | | } |
| | 54 | |
|
| | 55 | | protected override void OnAfterLoadOrReuse() |
| | 56 | | { |
| 34 | 57 | | settings.ApplyAfterLoad(asset.container.transform); |
| 34 | 58 | | } |
| | 59 | |
|
| | 60 | | protected override void OnBeforeLoadOrReuse() |
| | 61 | | { |
| | 62 | | #if UNITY_EDITOR |
| 39 | 63 | | asset.container.name = "AB: " + hash; |
| | 64 | | #endif |
| 39 | 65 | | settings.ApplyBeforeLoad(asset.container.transform); |
| 39 | 66 | | } |
| | 67 | |
|
| | 68 | | protected override void OnCancelLoading() |
| | 69 | | { |
| 5 | 70 | | if (loadingCoroutine != null) |
| | 71 | | { |
| 5 | 72 | | CoroutineStarter.Stop(loadingCoroutine); |
| 5 | 73 | | loadingCoroutine = null; |
| | 74 | | } |
| | 75 | |
|
| 5 | 76 | | if (asset != null) |
| 5 | 77 | | UnityEngine.Object.Destroy(asset.container); |
| | 78 | |
|
| 5 | 79 | | AssetPromiseKeeper_AB.i.Forget(subPromise); |
| 5 | 80 | | } |
| | 81 | |
|
| | 82 | |
|
| | 83 | | public override string ToString() |
| | 84 | | { |
| 0 | 85 | | if (subPromise != null) |
| 0 | 86 | | return $"{subPromise.ToString()} ... AB_GameObject state = {state}"; |
| | 87 | | else |
| 0 | 88 | | return $"subPromise == null? state = {state}"; |
| | 89 | | } |
| | 90 | |
|
| | 91 | | public IEnumerator LoadingCoroutine(Action OnSuccess, Action OnFail) |
| | 92 | | { |
| 15 | 93 | | subPromise = new AssetPromise_AB(contentUrl, hash, asset.container.transform); |
| 15 | 94 | | bool success = false; |
| 27 | 95 | | subPromise.OnSuccessEvent += (x) => success = true; |
| 15 | 96 | | asset.ownerPromise = subPromise; |
| 15 | 97 | | AssetPromiseKeeper_AB.i.Keep(subPromise); |
| | 98 | |
|
| 15 | 99 | | yield return subPromise; |
| | 100 | |
|
| 13 | 101 | | if (success) |
| | 102 | | { |
| 12 | 103 | | yield return InstantiateABGameObjects(); |
| | 104 | |
|
| 12 | 105 | | if (subPromise.asset == null || asset.container == null) |
| 2 | 106 | | success = false; |
| | 107 | | } |
| | 108 | |
|
| 13 | 109 | | if (success) |
| | 110 | | { |
| 10 | 111 | | OnSuccess?.Invoke(); |
| 10 | 112 | | } |
| | 113 | | else |
| | 114 | | { |
| 3 | 115 | | OnFail?.Invoke(); |
| | 116 | | } |
| 13 | 117 | | } |
| | 118 | |
|
| | 119 | |
|
| | 120 | | public IEnumerator InstantiateABGameObjects() |
| | 121 | | { |
| 12 | 122 | | var goList = subPromise.asset.GetAssetsByExtensions<GameObject>("glb", "ltf"); |
| | 123 | |
|
| 12 | 124 | | if (goList.Count == 0) |
| | 125 | | { |
| 1 | 126 | | if (asset.container != null) |
| 1 | 127 | | UnityEngine.Object.Destroy(asset.container); |
| | 128 | |
|
| 1 | 129 | | asset.container = null; |
| | 130 | |
|
| 1 | 131 | | AssetPromiseKeeper_AB.i.Forget(subPromise); |
| | 132 | |
|
| 1 | 133 | | yield break; |
| | 134 | | } |
| | 135 | |
|
| 42 | 136 | | for (int i = 0; i < goList.Count; i++) |
| | 137 | | { |
| 11 | 138 | | if (loadingCoroutine == null) |
| | 139 | | break; |
| | 140 | |
|
| 11 | 141 | | if (asset.container == null) |
| | 142 | | break; |
| | 143 | |
|
| 10 | 144 | | GameObject assetBundleModelGO = UnityEngine.Object.Instantiate(goList[i], asset.container.transform); |
| | 145 | |
|
| 10 | 146 | | List<Renderer> rendererList = assetBundleModelGO.GetComponentsInChildren<Renderer>(true).ToList(); |
| | 147 | |
|
| 10 | 148 | | asset.renderers.AddRange(rendererList); |
| 10 | 149 | | UploadMeshesToGPU(MeshesInfoUtils.ExtractMeshes(assetBundleModelGO)); |
| 10 | 150 | | asset.totalTriangleCount = MeshesInfoUtils.ComputeTotalTriangles(asset.renderers, asset.meshToTriangleCo |
| | 151 | |
|
| | 152 | | //NOTE(Brian): Renderers are enabled in settings.ApplyAfterLoad |
| 10 | 153 | | yield return MaterialCachingHelper.Process(rendererList, enableRenderers: false, settings.cachingFlags); |
| | 154 | |
|
| 10 | 155 | | var animators = assetBundleModelGO.GetComponentsInChildren<Animation>(true); |
| | 156 | |
|
| 20 | 157 | | for (int animIndex = 0; animIndex < animators.Length; animIndex++) |
| | 158 | | { |
| 0 | 159 | | animators[animIndex].cullingType = AnimationCullingType.AlwaysAnimate; |
| | 160 | | } |
| | 161 | |
|
| | 162 | | #if UNITY_EDITOR |
| 10 | 163 | | assetBundleModelGO.name = subPromise.asset.assetBundleAssetName; |
| | 164 | | #endif |
| | 165 | | //assetBundleModelGO.transform.SetParent(asset.container.transform, false); |
| 10 | 166 | | assetBundleModelGO.transform.ResetLocalTRS(); |
| 10 | 167 | | yield return null; |
| 10 | 168 | | } |
| 11 | 169 | | } |
| | 170 | |
|
| | 171 | | private void UploadMeshesToGPU(List<Mesh> meshesList) |
| | 172 | | { |
| 60 | 173 | | foreach ( Mesh mesh in meshesList ) |
| | 174 | | { |
| 20 | 175 | | if ( !mesh.isReadable ) |
| | 176 | | continue; |
| | 177 | |
|
| 20 | 178 | | Physics.BakeMesh(mesh.GetInstanceID(), false); |
| 20 | 179 | | asset.meshToTriangleCount[mesh] = mesh.triangles.Length; |
| 20 | 180 | | asset.meshes.Add(mesh); |
| 20 | 181 | | mesh.UploadMeshData(true); |
| | 182 | | } |
| 10 | 183 | | } |
| | 184 | |
|
| | 185 | | protected override Asset_AB_GameObject GetAsset(object id) |
| | 186 | | { |
| 24 | 187 | | if (settings.forceNewInstance) |
| | 188 | | { |
| 9 | 189 | | return ((AssetLibrary_AB_GameObject) library).GetCopyFromOriginal(id); |
| | 190 | | } |
| | 191 | | else |
| | 192 | | { |
| 15 | 193 | | return base.GetAsset(id); |
| | 194 | | } |
| | 195 | | } |
| | 196 | | } |
| | 197 | | } |