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