码迷,mamicode.com
首页 > 其他好文 > 详细

加载Assetbundle

时间:2018-01-24 11:01:20      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:压缩   exce   blog   sage   ati   game   []   gas   www.   

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

标签:压缩   exce   blog   sage   ati   game   []   gas   www.   

原文地址:https://www.cnblogs.com/fujianxiaopihai/p/8340131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!