using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.IO; public class DownLoadAssetBundle : MonoBehaviour { private string mainAssetBundleURL = @"http://www.XXX.com/AssetBundles/AssetBundles"; private string allAssetBundleURL = @"http://www.XXX.com/AssetBundles/"; void Start () { StartCoroutine("DownLoadMainAssetBundel"); } IEnumerator DownLoadMainAssetBundel()
{
UnityWebRequest request = UnityWebRequest.GetAssetBundle(mainAssetBundleURL);
yield return request.Send();
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
//Debug.Log("OK");
AssetBundleManifest manifest = ab.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
string[] names = manifest.GetAllAssetBundles();
for (int i = ; i < names.Length; i++)
{
Debug.Log(allAssetBundleURL + names[i]);
StartCoroutine(DownLoadSingleAssetBundel(allAssetBundleURL + names[i]));
}
}
/// <summary>
/// 下载单个AB文件
/// </summary>
IEnumerator DownLoadSingleAssetBundel(string url)
{
UnityWebRequest request = UnityWebRequest.GetAssetBundle(url);
yield return request.Send();
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
//Debug.Log("OK"); string[] names = ab.GetAllAssetNames();
for (int i = ; i < names.Length; i++)
{ string tempName = Path.GetFileNameWithoutExtension(names[i]);
//Debug.Log(tempName); GameObject gameObject = ab.LoadAsset<GameObject>(tempName);
GameObject.Instantiate<GameObject>(gameObject); } } }

下面是下载存储一条龙

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.IO; public class DownLoadAssetBundle : MonoBehaviour { private string mainAssetBundleURL = @"http://www.XXX.com/AssetBundles/AssetBundles"; private string allAssetBundleURL = @"http://www.XXX.com/AssetBundles/"; void Start () { StartCoroutine("DownLoadMainAssetBundel"); } IEnumerator DownLoadMainAssetBundel()
{
UnityWebRequest request = UnityWebRequest.GetAssetBundle(mainAssetBundleURL);
yield return request.Send();
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
//Debug.Log("OK");
AssetBundleManifest manifest = ab.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
string[] names = manifest.GetAllAssetBundles();
for (int i = ; i < names.Length; i++)
{
Debug.Log(allAssetBundleURL + names[i]);
//StartCoroutine(DownLoadSingleAssetBundel(allAssetBundleURL + names[i]));
StartCoroutine(DownLoadAssetBundelAbdSave(allAssetBundleURL + names[i]));
}
}
/// <summary>
/// 下载单个AB文件不保存
/// </summary>
IEnumerator DownLoadSingleAssetBundel(string url)
{
UnityWebRequest request = UnityWebRequest.GetAssetBundle(url);
yield return request.Send();
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
//Debug.Log("OK"); string[] names = ab.GetAllAssetNames();
for (int i = ; i < names.Length; i++)
{ string tempName = Path.GetFileNameWithoutExtension(names[i]);
//Debug.Log(tempName); GameObject gameObject = ab.LoadAsset<GameObject>(tempName);
GameObject.Instantiate<GameObject>(gameObject); } }
/// <summary>
/// 下载AB文件并保存到本地
/// </summary>
/// <returns></returns>
IEnumerator DownLoadAssetBundelAbdSave(string url)
{
WWW www = new WWW(url);
yield return www;
if(www.isDone)
{
//表示资源下载完毕使用IO技术把www对象存储到本地
SaveAssetBundle(Path.GetFileName(url), www.bytes, www.bytes.Length); }
}
/// <summary>
/// 存储AB文件到本地
/// </summary>
private void SaveAssetBundle(string fileName, byte[] bytes, int count)
{
FileInfo fileInfo = new FileInfo(Application.streamingAssetsPath + "//" + fileName);
FileStream fs = fileInfo.Create();
fs.Write(bytes, , count);
fs.Flush();
fs.Close();
fs.Dispose();
Debug.Log(fileName + "下载并存储完成");
} }

