| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using Object = UnityEngine.Object; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public class Asset_AB : Asset |
| | 9 | | { |
| | 10 | | const string METADATA_FILENAME = "metadata.json"; |
| | 11 | | const string METRICS_FILENAME = "metrics.json"; |
| | 12 | |
|
| | 13 | | private AssetBundle assetBundle; |
| | 14 | | private Dictionary<string, List<Object>> assetsByExtension; |
| 30 | 15 | | public AssetBundleMetrics metrics { get; private set; } = new AssetBundleMetrics { meshesEstimatedSize = 0, anim |
| | 16 | |
|
| 30 | 17 | | public Asset_AB() |
| | 18 | | { |
| 30 | 19 | | assetsByExtension = new Dictionary<string, List<Object>>(); |
| 30 | 20 | | } |
| | 21 | |
|
| 0 | 22 | | public override object Clone() => (Asset_AB) MemberwiseClone(); |
| 12 | 23 | | public string GetName() => assetBundle.name; |
| | 24 | |
|
| 8 | 25 | | public void CancelShow() => Cleanup(); |
| 23 | 26 | | public bool IsValid() => assetBundle != null; |
| | 27 | |
|
| | 28 | | public override void Cleanup() |
| | 29 | | { |
| 38 | 30 | | assetsByExtension = null; |
| | 31 | |
|
| 38 | 32 | | if (assetBundle) |
| | 33 | | { |
| 23 | 34 | | assetBundle.Unload(true); |
| 23 | 35 | | assetBundle = null; |
| | 36 | | } |
| 38 | 37 | | } |
| | 38 | |
|
| | 39 | | public List<T> GetAssetsByExtensions<T>(params string[] extensions) |
| | 40 | | where T : Object |
| | 41 | | { |
| 15 | 42 | | var goList = new List<T>(); |
| | 43 | |
|
| 90 | 44 | | for (int i1 = 0; i1 < extensions.Length; i1++) |
| | 45 | | { |
| 30 | 46 | | string ext = extensions[i1]; |
| | 47 | | List<Object> assets; |
| | 48 | |
|
| 30 | 49 | | if (assetsByExtension.ContainsKey(ext)) |
| | 50 | | { |
| 13 | 51 | | assets = assetsByExtension[ext]; |
| 13 | 52 | | int glbCount = assets.Count; |
| | 53 | |
|
| 52 | 54 | | for (int i = 0; i < glbCount; i++) |
| | 55 | | { |
| 13 | 56 | | Object go = assets[i]; |
| | 57 | |
|
| 13 | 58 | | if (go is T) |
| 13 | 59 | | goList.Add((T) go); |
| | 60 | | } |
| | 61 | | } |
| | 62 | | } |
| | 63 | |
|
| 15 | 64 | | return goList; |
| | 65 | | } |
| | 66 | | public void AddAssetByExtension(string ext, Object loadedAsset) |
| | 67 | | { |
| 67 | 68 | | if (assetsByExtension == null) |
| | 69 | | { |
| 0 | 70 | | Debug.LogWarning($"Trying to add asset of type {ext} to unloaded AB"); |
| | 71 | |
|
| 0 | 72 | | return; |
| | 73 | | } |
| | 74 | |
|
| 67 | 75 | | if (!assetsByExtension.ContainsKey(ext)) |
| | 76 | | { |
| 48 | 77 | | assetsByExtension.Add(ext, new List<Object>()); |
| | 78 | | } |
| | 79 | |
|
| 67 | 80 | | assetsByExtension[ext].Add(loadedAsset); |
| 67 | 81 | | } |
| | 82 | | public void SetAssetBundle(AssetBundle ab) |
| | 83 | | { |
| 0 | 84 | | assetBundle = ab; |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | public void LoadMetrics() |
| | 88 | | { |
| 23 | 89 | | var metricsFile = assetBundle.LoadAsset<TextAsset>(METRICS_FILENAME); |
| 23 | 90 | | if (metricsFile != null) |
| 0 | 91 | | metrics = JsonUtility.FromJson<AssetBundleMetrics>(metricsFile.text); |
| 23 | 92 | | } |
| | 93 | |
|
| | 94 | | public TextAsset GetMetadata() |
| | 95 | | { |
| 23 | 96 | | return assetBundle.LoadAsset<TextAsset>(METADATA_FILENAME); |
| | 97 | | } |
| | 98 | | public AssetBundleRequest LoadAllAssetsAsync() |
| | 99 | | { |
| 22 | 100 | | return assetBundle.LoadAllAssetsAsync(); |
| | 101 | | } |
| | 102 | | } |
| | 103 | | } |