加载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;
//}
}

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

时间: 2024-10-28 21:21:07

加载Assetbundle的相关文章

Unity3d 同步加载 Assetbundle 方法 AssetBundle.CreateFromFile

今天在群里问朋友有没有 同步加载 Assetbundle 的方法,同事给我指点 Unity3d 不知道在哪一个版本新增了一个 AssetBundle.CreateFromFile 的接口,这个接口是同步读取Assetbundle的.于是到帮助文档查询相关资料. http://docs.unity3d.com/ScriptReference/AssetBundle.CreateFromFile.html 文章转自http://blog.csdn.net/huutu 星环游戏 http://www.

加载AssetBundle方法

先介绍一种常用的加载AssetBundle方法 using UnityEngine; using System.Collections; using System.IO; public class LoadUnity3d : MonoBehaviour { // Use this for initialization void Start() { StartCoroutine(LoadScene()); } // Update is called once per frame void Upda

加载 AssetBundle 的四种方法

[加载 AssetBundle 的四种方法] 1.AssetBundle.LoadFromMemoryAsync(byte[] binary, uint crc = 0); 返回AssetBundleCreateRequest.Use assetBundle property to get an AssetBundle once it is loaded. Compared to LoadFromMemory, this version will perform AssetBundle deco

unity 加载Assetbundle文件夹路径需要注意

测试时 在Editor下加载Assetbundle文件夹下路径成功 在Android下使用www一直失败 使用Assetbundle.loadfromfile可以 发现:使用AssetBundle.LoadFromFile的路径是path,那么使用www加载需要前缀 "File://"+path才可以 ( Application.persistentDataPath;) 加载StreamAssets路径下同理,  www需要加前缀 "jar:File://";   

Unity中Android加载assetbundle需要注意的两点问题

1.打包需要用Android的路径打包 using UnityEngine;using System.Collections;using UnityEditor; public class Test : Editor{ [MenuItem("Custom Editor/Create AssetBunldes Main")] static void CreateAssetBunldesMain() { //获取在Project视图中选择的所有游戏对象 Object[] SelectedA

OSS加载AssetBundle的坑

http协议有被盗链的风险,万一被恶意刷流量就惨了 最近一直在想如何安全的下载AssetBundle webgl加载那么慢 ab包放在oss是个不错的选择 所以就将AssetBundle加载的办法挨个试 debug的时候发现 oss传过来的流can.seek属性为false ab包通过流的方式加载 要求 canseek属性必须为true 只能用其他方法了 明天再搞 Memory 方式 =-= 原文地址:https://www.cnblogs.com/pz904/p/11955021.html

加载Assetbundle需要注意的地方

WWW:异步实现,手机上不能用于同步代码,需要监测其完成状态.不用www.dispose. CreateFromFile:阻塞,但是移动平台上面的路径格式有点坑,没时间看,不用. 以下两个方式需要先使用文件流读取到字节数组: CreateFromMemery:异步,注意返回值不是Assetbundle CreateFromMermeryImmediate:阻塞,返回值为Assetbundle

Unity5 AssetBundle系列——资源加载卸载以及AssetBundleManifest的使用

下面代码列出了对于assetbundle资源的常用操作,其中有针对bundle.asset.gameobject三种类型对象的操作,实际使用中尽量保证成对使用. 这一块的操作比较繁琐,但只要使用正确,是可以保证资源完全没有泄露的. using UnityEngine; using System.Collections; public class TestAssetBundle : MonoBehaviour { public string AssetBundleName = "cube1.ass

Unity3d热更新全书-资源加载(一)从AssetBundle说起

Unity3D动态下载资源,有没有解?有,AssetBundle就是通用解,任何一本书都会花大幅篇章来介绍AssetBundle. 我们也来说说AssetBundle 我们试全面的分析一下Unity3D提供的资源加载机制 1.Resources//内嵌资源,使用方法Resources.Load,可以加载任意种类的资源,不能动态修改,卒. 2.StreamingAssets//随包资源,使用方法IO或WWW.Load.WWW.Load可以加载任意种类资源,IO仅限bytes 和 text. 3.W