Assetbundle打包自制工具

工具说明:

1.Editor文件拷贝至Project中
2.稍等片刻后点击菜单栏Tools->Assetbundle Tool
3.选择打包的平台(win,iphone,android)
4.在Project中,选中要打包的资源(prefab,图片,音效,文档等)

5.点击Package打包(打包后的文件在Assets的同级目录下,文件夹名为Dynamic_Asset)

《----------------------------------------------------------------------------》

EvangelTool:

//----------------------------------------------
//  EvangelTool
//  脚本描述
//----------------------------------------------
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;


public class EvangelTool{


    static Texture2D mGradientTex;


    static public Texture2D blankTexture
    {
        get
        {
            return EditorGUIUtility.whiteTexture;
        }
    }
    static public Texture2D gradientTexture
    {
        get
        {
            if (mGradientTex == null) mGradientTex = CreateGradientTex();
            return mGradientTex;
        }
    }
    static public void DrawSeparator()
    {
        GUILayout.Space(12f);


        if (Event.current.type == EventType.Repaint)
        {
            Texture2D tex = blankTexture;
            Rect rect = GUILayoutUtility.GetLastRect();
            GUI.color = new Color(0f, 0f, 0f, 0.25f);
            GUI.DrawTexture(new Rect(0f, rect.yMin + 6f, Screen.width, 4f), tex);
            GUI.DrawTexture(new Rect(0f, rect.yMin + 6f, Screen.width, 1f), tex);
            GUI.DrawTexture(new Rect(0f, rect.yMin + 9f, Screen.width, 1f), tex);
            GUI.color = Color.white;
        }
    }
    static public Rect DrawHeader(string text)
    {
        GUILayout.Space(28f);
        Rect rect = GUILayoutUtility.GetLastRect();
        rect.yMin += 5f;
        rect.yMax -= 4f;
        rect.width = Screen.width;


        if (Event.current.type == EventType.Repaint)
        {
            GUI.color = Color.black;
            GUI.DrawTexture(new Rect(0f, rect.yMin, Screen.width, rect.yMax - rect.yMin), gradientTexture);
            GUI.color = new Color(0f, 0f, 0f, 0.25f);
            GUI.DrawTexture(new Rect(0f, rect.yMin, Screen.width, 1f), blankTexture);
            GUI.DrawTexture(new Rect(0f, rect.yMax - 1, Screen.width, 1f), blankTexture);
            GUI.color = Color.white;
            GUI.Label(new Rect(rect.x + 4f, rect.y, rect.width - 4, rect.height), text, EditorStyles.boldLabel);
        }
        return rect;
    }
    static Texture2D CreateGradientTex()
    {
        Texture2D tex = new Texture2D(1, 16);
        tex.name = "[Generated] Gradient Texture";
        tex.hideFlags = HideFlags.DontSave;


        Color c0 = new Color(1f, 1f, 1f, 0f);
        Color c1 = new Color(1f, 1f, 1f, 0.4f);


        for (int i = 0; i < 16; ++i)
        {
            float f = Mathf.Abs((i / 15f) * 2f - 1f);
            f *= f;
            tex.SetPixel(0, i, Color.Lerp(c0, c1, f));
        }


        tex.Apply();
        tex.filterMode = FilterMode.Bilinear;
        return tex;
    }


    static public void HighlightLine(Color c)
    {
        Rect rect = GUILayoutUtility.GetRect(Screen.width - 16f, 22f);
        GUILayout.Space(-23f);
        c.a *= 0.3f;
        GUI.color = c;
        GUI.DrawTexture(rect, gradientTexture);
        c.r *= 0.5f;
        c.g *= 0.5f;
        c.b *= 0.5f;
        GUI.color = c;
        GUI.DrawTexture(new Rect(rect.x, rect.y + 1f, rect.width, 1f), blankTexture);
        GUI.DrawTexture(new Rect(rect.x, rect.y + rect.height - 1f, rect.width, 1f), blankTexture);
        GUI.color = Color.white;
    }

}

------------------------------------------------------------------

AssetBundleTool:

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