我的Unity学习笔记之——Unity中从网站下载ab资源+下载存储一条龙的更多相关文章

  1. Unity学习笔记(4) --- Unity的界面排版:初识GUI

    GUI和GUILayout是Unity提供的UIKit.在使用GUI的Controls时都要求设置Rect參数.没办法做到自己主动排版,给适配带来难度.而GUILayout的设计就是为了弥补这个缺陷, ...

  2. 并发编程学习笔记(4)----jdk5中提供的原子类及Lock使用及原理

    (1)jdk中原子类的使用: jdk5中提供了很多原子类,它会使变量的操作变成原子性的. 原子性:原子性指的是一个操作是不可中断的,即使是在多个线程一起操作的情况下,一个操作一旦开始,就不会被其他线程 ...

  3. [学习笔记] 在Eclipse中导入项目

    参考前文:[学习笔记] 在Eclips 中导出项目 选择已经导出的文件: 导入之后,项目结构如下: 至此,完成.

  4. CockroachDB学习笔记——[译]CockroachDB中的SQL:映射表中数据到键值存储

    CockroachDB学习笔记--[译]CockroachDB中的SQL:映射表中数据到键值存储 原文标题:SQL in CockroachDB: Mapping Table Data to Key- ...

  5. [学习笔记] 在Eclipse中导出可以直接运行的jar,依赖的jar中的类解压后放在运行jar中

    前文: [学习笔记] 在Eclipse中导出可以直接运行的jar,依赖的jar打在jar包中 使用7z打开压缩包,查看所有依赖的jar都被解压以包名及class的方式存储在了运行jar中,此时jar的 ...

  6. [学习笔记] 在Eclipse中导出可以直接运行的jar,依赖的jar打在jar包中

    本文需要参考前文: [学习笔记] 在Eclipse中导出可以直接运行的jar,依赖的jar在子目录中 上文是导出的运行的依赖jar被放在了子目录中,本文是将依赖jar放在可运行jar的本身,这样发布的 ...

  7. [学习笔记] 在Eclipse中导出可以直接运行的jar,依赖的jar在子目录中

    工程创建可参考前文: [学习笔记] 在Eclipse中使用Hibernate,并创建第一个工程,数据库为Oracle XE 在工程上鼠标右键: 找到java 选择 Runable JAR file N ...

  8. Web安全学习笔记 SQL注入中

    Web安全学习笔记 SQL注入中 繁枝插云欣 --ICML8 权限提升 数据库检测 绕过技巧 一.权限提升 1. UDF提权 UDF User Defined Function,用户自定义函数 是My ...

  9. (转) OpenCV学习笔记大集锦 与 图像视觉博客资源2之MIT斯坦福CMU

          首页 视界智尚 算法技术 每日技术 来打我呀 注册     OpenCV学习笔记大集锦 整理了我所了解的有关OpenCV的学习笔记.原理分析.使用例程等相关的博文.排序不分先后,随机整理的 ...

随机推荐

  1. linux下open-vswitch安装卸载操作

    一. ovs 从源码编译安装: 安装依赖项: ? 1 2 3 4 5 6 7 8 9 10 11 # apt install make # apt install gcc # apt install ...

  2. Java框架spring Boot学习笔记(九):一个简单的RESTful API

    RESTful API设计需求如下: User.java package com.springboot.test; public class User { private Long id; priva ...

  3. Java框架spring Boot学习笔记(七):@Configuration,@bean注解

    @Configuration作用在类上,相当于一个xml文件 @bean作用于方法上,相当于xml配置中的<bean>标签 一个例子: 新建一个Springboot工程 新建一个User类 ...

  4. MongoDB基本语法

    建立连接 client = pymongo.MongoClient() 新建数据库 db = client["db_name"] 新建表 tble=db["table_n ...

  5. python环境搭建(linux)

    python安装 # wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz # yum install openssl-devel ...

  6. Intro to Airplane Physics in Unity 3D – 2017 and 2018

    Info:DescriptionHave you ever wanted to build your own Airplane Physics using the Rigidbody componen ...

  7. pythone函数基础(11)读,写,修改EXCEL

    #读EXCEL需要导入xlrd模块---在python控制台pip install xlrd模块import xlrdbook = xlrd.open_workbook('stu3.xls')shee ...

  8. ABAP开发规范

    一.数据库操作 1.禁止修改系统标准表. 2.如果使用到FOR ALL ENTRIES IN语句取数,一定要校验关联内表非空性. 3.禁止一条SELECT关联的表超过5张,需要多表取值的时候建议分开取 ...

  9. BIF

    list()把一个可迭代对象转化为列表 tuple()把一个可迭代对象转化为元祖 str()把参数对象转化为字符串 len()返回参数的长度 max()返回序列或者参数集合中的最大值 min()返回序 ...

  10. vs2013 v8编译

    最新v8,只能在vs2015编译(在官网看了资料,新版本v8/chrome使用的c++11特性只能用vs2015编译) vs2015 vc需要的dll有近50个,发布不太方便,所以采用vs2013up ...