Tanoda
pb_ResourceManager.cs
Go to the documentation of this file.
1using UnityEngine;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5
6namespace GILES
7{
8
12 public class pb_ResourceManager : pb_ScriptableObjectSingleton<pb_ResourceManager>
13 {
15 Dictionary<string, GameObject> lookup = new Dictionary<string, GameObject>();
16
21 protected override void OnEnable()
22 {
23 base.OnEnable();
24
25 foreach(string resourceFolder in pb_Config.Resource_Folder_Paths)
26 {
27 GameObject[] prefabs = Resources.LoadAll(resourceFolder).Select(x => x is GameObject ? (GameObject)x : null).Where(y => y != null).ToArray();
28
29#if ASSET_LOADER_DEBUG
30 System.Text.StringBuilder sb = new System.Text.StringBuilder();
31 sb.AppendLine("Loaded Resources: ");
32#endif
33
34 // Populate a dictionary to use as a lookup, then unload whatever isn't used
35 for(int i = 0; i < prefabs.Length; i++)
36 {
37#if ASSET_LOADER_DEBUG
38 sb.AppendLine(string.Format("{0,-50} : {1}", prefabs[i].name, prefabs[i].GetComponent<pb_MetaDataComponent>().metadata.fileId) );
39#endif
40 lookup.Add(prefabs[i].GetComponent<pb_MetaDataComponent>().metadata.fileId, prefabs[i]);
41 }
42
43#if ASSET_LOADER_DEBUG
44 Debug.Log(sb.ToString());
45#endif
46
47 Resources.UnloadUnusedAssets();
48 }
49 }
50
51 public static GameObject LoadPrefabWithId(string fileId)
52 {
53 if( string.IsNullOrEmpty(fileId) )
54 return null;
55
56 GameObject obj = null;
57
58 if(pb_ResourceManager.instance.lookup.TryGetValue(fileId, out obj))
59 return obj;
60
61 return null;
62 }
63
68 public static GameObject LoadPrefabWithMetadata(pb_MetaData metadata)
69 {
70 if( metadata.assetType == AssetType.Instance )
71 {
72 Debug.LogWarning("Attempting to load instance asset through resource manager.");
73 return null;
74 }
75
76 switch(metadata.assetType)
77 {
78 case AssetType.Resource:
79 {
80 if(instance.lookup.ContainsKey(metadata.fileId))
81 {
82 return instance.lookup[metadata.fileId];
83 }
84 else
85 {
86 Debug.LogWarning("Resource manager could not find \"" + metadata.fileId + "\" in loaded resources.");
87 return null;
88 }
89 }
90
91 case AssetType.Bundle:
92 {
93 return pb_AssetBundles.LoadAsset<GameObject>(metadata.assetBundlePath);
94 }
95
96 default:
97 {
98 Debug.LogError("File not found from metadata: " + metadata);
99 return null;
100 }
101 }
102 }
103
107 public static IEnumerable<T> LoadAll<T>() where T : UnityEngine.Object
108 {
109 List<T> assets = new List<T>();
110
111 foreach(string path in pb_Config.Resource_Folder_Paths)
112 {
113 assets.AddRange( Resources.LoadAll<T>(path) );
114 }
115
116 foreach(string bundleName in pb_Config.AssetBundle_Names)
117 {
118 try
119 {
120 AssetBundle bundle = pb_AssetBundles.LoadAssetBundleWithName(bundleName);
121 assets.AddRange( bundle.LoadAllAssets<T>() );
122 }
123 catch {}
124 }
125
126 return assets;
127 }
128 }
129}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
static AssetBundle LoadAssetBundleWithName(string name)
pb_AssetBundlePath assetBundlePath
Definition: pb_MetaData.cs:75
AssetType assetType
Definition: pb_MetaData.cs:83
static GameObject LoadPrefabWithId(string fileId)
static IEnumerable< T > LoadAll< T >()
static GameObject LoadPrefabWithMetadata(pb_MetaData metadata)
AssetType
Definition: pb_Enum.cs:69
UnityEngine.Object Object