public class AssetBundleTool : EditorWindow
{
    //打包平台
    private enum Platform{
        Windows=0,
        Android=1,
        iPhone=2
    }
    private Platform target;


    //打包对象
    private enum PackType { 
        GirlIcon=0,
        Image=1,
        Music=2,
        Widget=3,
        Prop=4,
        Role=5
    }
    private PackType type;


    private BuildTarget buildTarget;


    private bool isMulti;
    private string filePath;


    void OnGUI()
    {
        EditorGUIUtility.LookLikeControls(100,100);


        GUILayout.Space(10f);
        GUILayout.Label("Make your assetBundle with the following parameters:");
        GUILayout.Space(10f);


        EvangelTool.DrawSeparator();


        GUILayout.BeginHorizontal();
        target = (Platform)EditorGUILayout.EnumPopup("BuildTarget:", target);
        GUILayout.EndHorizontal();


        GUILayout.BeginHorizontal();
        type = (PackType)EditorGUILayout.EnumPopup("PackType:", type);
        GUILayout.EndHorizontal();




        GUILayout.BeginHorizontal();
        isMulti = EditorGUILayout.Toggle("Multi-Packages", isMulti);
        GUILayout.Label("If build Multi-Packages,Please check it");
        GUILayout.EndHorizontal();


        GUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel("Click To Build");
        bool pack = GUILayout.Button("Package", GUILayout.Width(120f));
        GUILayout.EndHorizontal();
        GUILayout.Space(10f);


        EvangelTool.DrawHeader("You Select: " + Selection.objects.Length + " objects");
        EditorGUIUtility.LookLikeControls(50, 50);


        foreach (Object o in Selection.objects){
            EvangelTool.HighlightLine(new Color(0.6f, 0.6f, 0.6f));
            EditorGUILayout.LabelField(o.name, o.GetType().ToString());
        }


        EditorGUILayout.HelpBox("After the success of BuildAssetBundle," +
                                "They are Stored in the upper layer of the project file\n\n" +
                                "which named Dynamic_Asset.", MessageType.Info);
        if (pack) Package(target,type);
    }


    void Package(Platform target,PackType type)
    {
        if (Selection.objects.Length == 0) {
            EditorUtility.DisplayDialog("Warning", "You have not selected any object", "Confirm");
            return;
        }
        switch (target){
            case Platform.Windows:
                buildTarget = BuildTarget.StandaloneWindows; break;
            case Platform.Android:
                buildTarget = BuildTarget.Android; break;
            case Platform.iPhone:
                buildTarget = BuildTarget.iOS; break;
            default:
                Debug.LogError("Unrecognized Option");
                break;
}
        switch (type) {
            case PackType.GirlIcon:
                BuildAsset("GirlIcon.assetbundle"); break;
            case PackType.Image:
                BuildAsset("Image.assetbundle"); break;
            case PackType.Music:
                BuildAsset("Music.assetbundle"); break;
            case PackType.Prop:
                BuildAsset("Prop.assetbundle"); break;
            case PackType.Role:
                BuildAsset("Role.assetbundle"); break;
            case PackType.Widget:
                BuildAsset("Widget.assetbundle"); break;
            default:
                Debug.LogError("Unrecognized Option");
                break;
        }
    }


    void BuildAsset(string name) {
        Directory.CreateDirectory("Dynamic_Asset");
        filePath = Application.dataPath + "/../Dynamic_Asset/";


        string path = filePath + name;
        if (BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path, BuildAssetBundleOptions.CollectDependencies, buildTarget))
        {
            Debug.Log("create assetBundle [" + path + "] BINGO!");
        }
        else
        {
            Debug.Log("create assetBundle [" + path + "] ERROR!!!");
        }
    }
    void OnSelectionChange() { Repaint(); }

}


----------------------------------------------------------------

MyTools

using UnityEngine;
using UnityEditor;
using System.Collections;


public class MyTools
{
    [MenuItem("Tools/AssetBundle Tool")]
    static public void OpenPanelWizard()
    {
        EditorWindow.GetWindow(false, "AssetBundle", true);
    }

}

源文件:https://download.csdn.net/download/u010665359/8861861

你可能感兴趣的:(【Unity笔记】)