AssetBundle加载的三种方式(脚本挂载直接可以使用。复习使用。商业有待验证)
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
public enum LoadType
{
None = -1,
WebRequest = 0,
Memroy = 1,
File = 2
}
public enum MemoryType
{
Normal = 1,
Async = 2
}
public enum FileType
{
Normal = 1,
Async = 2
}
public class BundleLoader : MonoBehaviour
{
[Header("路径不需要填写Asset")]
public string url;
[Header("选择加载方式")]
public LoadType loadType = LoadType.WebRequest;
[Header("内存加载方式(同步Or异步)")]
public MemoryType memoryType = MemoryType.Normal;
[Header("从本地文件加载方式(同步Or异步)")]
public FileType fileType = FileType.Normal;
private void Start()
{
switch (loadType)
{
case LoadType.WebRequest:
StartCoroutine(LoadAssetsByWebRequest());
break;
case LoadType.Memroy:
LoadAssetsByMemory(memoryType);
break;
case LoadType.File:
LoadAssetsByFile(fileType);
break;
default:
Debug.LogError("Nothing To Do");
break;
}
}
private IEnumerator LoadAssetsByWebRequest()
{
Debug.LogError("从Web加载AssetBundle包");
UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(Application.dataPath + "/" + url);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.LogError(www.error);
}
else
{
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);
InstGameObject(bundle);
}
}
#region 从内存加载AssetBundle包
private void LoadAssetsByMemory(MemoryType memoryType)
{
Debug.LogError("从内存加载AssetBundle包");
switch (memoryType)
{
case MemoryType.Normal:
LoadAssetsByMemroyNormal();
break;
case MemoryType.Async:
StartCoroutine(LoadAssetsByMemoryAsync());
break;
default:
Debug.LogError("请选择同步或者异步");
break;
}
}
private void LoadAssetsByMemroyNormal()
{
Debug.LogError("从内存加载AssetBundle包------>普通方式");
byte[] bin = File.ReadAllBytes(Application.dataPath + "/" + url);
AssetBundle bundle = AssetBundle.LoadFromMemory(bin);
InstGameObject(bundle);
}
private IEnumerator LoadAssetsByMemoryAsync()
{
Debug.LogError("从内存加载AssetBundle包------>异步方式");
byte[] bin = File.ReadAllBytes(Application.dataPath + "/" + url);
AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(bin);
yield return request;
if (request.isDone)
{
AssetBundle bundle = request.assetBundle;
InstGameObject(bundle);
}
}
#endregion
#region 从本地文件加载包
private void LoadAssetsByFile(FileType fileType)
{
Debug.LogError("从本地文件加载AssetBundle包");
switch (fileType)
{
case FileType.Normal:
LoadAssetByFileNormal();
break;
case FileType.Async:
StartCoroutine(LoadAssetsByFileAsync());
break;
default:
Debug.LogError("请选择同步或者异步");
break;
}
}
private void LoadAssetByFileNormal()
{
Debug.LogError("从本地文件加载AssetBundle包------>普通方式");
AssetBundle bundle = AssetBundle.LoadFromFile(Application.dataPath + "/" + url);
InstGameObject(bundle);
}
private IEnumerator LoadAssetsByFileAsync()
{
Debug.LogError("从本地文件加载AssetBundle包------>异步方式");
AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(Application.dataPath + "/" + url);
yield return request;
if (request.isDone)
{
AssetBundle bundle = request.assetBundle;
InstGameObject(bundle);
}
}
#endregion
private void InstGameObject(AssetBundle bundle)
{
GameObject go = bundle.LoadAsset<GameObject>("Player001");
Instantiate(go);
bundle.Unload(false);
}
}
|