1. 程式人生 > >自動打包02工具類_Demo

自動打包02工具類_Demo

通過最近的學習擴充套件一下自動打包的功能,重寫了之前的程式碼,支援Windows和Mac打包Android和IOS(Xcode工程包),簡直perfect~~

這個自動打包只是一個Demo,簡單的實現了出包功能,直接拿來用與手動也沒多大區別,不過是少點幾下滑鼠,實際使用還要與開發流程相結合,設定條件,自動呼叫打包方法,自動出包~

//自動打包部分:

#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System.IO;

//Assets/Editor資料夾下

namespace SimpleFrame.Tool
{
    public class PackageTool
    {
        //打包操作
        public static void Build(string apkPath, string apkName, BuildTarget buildTarget)
        {
            if (!Directory.Exists(apkPath) || string.IsNullOrEmpty(apkName))
                return;

            if (BuildPipeline.isBuildingPlayer)
                return;

            //打包場景路徑
            List<string> sceneNames = new List<string>();
            foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
            {
                if (scene != null && scene.enabled)
                {
                    sceneNames.Add(scene.path);
                    //Debug.Log(scene.path);
                }
            }
            if(sceneNames.Count == 0)
            {
                Debug.Log("Build Scene Is None");
                return;
            }

            BuildTargetGroup buildTargetGroup = GetBuildTargetGroupByBuildTarget(buildTarget);
            //切換平臺
            EditorUserBuildSettings.SwitchActiveBuildTarget(buildTargetGroup, buildTarget);
            //設定打包平臺、標籤
            PlayerSettings.SetScriptingDefineSymbolsForGroup(buildTargetGroup, null);

            string path = apkPath + "/" + apkName;

            if (buildTarget == BuildTarget.Android)
                path += ".apk";
            else if(buildTarget == BuildTarget.iOS)
                path += "_IOS";
            
            Debug.Log("Start Build Package");
            //開始打包
            BuildPipeline.BuildPlayer(sceneNames.ToArray(), path, buildTarget, BuildOptions.None);
        }

        ////回撥  場景執行前操作
        //[PostProcessScene(1)]
        //public static void BeforeBuild()
        //{
        //    Debug.Log("Build Start");
        //}

        //回撥  打包後操作
        [PostProcessBuild(1)]
        public static void AfterBuild(BuildTarget target, string pathToBuiltProject)
        {
            Debug.Log("Build Success  " + target + "  " + pathToBuiltProject);

            //開啟檔案或資料夾
            System.Diagnostics.Process.Start(pathToBuiltProject);
        }

        static BuildTargetGroup GetBuildTargetGroupByBuildTarget(BuildTarget buildTarget)
        {
            switch(buildTarget)
            {
                case BuildTarget.Android:
                    return BuildTargetGroup.Android;

                case BuildTarget.iOS:
                    return BuildTargetGroup.iOS;

                case BuildTarget.StandaloneOSX:
                case BuildTarget.StandaloneWindows:
                case BuildTarget.StandaloneWindows64:
                    return BuildTargetGroup.Standalone;
     
                    //······
                default:
                    return BuildTargetGroup.Standalone;
            }
        }
    }
}
#endif

//接下來這一段程式碼,自定義了一個彈窗,不涉及自動打包,可忽略 ······

    //確認視窗, 可用快捷鍵啟動
    public class BuildPackageWindow : EditorWindow
    {
        [MenuItem("MyTools/Package/Build Android Package #&b")]
        public static void ShowPackageWindow()
        {
            //彈出確認視窗
            EditorWindow.GetWindow(typeof(BuildPackageWindow), false, "Build Package Window");
        }

        const string buildPathPlayerPrefsStr = "BuildPathPlayerPrefsStr";

        string showStr;
        Texture2D defaultIcon;

        //
        string apkPath, apkName;

        void OnEnable()
        {
            //確認資訊
            showStr = "Package Info :";
            showStr += "\nCompany Name : " + PlayerSettings.companyName;
            showStr += "\nProduct Name : " + PlayerSettings.productName;
            showStr += "\nIdentifier : " + PlayerSettings.applicationIdentifier;
            showStr += "\nBundle Version : " + PlayerSettings.bundleVersion;
            showStr += "\nBundle Version Code : " + PlayerSettings.Android.bundleVersionCode;

            //圖示Iocn
            Texture2D[] texture2Ds = PlayerSettings.GetIconsForTargetGroup(BuildTargetGroup.Android);
            defaultIcon = texture2Ds.Length > 0 ? texture2Ds[0] : null;

            //打包預設路徑及預設檔名
            apkPath = PlayerPrefs.GetString(buildPathPlayerPrefsStr);
            apkName = PlayerSettings.productName;
        }

        void OnGUI()
        {
            GUILayout.Space(10);
            GUILayout.Label("Please Make Sure");
            defaultIcon = EditorGUILayout.ObjectField(defaultIcon, typeof(Texture2D), true) as Texture2D;
            EditorGUILayout.TextArea(showStr);

            GUILayout.Space(5);
            apkPath = EditorGUILayout.TextField("Package Path : ", apkPath);
            if (!Directory.Exists(apkPath))
                EditorGUILayout.TextArea("Package Path Is Error");

            apkName = EditorGUILayout.TextField("Package Name : ", apkName);
            if (string.IsNullOrEmpty(apkName))
                EditorGUILayout.TextArea("Package Name Is Empty");

            //呼叫系統視窗,選擇儲存路徑
            if (GUILayout.Button("Choose Path"))
            {
                //EditorUtility.OpenFolderPanel("Choose Output Path", "", "");
                string tmpPath = EditorUtility.SaveFolderPanel("Choose Package Output Path", "", "");

                if (!string.IsNullOrEmpty(tmpPath))
                {
                    apkPath = tmpPath;
                    PlayerPrefs.SetString(buildPathPlayerPrefsStr, apkPath);
                }
            }

            GUILayout.Space(10);
            if (GUILayout.Button("Cancel"))
                this.Close();
            GUILayout.Space(5);

            if (Directory.Exists(apkPath) && !string.IsNullOrEmpty(apkName))
            {
                if (GUILayout.Button("Build Android"))
                {
                    this.Close();
                    PackageTool.Build(apkPath, apkName, BuildTarget.Android);
                }

                GUILayout.Space(5);

                if (GUILayout.Button("Build IOS"))
                {
                    this.Close();
                    PackageTool.Build(apkPath, apkName, BuildTarget.iOS);
                }
            }
            this.Repaint();
        }

        void OnDisable()
        {
            showStr = null;
            defaultIcon = null;
        }
    }