AssetBundle创建,加载,5.1版本

using UnityEngine;
using System.Collections;
using UnityEditor;


//Assetbundle 创建
public class AssetBundleControl{
    
    //逐个打包   //不常用
    [MenuItem("Custom Editor/Create AssetBundles Main")]
    static void CreateAssetBundlesMain()
    {
        //获取选取的对象
        Object[] selectedAssets = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
        
        //遍历对象
        foreach(var it in selectedAssets)
        {
            string sourcePath = AssetDatabase.GetAssetPath(it);


            //本地测试:建议最后将Assetbundle放在StreamingAssets文件夹下,如果没有就创建一个,因为移动平台下只能读取这个路径
            //StreamingAssets是只读路径,不能写入
            //服务器下载:就不需要放在这里,服务器上客户端用www类进行下载。


            string targetPath = Application.dataPath + "/StreamingAssets/" + it.name + ".assetbundle";
            if(BuildPipeline.BuildAssetBundle(it,null,targetPath,BuildAssetBundleOptions.CollectDependencies))
            {
                Debug.Log(it.name+"资源打包成功");
            }
            else
            {
                Debug.Log(it.name+"资源打包失败");
            }
        }
        //刷新编辑器
        AssetDatabase.Refresh();
    }


    //打包在一起
    [MenuItem("Custom Editor/Create AssetBundles All")]
    static void CreateAssetBundlesAll()
    {
        Caching.CleanCache();   //清空缓存


        string path = Application.dataPath + "/StreamingAssets/All.assetbundle";


        Object[] selectedAssets = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
        foreach (var it in selectedAssets)
        {
            //Debug.Log("create assetbundle name:" + it);
        }


        if (BuildPipeline.BuildAssetBundle(null, selectedAssets, path, BuildAssetBundleOptions.CollectDependencies))
        {
            AssetDatabase.Refresh();
            Debug.Log("打包所有");
        }
        else
        {


        }
    }
    
}





using UnityEngine;

using System.Collections;
using System.Collections.Generic;


//Assetbundle 生成资源
public class AssetBundle_LoadAsset:MonoBehaviour  {
    private static AssetBundle_LoadAsset instance;
    public static AssetBundle_LoadAsset Instance
    {
        get
        {
            if(instance==null)
            {
                Debug.Log("this must be added on the object");
 


                return null;
            }
            return instance;
        }
    }
    private AssetBundle_LoadAsset()
    {
        instance = this;
    }


    private GameObject preObj;   //还未生成的预制体
    private List<GameObject> preObjs; 


    //不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
    public static readonly string PathURL =
#if UNITY_ANDROID
"jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
Application.dataPath + "/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
    "file://" + Application.dataPath + "/StreamingAssets/";
#else
        string.Empty;
#endif


    void OnGUI()
    {
        //if(GUILayout.Button("单个"))
        //{
        //    LoadMainObject("test1.assetbundle");
        //}
        //if(GUILayout.Button("多资源的单个资源"))
        //{
        //    LoadTogetherObject_One("All.assetbundle", "test2");
        //}
        //if (GUILayout.Button("多资源的所有资源"))
        //{
        //    LoadTogetherObject_All("All.assetbundle");
        //}
    }


    //返回一个为生成的物体  包含一个资源的Assetbundle
    public GameObject LoadMainObject(string assetName)
    {
        preObj = null;
        StartCoroutine(LoadMainObject_(PathURL+assetName));
        return preObj;
    }


    //返回生成所有的资源   包含多个资源的Assetbundle
    public List<GameObject> LoadTogetherObject_All(string path)
    {
        if(preObjs!=null)
            foreach(var it in preObjs)        
               Destroy(it);        
        preObjs = null;
        preObjs = new List<GameObject>();
        StartCoroutine(LoadTogetherObject_All_(PathURL+path));
        return preObjs;        
    }


    //返回一个指定生成的物体  包含多个资源的Assetbundle
    public GameObject LoadTogetherObject_One(string path,string assetName)
    {
        preObj = null;
        StartCoroutine(LoadTogetherObject_One_(PathURL+path, assetName));
        return preObj;
    }


    private IEnumerator LoadMainObject_(string path)
    {
        WWW bundle = new WWW(path);


        yield return bundle;


        yield return preObj=Instantiate(bundle.assetBundle.mainAsset)as GameObject;
        bundle.assetBundle.Unload(false);
    }


    private IEnumerator LoadTogetherObject_All_(string path)
    {
        WWW bundle = new WWW(path);


        yield return bundle;


        foreach (var it in bundle.assetBundle.LoadAllAssets())
        {
            preObjs.Add(Instantiate(it)as GameObject);
        }
        bundle.assetBundle.Unload(false);
    }


    private IEnumerator LoadTogetherObject_One_(string path,string assetName)
    {
        WWW bundle = new WWW(path);


        yield return bundle;
                     
        yield return preObj =Instantiate(bundle.assetBundle.LoadAsset(assetName))as GameObject;
        bundle.assetBundle.Unload(false);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值