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