项目记录09-- BaseLoader加载AssetBundle

资源使用自己的规则打包成AssetBunlde之后肯定就是要读取,使用官方的例子修改就好,免得做重复性工作毕竟自己做,偷下懒。官方的例子已经做好了资源的关联。

        EditorUtility.DisplayDialog("打包规则","1.开发开始将资源放倒Resoucrc下,打包将资源移开。\n2.需要打包的资源都放BundleResTemp下,打包规则超过如果3层就使用文件夹名字统一打包。默认:Prefabs 放预设(包括mode预设),TPack 放使用TexturePack生成文件,UI放UI资源文件,Audios音效.\n3.注意文件名字不能重复,比如(1.png和1.txt就不行)。","OK");

/**************************************************


 * 创建人   :XingHua


 * 创建时间 :2015-12-14


 * 模块名字 :BaseLoader


 * 基本功能 :资源下载


************************************************/


using UnityEngine;
using System.Collections;
using System.Runtime.Serialization;
#if UNITY_EDITOR
using UnityEditor;
#endif


//携带参数
public class LoaderParameter {
    /// <summary>
    /// //主要是使用在图集加载 
    /// </summary>
    public BaseLoader.LoadAssetCallBack call;


    public System.Object parm;
    public LoaderParameter(System.Object tobj)
    {
        parm = tobj;
    }
    public LoaderParameter(System.Object tobj, BaseLoader.LoadAssetCallBack tcall)
    {
        parm = tobj;
        call = tcall;
    }
}


public class BaseLoader :AllSceneSingle<BaseLoader>  {  


private static string kAssetBundlesPath = PlatformUtil.wwwStreamingAssetsPath + "/" + PlatformUtil.GetPlatformName() + "/";
private static string assetTila = ".assetbundle";


//下载prefab回调
public delegate void LoadAssetCallBack(UnityEngine.Object obj,LoaderParameter parm);




    public void LoadAsset<T>(string assetBundleName, string assetName, LoadAssetCallBack call)
{
        LoadAsset<T>(assetBundleName, assetName, call, null);
}


    /// <summary>
    /// 下载Asset
    /// </summary>
    /// <param name="assetBundleName">打包assetBundle的名字</param>
    /// <param name="assetName">asset的真实名字</param>
    /// <param name="call">回调</param>
    /// <param name="parm">携带参数</param>
public void LoadAsset<T>(string assetBundleName , string assetName, LoadAssetCallBack call ,LoaderParameter parm)
{
assetBundleName = assetBundleName + assetTila;
   StartCoroutine (PLoadAsset <T>(assetBundleName,assetName,call,parm));
}


    protected IEnumerator PLoadAsset<T>(string assetBundleName, string assetName, LoadAssetCallBack call, LoaderParameter parm)
{
yield return StartCoroutine(Initialize() );
        yield return StartCoroutine(Load<T>(assetBundleName, assetName, call, parm));
//AssetBundleManager.Instance.UnloadAssetBundle(assetBundleName);  //如果在这里卸载后面同一个包里面的加载就会出错



// Initialize the downloading url and AssetBundleManifest object.
protected IEnumerator Initialize()
{
// Don't destroy the game object as we base on it to run the loading script.
//DontDestroyOnLoad(gameObject);

#if UNITY_EDITOR
Debug.Log ("We are " + ( AssetBundleManager.Instance.SimulateAssetBundleInEditor ? "in Editor simulation mode" : "in normal mode") );
#endif


string platformFolderForAssetBundles = PlatformUtil.GetPlatformName ();




// Set base downloading url.
//string relativePath = GetRelativePath();
        //AssetBundleManager.BaseDownloadingURL = relativePath + kAssetBundlesPath + platformFolderForAssetBundles + "/";


AssetBundleManager.Instance.BaseDownloadingURL = kAssetBundlesPath ;


// Initialize AssetBundleManifest which loads the AssetBundleManifest object.
var request =  AssetBundleManager.Instance.Initialize(platformFolderForAssetBundles);
if (request != null)
yield return StartCoroutine(request);
}


public string GetRelativePath()
{
if (Application.isEditor)
return "file://" +  System.Environment.CurrentDirectory.Replace("\\", "/"); // Use the build output folder directly.
else if (Application.isWebPlayer)
return System.IO.Path.GetDirectoryName(Application.absoluteURL).Replace("\\", "/")+ "/StreamingAssets";
else if (Application.isMobilePlatform || Application.isConsolePlatform)
return Application.streamingAssetsPath;
else // For standalone player.
return "file://" +  Application.streamingAssetsPath;
}




protected IEnumerator Load <T>(string assetBundleName , string assetName, LoadAssetCallBack call,LoaderParameter parm)
{
Debug.Log("Start to load " + assetName + " at frame " + Time.frameCount);
// Load asset from assetBundle.
        AssetBundleLoadAssetOperation request = AssetBundleManager.Instance.LoadAssetAsync(assetBundleName, assetName, typeof(T));
if (request == null)
yield break;
yield return StartCoroutine(request);
        
        UnityEngine.Object prefab = request.GetAsset<UnityEngine.Object>();
        Debug.Log(assetName + (prefab == null ? " isn't" : " is") + " loaded successfully at frame " + Time.frameCount);


        if (prefab != null && call != null)
        {
            call(prefab, parm);
        }
}

}


//测试

BaseLoader.Instance.LoadAsset<Texture2D>("giant", "giant", call);

成功。。。。。

后面再考虑管理下。

你可能感兴趣的:(项目记录09-- BaseLoader加载AssetBundle)