加载Assetbundle

Posted 福建小屁孩

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了加载Assetbundle相关的知识,希望对你有一定的参考价值。

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

/// <summary>
/// 脚本位置:需要加载物体的场景中任意物体上
/// 脚本功能:加载场景
/// </summary>
public class LoadAssetBundle
{
private bool m_IsLoadInDoor = false;
private bool m_IsLoadOutDoor = false;
public string path = "";

private GameObject Quad;

private bool m_IsLoadingIndoor = false;
private bool m_IsLoadingOutdoor = false;
private void Awake()
{
Quad = GameObject.Find("Quad");
//清空缓存
//Caching.CleanCache();
}
/// <summary>
/// 加载室内场景
/// </summary>
public void LoadIndoor()
{
if (!m_IsLoadInDoor)
{
m_IsLoadingIndoor = true;
StartCoroutine(DownloadScene(path + "/Indoor.unity3d"));
}
else
SceneManager.LoadSceneAsync("Indoor", LoadSceneMode.Additive);
m_IsLoadInDoor = true;
SaveData.GetInstance().gameData.ScenceEnum = 1;
Quad.SetActive(true);
}
/// <summary>
/// 加载室外场景
/// </summary>
public void LoadOutdoor()
{
if (!m_IsLoadOutDoor)
{
m_IsLoadingOutdoor = true;
StartCoroutine(DownloadScene(path + "/Outdoor.unity3d"));
}
else
SceneManager.LoadSceneAsync("Outdoor", LoadSceneMode.Additive);
m_IsLoadOutDoor = true;
SaveData.GetInstance().gameData.ScenceEnum = 2;
Quad.SetActive(false);
}
public void LoadMainScene()
{
StartCoroutine(DownloadScene(path + "/MainScene.unity3d"));
}
public void UnloadScene(string name)
{
Debug.Log("UnloadScene:" + name);
try
{
SceneManager.UnloadSceneAsync(name);
SaveData.GetInstance().gameData.ScenceEnum = 0;
Quad.SetActive(true);
}
catch (System.Exception e)
{
Debug.Log(e.Message);
}
}

IEnumerator DownloadScene(string url)
{
Debug.Log("开始下载场景" + url);
WWW bundlewww = WWW.LoadFromCacheOrDownload(new System.Uri(url).AbsoluteUri, 1);
yield return bundlewww;
Debug.Log("下载资源成功");
AssetBundle assetbundle = bundlewww.assetBundle;
if (assetbundle.isStreamedSceneAssetBundle)
{
Debug.Log("该资源是场景资源");
string[] scenePaths = assetbundle.GetAllScenePaths();
string sceneName = System.IO.Path.GetFileNameWithoutExtension(scenePaths[0]);
Debug.Log("获取场景名字:" + sceneName);
if ((sceneName == "Indoor" && SaveData.GetInstance().gameData.ScenceEnum == 1) || (sceneName == "Outdoor" && SaveData.GetInstance().gameData.ScenceEnum == 2))
{
SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
if (sceneName == "Indoor")
m_IsLoadInDoor = false;
else
m_IsLoadingOutdoor = false;
}
Debug.Log("加载场景");
}
}

public void LoadInternalResources(string name, Callback<Object> callback)
{
if (name != "")
StartCoroutine(LoadInternalGameObject(path + "/internalresources.data", name, callback));
}

public void LoadUIResources(string name, ObjectType type, Callback<Object, ObjectType> callback)
{
if (name != "")
StartCoroutine(LoadUIGameObject(path + "/ui.data", name, type, callback));
}

private IEnumerator LoadInternalGameObject(string path, string name, Callback<Object> callback)
{
//WWW www = WWW.LoadFromCacheOrDownload(new System.Uri(path).AbsoluteUri, 6);
WWW www = new WWW(new System.Uri(path).AbsoluteUri);
yield return www;
AssetBundle bundle = www.assetBundle;
Debug.Log(name);
Object obj = bundle.LoadAsset(name, typeof(Object));
if (callback != null)
{
callback(obj);
}
bundle.Unload(false); //切记释放没用资源
}

private IEnumerator LoadUIGameObject(string path, string name, ObjectType type, Callback<Object, ObjectType> callback)
{
//WWW www = WWW.LoadFromCacheOrDownload(new System.Uri(path).AbsoluteUri, 0);
WWW www = new WWW(new System.Uri(path).AbsoluteUri);
yield return www;
AssetBundle bundle = www.assetBundle;
Object request = bundle.LoadAsset(name, typeof(Object));
if (callback != null)
{
callback(request, type);
}
bundle.Unload(false); //切记释放没用资源
}

//// 加载Manifest
//private void LoadAssetBundleManifest()
//{
// var bundle = AssetBundle.LoadFromFile(System.IO.Path.Combine(Application.streamingAssetsPath, "StreamingAssets"));
// AssetBundleManifest manifest = bundle.LoadAsset<AssetBundleManifest>("internalresources");
// // 压缩包释放掉
// bundle.Unload(false);
// bundle = null;
//}
}

以上是关于加载Assetbundle的主要内容,如果未能解决你的问题,请参考以下文章

Android 逆向类加载器 ClassLoader ( 类加载时机 | 隐式加载 | 显示加载 | 类加载步骤 | 装载 | 链接 | 初始化 )

Android 逆向类加载器 ClassLoader ( 启动类加载器 | 扩展类加载器 | 应用类加载器 | 类加载的双亲委托机制 )

类加载器

类加载机制

类加载器和双亲委派

jpa 设置懒加载怎么手动加载