AssetsBundle 打包

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class PackageTools : Editor{

//StreamingAssets移动平台的流路径,他会把所有丢进去的文件变成byte[]

    //打包到本地使用LZ4打包,资源大小比较大,但是加载解包速度快
    //打包到网络上面推荐使用LZMA打包,资源包比较小

[MenuItem("MyTools/PackageToLocal")]        //自定义一个编辑器按钮,父按钮的名字叫MyTools和file按钮同级,下面有个子按钮叫PackageToLocal
public static void PackageToLocal (){
    BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath,BuildAssetBundleOptions.ChunkBasedCompression,BuildTarget.StandaloneWindows64);       //5.x要用这个api去打包
}

[MenuItem("MyTools/PackageToSever")]        //自定义一个编辑器按钮,父按钮的名字叫MyTools和file按钮同级,下面有个子按钮叫PackageToLocal
public static void PackageToSever()
{
    BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);       //5.x要用这个api去打包
}

[MenuItem("MyTools/PackageToAndroidLocal")]        //自定义一个编辑器按钮,父按钮的名字叫MyTools和file按钮同级,下面有个子按钮叫PackageToLocal
public static void PackageToAndroidLocal()
{
    BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.Android);       //5.x要用这个api去打包
}

[MenuItem("MyTools/PackageToAndoirdSever")]        //自定义一个编辑器按钮,父按钮的名字叫MyTools和file按钮同级,下面有个子按钮叫PackageToLocal
public static void PackageToAndroidServer()
{
    BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath, BuildAssetBundleOptions.None, BuildTarget.Android);       //5.x要用这个api去打包
}

}

打包资源包

using UnityEditor;

public class AssetBundleManager : Editor {

//StreamingAssets 移动平台的流路径,他会把所有丢进去的文件变成流
//打包到本地使用LZ4打包,,打包到服务器用LZMA
//打包到网络上面推荐使用LZMA打包,资源包比较小

[MenuItem("工具/PC/打包到本地")]
public static void PackageToLocal(){
    BuildPipeline.BuildAssetBundles (
        Application.streamingAssetsPath, 
        //打包到本地,用LZ4
        BuildAssetBundleOptions.ChunkBasedCompression, 
        //PC端windows64
        BuildTarget.StandaloneWindows64
    );
}

[MenuItem("工具/PC/打包到服务器")]
public static void PackageToSever(){
    BuildPipeline.BuildAssetBundles (
        Application.streamingAssetsPath, 
        //打包到本地,用LZ4
        BuildAssetBundleOptions.None, 
        //PC端windows64
        BuildTarget.StandaloneWindows64
    );
}

[MenuItem("工具/安卓/打包到本地")]
public static void PackageToAndroidLocal(){
    BuildPipeline.BuildAssetBundles (
        Application.streamingAssetsPath, 
        //打包到本地,用LZ4
        BuildAssetBundleOptions.ChunkBasedCompression, 
        //PC端windows64
        BuildTarget.Android
    );
}

[MenuItem("工具/安卓/打包到服务器")]
public static void PackageToAndroidSever(){
    BuildPipeline.BuildAssetBundles (
        Application.streamingAssetsPath, 
        //打包到本地,用LZ4
        BuildAssetBundleOptions.None, 
        //PC端windows64
        BuildTarget.Android
    );
}

}

加载资源包

网络

IEnumerator LoadFormServer()
{
    WWW www = WWW.LoadFromCacheOrDownload("http://101.200.76.117/TowerDefense/monster",1);    //不是马上下载好,需要等待,然后他第一次下载的数据会缓存,如果下次版本号不变,他就不用再下载  

    yield return www;//等待他下载好

    //abcr.progress进度,如果完全没下载就是0,下载完就是1  

    // 如果下载完成了
    if (www.isDone)
    {
        mObjs = www.assetBundle.LoadAllAssets();

    }
    mIsDone = true;
}

本地

IEnumerator LoadFormLocal() {
    AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath + "/monster");//加载Ab的函数,他不是马上可以加载好的,需要时间
    yield return abcr;//等待他下载好

    //abcr.progress进度,如果完全没下载就是0,下载完就是1  


    // 如果下载完成了
    if (abcr.isDone) {
        // mObjs = abcr.assetBundle.mainAsset; //如果包里面只有一个物体,那么用这个就能取到唯一的那个物体
        // mObjs[0] = abcr.assetBundle.Load("");        //通过名字取找物体
        mObjs = abcr.assetBundle.LoadAllAssets();
    }
    mIsDone = true;
//    BakeAllMonster ();
}

public class ResourceseLoad : MonoBehaviour {
//网络上的资源使用 WWW.LoadFromCacheOrDownload
//本地的使用 LoadFromFileAsync
public static ResourceseLoad RL;
public GameObject[] mObjs = new GameObject[7];
private bool isDone = false;
public bool IsDone {
get{ return isDone; }
}

void Awake(){
    RL = this;
}

public void Init () {
    StartCoroutine ("LoadFromLocal");
}

IEnumerator LoadFromLocal(){
    //加载的函数
    AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync (Application.streamingAssetsPath + "/allmonster");
    //等待他下载好
    yield return abcr;
    //如果下载好了
    if (abcr.isDone) {
        // mObjs = abcr.assetBundle.mainAsset;  如果包里面只有一个物体,那么就可以用mainAsset
        //mObjs[0]=abcr.assetBundle.Load("");   通过名字去找

        //读取全部
        mObjs=abcr.assetBundle.LoadAllAssets();
        isDone = true;
        StopCoroutine ("LoadFromLocal");
    }
}

}

你可能感兴趣的:(AssetsBundle 打包)