打包及加载AssetBundle资源

打包

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;

/// <summary>
/// 把Resource下的资源打包成.data到OutPut目录下
/// </summary>
public class Builder : Editor
{
    public static string sourcePath;
    const string AssetBundlesOutputPath = "Assets/StreamingAssets";

    [MenuItem("Custom/BuildAssetBundle")]
    public static void BuildAssetBundle()
    {
        sourcePath = AssetDatabase.GetAssetPath(Selection.activeObject);
        Debug.Log("sourcePath is " + sourcePath);
        ClearAssetBundlesName();

        Pack(sourcePath);

        string outputPath = Path.Combine(AssetBundlesOutputPath, Platform.GetPlatformFolder(EditorUserBuildSettings.activeBuildTarget));
        if (!Directory.Exists(outputPath))
        {
            Directory.CreateDirectory(outputPath);
        }
        Debug.Log("outputPath is " + outputPath);
      
        //根据BuildSetting里面所激活的平台进行打包 设置过AssetBundleName的都会进行打包
        BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget);

        AssetDatabase.Refresh();

        Debug.Log("打包完成");

    }

    /// <summary>
    /// 清除之前设置过的AssetBundleName,避免产生不必要的资源也打包
    /// 之前说过,只要设置了AssetBundleName的,都会进行打包,不论在什么目录下
    /// </summary>
    static void ClearAssetBundlesName()
    {
        int length = AssetDatabase.GetAllAssetBundleNames().Length;
        Debug.Log(length);
        string[] oldAssetBundleNames = new string[length];
        for (int i = 0; i < length; i++)
        {
            oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];
        }

        for (int j = 0; j < oldAssetBundleNames.Length; j++)
        {
            AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j], true);
        }
        length = AssetDatabase.GetAllAssetBundleNames().Length;
        Debug.Log("length is " + length);
    }

    static void Pack(string source)
    {
        //Debug.Log("Pack source " + source);
        DirectoryInfo folder = new DirectoryInfo(source);
        FileSystemInfo[] files = folder.GetFileSystemInfos();
        int length = files.Length;
        for (int i = 0; i < length; i++)
        {
            if (files[i] is DirectoryInfo)
            {
                Pack(files[i].FullName);
            }
            else
            {
                if (!files[i].Name.EndsWith(".meta"))
                {
                    fileWithDepends(files[i].FullName);
                }
            }
        }
    }
    //设置要打包的文件
    static void fileWithDepends(string source)
    {
        Debug.Log("file source " + source);
        string _source = Replace(source);
        string _assetPath = "Assets" + _source.Substring(Application.dataPath.Length);

        Debug.Log(_assetPath);

        //自动获取依赖项并给其资源设置AssetBundleName
        string[] dps = AssetDatabase.GetDependencies(_assetPath);
        foreach (var dp in dps)
        {
            Debug.Log("dp " + dp);
            if (dp.EndsWith(".cs"))
                continue;
            AssetImporter assetImporter = AssetImporter.GetAtPath(dp);
            string pathTmp = dp.Substring("Assets".Length + 1);
            string assetName = pathTmp.Substring(pathTmp.IndexOf("/") + 1);
            assetName = assetName.Replace(Path.GetExtension(assetName), ".data");
            Debug.Log(assetName + "assetName");
            assetImporter.assetBundleName = assetName;
        }
    }

    //设置要打包的文件
    static void file(string source)
    {
        Debug.Log("file source " + source);
        string _source = Replace(source);
        string _assetPath = "Assets" + _source.Substring(Application.dataPath.Length);
        string _assetPath2 = _source.Substring(Application.dataPath.Length + 1);
        //Debug.Log (_assetPath);

        //在代码中给资源设置AssetBundleName
        AssetImporter assetImporter = AssetImporter.GetAtPath(_assetPath);
        string[] dps = AssetDatabase.GetDependencies(_assetPath);
        foreach (var dp in dps)
        {
            Debug.Log("dp " + dp);
        }
        string assetName = _assetPath2.Substring(_assetPath2.IndexOf("/") + 1);
        assetName = assetName.Replace(Path.GetExtension(assetName), ".unity3d");
        Debug.Log(assetName);
        assetImporter.assetBundleName = assetName;
    }

    static string Replace(string s)
    {
        return s.Replace("\", "/");
    }
}

public class Platform
{
    public static string GetPlatformFolder(BuildTarget target)
    {
        switch (target)
        {
            case BuildTarget.Android:
                return "Android";
            case BuildTarget.iOS:
                return "IOS";
            case BuildTarget.WebPlayer:
                return "WebPlayer";
            case BuildTarget.StandaloneWindows:
            case BuildTarget.StandaloneWindows64:
                return "Windows";
            case BuildTarget.StandaloneOSXIntel:
            case BuildTarget.StandaloneOSXIntel64:
            case BuildTarget.StandaloneOSXUniversal:
                return "OSX";
            default:
                return null;
        }
    }
}
Build代码

加载资源

using UnityEngine;
using System.Collections;
using System;
using UnityEngine.SceneManagement;
public class TestLoadNewAB : MonoBehaviour
{

    void OnGUI()
    {
        if (GUILayout.Button("LoadAssetbundle"))
        {
            #region 加载AssetBundle
            DateTime t0 = DateTime.Now;
            //加载Manifest文件;  
            AssetBundle manifestBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath
                                                                  + "/Android/Android");

            Debug.Log(manifestBundle == null);
            if (manifestBundle != null)
            {
                AssetBundleManifest manifest = (AssetBundleManifest)manifestBundle.LoadAsset("AssetBundleManifest");
                Debug.Log(manifest == null);
                //获取依赖文件列表;  
                string[] cubedepends = manifest.GetAllDependencies("fruitgame/scene/fruitgame.data");

                Debug.Log(cubedepends.Length);
                foreach (var item in cubedepends)
                {
                    Debug.Log("depends " + item);
                }
                AssetBundle[] dependsAssetbundle = new AssetBundle[cubedepends.Length];

                for (int index = 0; index < cubedepends.Length; index++)
                {
                    dependsAssetbundle[index] = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/Android/" + cubedepends[index]);
                }

                //加载场景文件
                AssetBundleCreateRequest sceneBundle = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath + "/Android/fruitgame/scene/fruitgame.data");

            
                if (sceneBundle.assetBundle.isStreamedSceneAssetBundle)
                {
                    string[] scenePaths = sceneBundle.assetBundle.GetAllScenePaths();
                    string sceneName = System.IO.Path.GetFileNameWithoutExtension(scenePaths[0]);
                    SceneManager.LoadSceneAsync(sceneName);
                }
                DateTime t1 = DateTime.Now;
                TimeSpan timeCost = t1.Subtract(t0).Duration();
                Debug.Log("加载用时 " + timeCost.Milliseconds.ToString() + "毫秒");
            }
            #endregion
        }
    }
}
加载
原文地址:https://www.cnblogs.com/leesymbol/p/7735284.html