using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.UI;
public class LoadAssetsBundles : MonoBehaviour {
string path1;
string path2 ;
string path3;
Texture texture;
void Start()
{
//需要在这里赋值
path1 = "Assets/AssetsBundles/object.unity3d";//包内容一个Cube和Sphere预设 包名+后缀
path2 = "Assets/AssetsBundles/material.unity3d";//包内容一个材质球
path3 = "Assets/AssetsBundles/texture.assetsbundle";//包内容一张名为timg的图片
StartCoroutine(StartAB());
}
// Use this for initialization
IEnumerator StartAB () {
//第一种方式从内存中加载AB包
#region 异步加载
//异步加载
AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path1));
yield return request;
//加载共同依赖资源,如贴图 材质
AssetBundleCreateRequest request2 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path2));
yield return request2;
AssetBundleCreateRequest request3 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path3));
yield return request3;
AssetBundle ab = request.assetBundle;//获取包
AssetBundle ab2 = request2.assetBundle;//获取包
AssetBundle ab3 = request3.assetBundle;
//使用里面的资源
GameObject cube = ab.LoadAsset<GameObject>("Cube");
GameObject sphere = ab.LoadAsset<GameObject>("Sphere");
texture = ab3.LoadAsset<Texture>("timg");//获取图片
Debug.Log(texture.width + texture.height+texture.name);
//场景中新建一个RawImage
GameObject.Find("Canvas/RawImage").GetComponent<RawImage>().texture = texture;
Instantiate(cube);
Instantiate(sphere);
#endregion
#region 同步加载
#endregion
StopAllCoroutines();
}
// Update is called once per frame
void Update () {
}
}using System.Collections;
using System.Co