using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class LoadResources : MonoBehaviour {
private void Start()
{
// StartCoroutine("LoadAbAsync", "AssetBundles/test");
// LoadAB("AssetBundles/test");
// StartCoroutine("ServerLoading", @"file:/G:UnitySotfware\test\AssetBundles");
GetAbManifest();
}
//加载方法,异步加载
IEnumerator LoadAbAsync(string AbPath)
{
if (File.Exists(AbPath))
{
//第一种加载AB的方式 ,从内存中加载 LoadFromMemory
AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(AbPath));
yield return request;
AssetBundle ab = request.assetBundle;
GameObject cube = ab.LoadAsset<GameObject>("Cube"); //参数写的是物体名字
Instantiate(cube);
}else
{
Debug.LogWarning("没有文件");
}
}
// 第二种同步加载
public void LoadAB(string AbPath)
{
if (File.Exists(AbPath))
{
AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(AbPath));
GameObject cube = ab.LoadAsset<GameObject>("Cube");
Instantiate(cube);
}else
{
Debug.LogWarning("没有文件");
}
}
//服务器或本地加载 本地:@"file:/G:UnitySotfware\test\AssetBundles\" 服务器: @"http://localhost/AssetBundles/test"
IEnumerator ServerLoading(string AbPath)
{
while (Caching.ready == false)
{
yield return null;
}
WWW www = WWW.LoadFromCacheOrDownload("G:UnitySotfware\\test\\AssetBundles", 1);
yield return www;
//是否报错
if (string.IsNullOrEmpty(www.error) == false)
{
Debug.Log(www.error); yield break;
}
AssetBundle ab = www.assetBundle;
GameObject Cube = ab.LoadAsset<GameObject>("Cube");
Instantiate(Cube);
}
public void GetAbManifest()
{
AssetBundle Ab = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");
AssetBundleManifest manifest = Ab.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
//得到全部资源包
//foreach (string name in manifest.GetAllAssetBundles())
// {
// Debug.Log(name);
// }
//得到资源所依赖的资源包并加载
string[] strs = manifest.GetAllDependencies("test");
foreach (string name in strs)
{
print(name);
AssetBundle.LoadFromFile("AssetBundles/" + name);
}
}
}
unity 资源加载
猜你喜欢
转载自blog.csdn.net/weixin_37744986/article/details/80565678
今日推荐
周排行