Unity中的一键打包实现

unity开发中 在发包之前经常会做一些额外操作。比如打包assetbundle。使用lua的还会创建对应的wrap代码等。以及编译完成后上传assetbundle包到web服务器。app到测试服务器等

这些额外的动作会导致打包的碎片化。

于是写了这么一个一键打包的脚本。实现不怎么漂亮,好在完成了功能,好在这代码几乎不会改动也没什么人会用。

思路就是把所有要做的事情依次执行,唯一的问题是某些步骤执行完毕后会有一个编译过程,要等编译结束才能执行下一步。下面直接上代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using UnityEditor;
using CSObjectWrapEditor;
using System.Threading;
using System.Net;

[InitializeOnLoad]

public class AutoBuild
{

    static List<Action> actions=new List<Action>();
    static AutoBuild()
    {
        CreateBuildActions();
        
    }
    static void CreateBuildActions()
    {
        string buildActions = EditorPrefs.GetString("BuildActions");
        if (buildActions.Equals("Android"))
        {
            actions = new List<Action>();
            actions.Add(CreateGameRes.CopyLua);
            actions.Add(CreateGameRes.CreateABOnly);
            actions.Add(Generator.GenAll);
            actions.Add(BuildApp);
            actions.Add(Generator.ClearAll);            
            EditorApplication.update += BuildUpdate;
        }
        else if (buildActions.Equals("AndroidAndUpload"))
        {
            actions = new List<Action>();
            actions.Add(CreateGameRes.CopyLua);
            actions.Add(CreateGameRes.CreateABOnly);
            actions.Add(Generator.GenAll);
            actions.Add(BuildApp);
            actions.Add(Generator.ClearAll);
            actions.Add(UploadApp);
            EditorApplication.update += BuildUpdate;
        }
        else if (buildActions.Equals("IOS"))
        {

        }
    }
    // static int actionIndex = 0;
    [MenuItem("Assets/AutoBuild/BuildAndroid")]
    public static void BuildAndroid()
    {
        EditorPrefs.SetInt("BuildIndex", 0);
        EditorPrefs.SetString("BuildActions", "Android");
        CreateBuildActions();
        BuildUpdate();
    }
    [MenuItem("Assets/AutoBuild/BuildAndroidAndUpLoad")]
    public static void BuildAndroidAndUpLoad()
    {
        EditorPrefs.SetInt("BuildIndex", 0);
        EditorPrefs.SetString("BuildActions", "AndroidAndUpload");
        CreateBuildActions();
        BuildUpdate();
    }
    public static void BuildApp()
    {
        Debug.Log("Build Over");
        // EditorPrefs.SetInt("BuildIndex", -1);

        string[] levels = { "Assets/Scenes/SampleScene.unity" };
        BuildPipeline.BuildPlayer(levels, "../t.APK", BuildTarget.Android, BuildOptions.None);
        // EditorApplication.update -= BuildUpdate;
    }
    private static void BuildUpdate()
    {
        int actionIndex = EditorPrefs.GetInt("BuildIndex");
        if (!EditorApplication.isCompiling)
        {
            if (actionIndex < actions.Count && actionIndex >= 0)
            {
                Debug.Log(actionIndex);
                actions[actionIndex].Invoke();
                actionIndex++;
            }
            else
            {
                Debug.Log(actionIndex);
                actionIndex = -1;
            }
            EditorPrefs.SetInt("BuildIndex", actionIndex);
        }
        if (actionIndex < 0)
        {
            EditorApplication.update -= BuildUpdate;
        }
    }    

}

猜你喜欢

转载自www.cnblogs.com/TheMeteor/p/10202134.html