using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
/// <summary>
/// SingletonMonoAuto<LoadObjectManager>可换成MonoBehaviour
/// 同步的优点:管理方便,资源准备好可以及时返回。缺点:没有异步快。
///异步的优点:速度快与主线程无关。缺点:调用比较麻烦,最好的做法是使用回调。
/// </summary>
public class LoadObjectManager : SingletonMonoAuto<LoadObjectManager>
{
/// <summary>
/// 用Resources.Load()来加载在项目Resource文件夹下的资源文件
/// 例 "Prefab/Bullet"
/// Resources文件夹内的资源只读,所以想要动态更新的资源不要放在这里。
/// 在Resources里面一般放Prefab,在打包的时候会自动过滤掉不需要的资源
/// </summary>
/// <param name="prefabPath">prefab的路径</param>
/// <param name="root">创建的新对象的父级对象</param>
/// <returns></returns>
public T LoadSolution1<T>(string prefabPath, Transform root) where T : UnityEngine.Object
{
T prefabObject = Resources.Load<T>(prefabPath);
T obj = Instantiate(prefabObject, root);
return obj;
}
/// <summary>
/// 从本地文件夹中使用AssetBundle同步加载(无需用协程), 只允许加载当前设备允许访问的路径
/// 例 "AssetBundles/Bullet.unity3d"
/// </summary>
/// <param name="resourcePath">资源文件在项目的路径</param>
/// <param name="resourceName">资源文件的名字</param>
/// <param name="root">创建的新对象的父级对象</param>
/// <returns></returns>
public T LoadSolution2<T>(string resourcePath, string resourceName, Transform root) where T : UnityEngine.Object
{
AssetBundle ab = AssetBundle.LoadFromFile(resourcePath);
T prefabObject = ab.LoadAsset<T>(resourceName);
T obj = GameObject.Instantiate(prefabObject, root);
return obj;
}
/// <summary>
/// 异步从本地或服务器使用WWW下载、加载到项目中,并从asset
/// bundle中提取其中一个资源对象实例一个新的对象。
/// 例 @"http://localhost/AssetBundles/Bullet.unity3d"
/// 或 @"file:/E:AssetBundleProject\AssetBundleProject\Bullet"
/// 注:此方法在2019版本中已弃用
/// </summary>
/// <param name="resourceUrl">资源在本地或者服务器的路径</param>
/// <param name="prefabName">资源的名字</param>
/// <returns></returns>
IEnumerator LoadSolution3<T>(string resourceUrl, string prefabName) where T : UnityEngine.Object
{
while (Caching.ready == false)
{
yield return null;
}
WWW www = WWW.LoadFromCacheOrDownload(resourceUrl, 1);
//yield return www;
if (!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
yield break;
}
T prefabObject = www.assetBundle.LoadAsset<T>(prefabName);
T obj = Instantiate(prefabObject);
yield return obj;
}
/// <summary>
/// 从本地异步加载资源,增加回调
/// </summary>
/// <param name="resourcePath">资源在本地的路径</param>
/// <param name="prefabName">资源的名字</param>
/// <returns></returns>
IEnumerable LoadSolution4<T>(string resourcePath, string prefabName) where T : UnityEngine.Object
{
AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(resourcePath);
yield return request;
AssetBundle ab = request.assetBundle;
T prefabObject = ab.LoadAsset<T>(prefabName);
T obj = Instantiate(prefabObject) as T;
yield return obj;
}
/// <summary>
/// 用UnityWebRequest访问服务器获得assetbundle
/// </summary>
/// <param name="resourceUrl"></param>
/// <param name="prefabName"></param>
/// <returns></returns>
IEnumerable LoadSolution5<T> (string resourceUrl, string prefabName) where T : UnityEngine.Object
{
UnityWebRequest webRequest = UnityWebRequestAssetBundle.GetAssetBundle(resourceUrl);
yield return webRequest.SendWebRequest();
if (webRequest.isHttpError || webRequest.isNetworkError)
Debug.Log(webRequest.error);
else
{
Debug.Log(webRequest.downloadHandler.text);
}
//AssetBundle ab = (webRequest.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(webRequest);
T prefabObject = ab.LoadAsset<T>(prefabName);
T obj = Instantiate(prefabObject) as T;
yield return obj;
}
}