加载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三种类型对象的操作,实际使用中尽量保证成对使用. 这一块的操作比较繁琐,但只要使用正确, ...
随机推荐
- css - content-visibility
css - content-visibility content-visibility:实现可见网页只加载可见区域内容 介绍 content-visibility是一个css属性,它控制一个元素是否呈 ...
- kubernetes service服务发现两种方式
service服务发现ClusterIP方式 1.暴露deployment服务 kubectl expose deployment nginx4 --port=8000 --target-port=8 ...
- Linux系统Shell脚本第五章:shell数组、正则表达式及文件三剑客之AWK
目录 一.shell数组 1.数组分类 2.定义数组方法 二.正则表达式 1.元字符 2.表示次数 3.位置锚定 4.分组 5.扩展正则表达式 三.文本三剑客之AWK 1.awk 2.使用格式 3.处 ...
- react 二级路由嵌套
嵌套路由之后,静态文静路径错误, 更改webpack 打包output 输出根目录,publicPath:'/',二级路由刷新之后白屏,在首页模板文件中路径前加 /,
- 移动自动化-swipe、scroll、drag、高级手势等
swipe 滑动事件 使用方法:driver.swipe(strat_x,start_y,end_x,end_y) swipe通过driver使用 传入起始的位置x和y,和结束的位置x和y 时间参数, ...
- [localhost-startStop-1]
第一次遇到Tomcat在Linux服务器启动卡住的情况,情况很简单,tomcat启动以后卡在INFO: Deploying web application directory ......这句话,具体 ...
- 抽取JDBC工具类:JDBCUtils
目的:简化书写 分析: 驱动注册,连接对象创建,其中包括输入驱动,数据库的地址,以及用户名和密码,每次编写代码都需要重复编写,如果每次使用的都是同一个账户的同一个数据库,代码的重复读很高,甚至 ...
- vue相关组件用法
<el-checkbox v-model="checkbox.checkModel" :label="index":disabled="chec ...
- AXI 协议翻译介绍
一.介绍 Introduction 本章描述了axis协议的体系结构和协议定义的基本事务.它包含以下部分:•第1-2页关于AXI协议•第1-3页是架构•第1-7页是基本事务•第1-11页的附加功能. ...
- next_permutation / prev_permutation 用法
给定输入的序列 a(整数即可,其他无限制条件),next_permutation(a + 1, a + n + 1) 可以求出 a 的关于值的下一个排列,prev_permutation(a + 1, ...