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