加载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的更多相关文章
- 加载AssetBundle方法
先介绍一种常用的加载AssetBundle方法 using UnityEngine; using System.Collections; using System.IO; public class L ...
- 打包加载 AssetBundle
1.先创建Asset序列化(单个文件夹所在文件夹路径,会遍历这个文件夹所有的Prefab,所有的Prefab名字不能重复,必须保证名字得唯一性),配置好ConfigAB表 /* ######### # ...
- 加载 AssetBundle 的四种方法
[加载 AssetBundle 的四种方法] 1.AssetBundle.LoadFromMemoryAsync(byte[] binary, uint crc = 0); 返回AssetBundle ...
- Unity加载AssetBundle的方法
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; usin ...
- Unity3D在移动平台下加载AssetBundle导致Shader效果不正确的问题
这个问题,主要还是在移动平台下开发导致的. 在编辑器里调试加载AB时会导致Shader效果不正确的原因,主要还是编辑器下加载以IOS或是ANDROID平台打包的AB它所使用的shader已经编译成对应 ...
- 加载Assetbundle需要注意的地方
WWW:异步实现,手机上不能用于同步代码,需要监测其完成状态.不用www.dispose. CreateFromFile:阻塞,但是移动平台上面的路径格式有点坑,没时间看,不用. 以下两个方式需要先使 ...
- Unity3d热更新全书-加载(一)从AssetBundle说起
Unity3D动态下载资源,有没有解?有,AssetBundle就是通用解,任何一本书都会花大幅篇章来介绍AssetBundle. 我们也来说说AssetBundle 我们试全面的分析一下Unity3 ...
- Unity3D使用Assetbundle打包加载(Prefab、场景)
之前有一篇文章中我们相惜讨论了Assetbundle的原理,如果对原理还不太了解的朋友可以看这一篇文章:Unity游戏开发使用Assetbundle加载场景的原理 本篇文章我们将说说assetbund ...
- Unity3d 5.x AssetBundle打包与加载
1.AssetBundle打包 unity 5.x版本AssetBundle打包,只需要设置好AssetBundle的名称后,unity会自动将其打包,无需处理其他,唯独需要做的是设置好个AssetB ...
- Unity5 AssetBundle系列——资源加载卸载以及AssetBundleManifest的使用
下面代码列出了对于assetbundle资源的常用操作,其中有针对bundle.asset.gameobject三种类型对象的操作,实际使用中尽量保证成对使用. 这一块的操作比较繁琐,但只要使用正确, ...
随机推荐
- awk 计算
[root@BJ-DATABASES lastday]# ps -aux| grep nginx|grep -v greproot 13783 0.0 0.0 56796 1208 ? Ss 11:1 ...
- 京准GPS北斗卫星时钟同步系统投运国电内蒙古晶阳能源有限公司
京准GPS北斗卫星时钟同步系统投运国电内蒙古晶阳能源有限公司 2020年1月初期,我京准科技生产研发的GPS北斗卫星时钟同步系统投运国电内蒙古晶阳能源有限公司,为该单位的能源管理系统及其他各业务子系 ...
- 一个好的程序应该像AK47
一个好的程序应该像AK47: 容易上手(配置.设置.功能描述清晰),结构简单(低耦合,模块化,单元化),拆装方便(安装部署.卸载.迁移很少有障碍),从不卡壳(已知业务冲突解决),故障率低(未知或可能出 ...
- java判断上传图片格式
由于客户上传图片将png的图片的后缀名改为jpg,所以通过后缀名判断不行,用下面这个方法可以 //判断是否是JPG格式 log.info("-1----进入JPG格式判断....." ...
- web服务器应答状态代码(status)及其含义
Web服务器响应浏览器或其他客户程序的请求时,其应答一般由以下几个部分组成:一个状态行,几个应答 头,一个空行,内容文档.下面是一个最简单的应答 : 状态行包含HTTP版本.状态代码.与状态代码对 ...
- 小白之Python-基础中的基础02
Python-基础中的基础02 继续整理笔记,反复练习!fighting! -----------------华丽的分界线-------------变量:第一次出现叫做定义变量,再次出现为为该变量赋值 ...
- c语言container_of 编译报错
求指针ptr所在的结构体实例的首地址, #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #define co ...
- js match方法
1.用法 match()方法可以字符串中检索指定的值,或者是匹配一个或多个正则表达式 2.返回值 该方法类似于indexOf()/lastIndexOf(),区别就是返回值不一样 indexOf()/ ...
- 51电子-STC89C51开发板:开发包
全部内容,请点击: 51电子-STC89C51开发板:<目录> --------------------------- 正文开始 --------------------------- ...
- Docker CLI docker build 常用命令
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows操作系统的机器上,也可以实现虚拟化.Docker是内核 ...