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的更多相关文章

  1. 加载AssetBundle方法

    先介绍一种常用的加载AssetBundle方法 using UnityEngine; using System.Collections; using System.IO; public class L ...

  2. 打包加载 AssetBundle

    1.先创建Asset序列化(单个文件夹所在文件夹路径,会遍历这个文件夹所有的Prefab,所有的Prefab名字不能重复,必须保证名字得唯一性),配置好ConfigAB表 /* ######### # ...

  3. 加载 AssetBundle 的四种方法

    [加载 AssetBundle 的四种方法] 1.AssetBundle.LoadFromMemoryAsync(byte[] binary, uint crc = 0); 返回AssetBundle ...

  4. Unity加载AssetBundle的方法

    using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; usin ...

  5. Unity3D在移动平台下加载AssetBundle导致Shader效果不正确的问题

    这个问题,主要还是在移动平台下开发导致的. 在编辑器里调试加载AB时会导致Shader效果不正确的原因,主要还是编辑器下加载以IOS或是ANDROID平台打包的AB它所使用的shader已经编译成对应 ...

  6. 加载Assetbundle需要注意的地方

    WWW:异步实现,手机上不能用于同步代码,需要监测其完成状态.不用www.dispose. CreateFromFile:阻塞,但是移动平台上面的路径格式有点坑,没时间看,不用. 以下两个方式需要先使 ...

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

    Unity3D动态下载资源,有没有解?有,AssetBundle就是通用解,任何一本书都会花大幅篇章来介绍AssetBundle. 我们也来说说AssetBundle 我们试全面的分析一下Unity3 ...

  8. Unity3D使用Assetbundle打包加载(Prefab、场景)

    之前有一篇文章中我们相惜讨论了Assetbundle的原理,如果对原理还不太了解的朋友可以看这一篇文章:Unity游戏开发使用Assetbundle加载场景的原理 本篇文章我们将说说assetbund ...

  9. Unity3d 5.x AssetBundle打包与加载

    1.AssetBundle打包 unity 5.x版本AssetBundle打包,只需要设置好AssetBundle的名称后,unity会自动将其打包,无需处理其他,唯独需要做的是设置好个AssetB ...

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

    下面代码列出了对于assetbundle资源的常用操作,其中有针对bundle.asset.gameobject三种类型对象的操作,实际使用中尽量保证成对使用. 这一块的操作比较繁琐,但只要使用正确, ...

随机推荐

  1. Linux系统Shell脚本第五章:shell数组、正则表达式及文件三剑客之AWK

    目录 一.shell数组 1.数组分类 2.定义数组方法 二.正则表达式 1.元字符 2.表示次数 3.位置锚定 4.分组 5.扩展正则表达式 三.文本三剑客之AWK 1.awk 2.使用格式 3.处 ...

  2. “初始化 Java 工具”期间发生了内部错误, java.lang.NullPointerException

    今天刚打开eclipse就报了这个错误,我怀疑是昨晚想关电脑的时候,关闭eclipse太快,没有等待工作空间保存就关了电脑的缘故 错误如图: (图片来自下方链接博客,因为忘记截图了) 我百度后按照提示 ...

  3. c# Winform 缓动动画

    一.定义缓动动画类public class AnimationHelper { Timer animationTimer = new Timer(); double velocity = 0.0; P ...

  4. maven :Cannot start maven service

    问题报错信息:maven启动失败 java.lang.RuntimeException: Cannot start maven service at org.jetbrains.idea.maven. ...

  5. gopher必读文章

    Getting Started with Go Programminghttps://www.programiz.com/golang/getting-startedHow to Write Go C ...

  6. Kubernetes-Pod进阶

    目录: 资源限制 CPU资源单位 内存资源单位 重启策略 健康检查/探针 探针的三种规则 Probe支持三种检查方法 总结 Pod 进阶 资源限制 当定义 Pod 时可以选择性地为每个容器设定所需要的 ...

  7. [部署日记]Android Studio在安装完后,sdk依旧提示SDK emulator directory is missing

    问题起源: 今天在闲着没事搞了个go的rest-api,用postman测试了一下可行,于是一拍大腿决定写一个安卓手机程序,于是一拍大腿重新下载了卸载没多久因为没空间放原神的Android Studi ...

  8. git submodule .gitmodules 子模块

    初始化 git submodule init 添加子项目 git submodule add -b xxxx {path} -b 指定分支 path 可为空 (不知为何, 直接写在 .gitmodul ...

  9. yolov5s yolov8n 在自己数据集上测试比较(640*640)

    yolov5s -> 0.5map:  96.5 -> ncnn:75ms yolov8n -> 0.5map:  94.1 -> ncnn:52ms

  10. 性能再升级,XMeter Cloud 专业版正式上线

    去年 8 月,EMQ 正式在全球市场推出了最简单.快速的 MQTT 负载和场景测试云服务--XMeter Cloud,用户无需部署即可进行 MQTT 测试,以更低的测试成本轻松打造具有竞争力的可靠物联 ...