接着前面的文章,我们打好包后还得需要读取资源文件,那我们就得需要一个资源读取管理器,因为对AssetBundle不是特别懂,我先使用下面的代码进行资源在内存中的管理
public class AssetLoadManager : MonoBehaviour
{
private static AssetLoadManager mInstance = null;
public static AssetLoadManager GetInstance()
{
if (null == mInstance) {
GameObject obj = new GameObject ();
mInstance = obj.AddComponent<AssetLoadManager> ();
}
return mInstance;
}
private AssetBundleManifest manifest = null;
private Dictionary<string,AssetBundle> abDict = new Dictionary<string, AssetBundle>();
public AssetBundle LoadAb(string abPath)
{
if (abDict.ContainsKey (abPath)) {
return abDict [abPath];
}
if (null == manifest) {
string assetBundlePath = AssetBundleConfig.AssetBundle_PATH + AssetBundleConfig.ASSET_FILENAME;
AssetBundle manifestBundle = AssetBundle.LoadFromFile (assetBundlePath);
if (null != manifestBundle) {
manifest = (AssetBundleManifest)manifestBundle.LoadAsset ("AssetBundleManifest");
}
}
if (null != manifest) {
string[] cubeDepends = manifest.GetAllDependencies (abPath);
for (int i = 0; i < cubeDepends.Length; ++i) {
LoadAb (cubeDepends [i]);
}
}
abDict [abPath] = AssetBundle.LoadFromFile (AssetBundleConfig.AssetBundle_PATH + abPath);
return abDict [abPath];
}
public Object LoadPrefab(string abName)
{
string abPath = abName + AssetBundleConfig.SUFFIX;
int index = abName.LastIndexOf ("/");
if (index == -1)
index = abName.Length;
string realPath = abName.Substring (0, index-1);
string realName = abName.Substring (index + 1, abName.Length - index - 1);
//Debug.Log ("Begin" +Time.unscaledTime);
LoadAb (abPath);
//Debug.Log ("End" +Time.unscaledTime);
if (abDict.ContainsKey (abPath) && abDict [abPath] != null) {
return abDict [abPath].LoadAsset (realName);
}
return null;
}
}还有很多不足的地方,我接下来会对AssetBundle是否需要缓存进行验证。
本文介绍了一个Unity中的资源加载管理器类AssetLoadManager,该类负责加载AssetBundles及其依赖,并在内存中管理这些资源,以实现高效的游戏资源管理。
1529

被折叠的 条评论
为什么被折叠?



