首页 > 技术文章 > AssetBundle流程备份

JimmyCode 2017-06-22 13:50 原文

 

资源Bundle创建,这里Assets下手动创建StreamingAssets文件夹

using UnityEngine;
using System.Collections;
using UnityEditor;

public class CreateAssetBundlesMain{
   
    [MenuItem("Custom Editor/Create AssetBundles Main")]

    static void CreateMain()
    {
        Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

        foreach(Object obj in SelectedAsset)
        {
            string sourcePath = AssetDatabase.GetAssetPath(obj);
            string targetPath = Application.dataPath + "/StreamingAssets/" + obj.name + ".assetbundle";
            if(BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.StandaloneWindows ))
            {
                Debug.Log(obj.name + "资源打包成功");
            }
            else
            {
                Debug.Log(obj.name + "资源打包失效");
            }
        }

        AssetDatabase.Refresh();
    }
}
using UnityEngine;
using System.Collections;
using UnityEditor;

public class CreateAssetBundlesAll {

    [MenuItem("Custom Editor/Create AssetBundles All")]

    static void CreatAll()
    {
        Caching.CleanCache();

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

        Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

        foreach(object obj in SelectedAsset)
        {
            Debug.Log("AssetBundle name:" + obj);
        }

        if(BuildPipeline.BuildAssetBundle(null, SelectedAsset, Path, BuildAssetBundleOptions.CollectDependencies, BuildTarget.StandaloneWindows))
        {
            AssetDatabase.Refresh();
        }
        else
        {

        }
    }
}
using UnityEngine;
using System.Collections;
using UnityEditor;

public class CreateSceneAll{

    [MenuItem("Custom Editor/Create AssetBundles Scene")]

    static void CreateScene()
    {
        Caching.CleanCache();
        string Path = Application.dataPath + "/StreamingAssets/MyScene.unity3d";
        string[] levels = { "Assets/Scene/Level1.unity" , "Assets/Scene/Level2.unity" };

        BuildPipeline.BuildStreamedSceneAssetBundle(levels, Path, BuildTarget.StandaloneWindows, BuildOptions.UncompressedAssetBundle);
        AssetDatabase.Refresh();
    }
}

 

资源的引用

using UnityEngine;
using System.Collections;

public class RunScript : MonoBehaviour
{
    //不同平台StreamingAssets路径有差异
    public static readonly string PathURL =
#if UNITY_ANDROID
        "jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
        Application.dataPath + "/Raw/";
#elif UNITY_STANDLONE_WIN || UNITY_EDITOR
        "file://" + Application.dataPath + "/StreamingAssets/";
#else
    string.Empty;
#endif

    void OnGUI()
    {
        if(GUILayout.Button("Main Assetbundle"))
        {
            StartCoroutine(LoadMain(PathURL + "GameObject1.assetbundle"));
            StartCoroutine(LoadMain(PathURL + "GameObject2.assetbundle"));
        }
        if(GUILayout.Button("All Assetbundle"))
        {
            StartCoroutine(LoadAll(PathURL + "All.assetbundle"));
        }

        if(GUILayout.Button("Load Scene"))
        {
            StartCoroutine(LoadScene());
        }
    }

    private IEnumerator LoadMain(string path)
    {
        WWW bundle = new WWW(path);
        yield return bundle;

        yield return Instantiate(bundle.assetBundle.mainAsset);
        bundle.assetBundle.Unload(false);
    }

    private IEnumerator LoadAll(string path)
    {
        WWW bundle = new WWW(path);
        yield return bundle;

        Object obj1 = bundle.assetBundle.LoadAsset("GameObject1");
        Object obj2 = bundle.assetBundle.LoadAsset("GameObject2");

        yield return Instantiate(obj1);
        yield return Instantiate(obj2);

        bundle.assetBundle.Unload(false);
    }

    private IEnumerator LoadScene()
    {
        AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/MyScene.unity3d");
        yield return ab;
        Application.LoadLevel("Level2");
    }
}

资源层级截图

 

 http://blog.csdn.net/lyh916/article/details/49815871

http://www.xuanyusong.com/archives/2405

推荐阅读