Unity Android 多渠道极速打包方案

Unity Android 多项目依赖打包方案 http://www.jianshu.com/p/e9cd674ef52f
iOS打包 http://www.jianshu.com/p/f347e00abb9c
C# 反射扩展 http://www.jianshu.com/p/cf05e80e4d1c

    static void FastBuildPlayers(params string[] channels)
    {
        FileTools.MoveFileOrDirectory("Temp/StagingArea", "StagingArea", true);

        PlayerSettings.Android.keystorePass = PlayerSettings.Android.keyaliasPass = "4sinyeeandroidteam";

        string stagingArea = "StagingArea";
        if(!Directory.Exists(stagingArea))
        {
            Debug.LogError("StagingArea 缓存资源不存在,不能极速打包");
            return;
        }

        string stagingAreaData = stagingArea + "/Data";

        string playerPackage = EditorApplication.applicationContentsPath + "/PlaybackEngines/AndroidPlayer";

        BuildOptions options = BuildOptions.ShowBuiltPlayer;
        if (EditorUserBuildSettings.development)
        {
            options |= BuildOptions.Development;

            if (EditorUserBuildSettings.connectProfiler)
                options |= BuildOptions.ConnectWithProfiler;
            if (EditorUserBuildSettings.allowDebugging)
                options |= BuildOptions.AllowDebugging;
        }

        if (EditorUserBuildSettings.exportAsGoogleAndroidProject)
            options |= BuildOptions.AcceptExternalModificationsToPlayer;


        foreach (var channel in channels)
        {
            string[] directorys = { stagingArea + "/libs", stagingArea + "/bin" };
            foreach (var dir in directorys)
            {
                FileTools.CreateOrCleanDirectory(dir);
            }

            FileTools.MoveFileOrDirectory(stagingArea + "/assets/bin/Data", stagingAreaData);
            FileTools.DeleteFileOrDirectory(stagingAreaData + "/unity default resources");

            SetManifestChannel(Application.dataPath + "/Plugins/Android/AndroidManifest.xml", channel);

            string installPath = Application.dataPath + "/../" + Application.productName + "-" + PlayerSettings.bundleVersion + "@" + PlayerSettings.Android.bundleVersionCode + "-" + channel + ".apk";
            if (EditorUserBuildSettings.development)
                installPath = Application.dataPath + "/../" + Application.productName + "-Development-" + PlayerSettings.bundleVersion + "@" + PlayerSettings.Android.bundleVersionCode + "-" + channel + ".apk";

            Debug.Log(installPath);

            //PostProcessAndroidPlayer.PostProcess(BuildTarget.Android, stagingAreaData, stagingArea, playerPackage, installPath, Application.companyName, Application.productName, options, null);

            try
            {
                var path = EditorApplication.applicationContentsPath + "/PlaybackEngines/AndroidPlayer/UnityEditor.Android.Extensions.dll";
                if (Application.platform == RuntimePlatform.OSXEditor)
                    path = EditorApplication.applicationPath + "/../PlaybackEngines/AndroidPlayer/UnityEditor.Android.Extensions.dll";

                var androidExtensions = Assembly.LoadFile(path);
                var androidPlayerType = androidExtensions.GetType("UnityEditor.Android.PostProcessAndroidPlayer");
                androidPlayerType.Invoke("PostProcess", BuildTarget.Android, stagingAreaData, stagingArea, playerPackage, installPath, Application.companyName, Application.productName, options, null);

                //var androidPlayer = androidPlayerType.InvokeConstructor();
                ////androidPlayer.Invoke("CreatePackagesWithAAPT", stagingArea, PlayerSettings.Android.useAPKExpansionFiles, new AndroidXmlDocument(Path.Combine(stagingArea, "assets/bin/Data/settings.xml")));
                //bool sign_using_keystore = PlayerSettings.Android.keyaliasName.Length != 0;
                //var androidLibrariesType = androidExtensions.GetType("UnityEditor.Android.AndroidLibraries");
                //var androidLibraries = androidLibrariesType.InvokeConstructor();
                //androidLibraries.Invoke("FindAndAddLibraryProjects", Path.Combine(Path.Combine(stagingArea, "android-libraries"), "*"));
                //androidPlayer.Invoke("BuildApk", stagingArea, sign_using_keystore, EditorUserBuildSettings.development, androidLibraries);
                //androidPlayer.Invoke("AlignPackage", stagingArea);

                //androidPlayer.GetFieldValue("m_Progress").Invoke("Step", "Copying to output folder", "Moving final Android package...");
                //androidPlayer.Invoke("MoveFinalAndroidPackage", stagingArea, PlayerSettings.Android.useAPKExpansionFiles, installPath);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }
            finally
            {
                EditorUtility.ClearProgressBar();
            }
        }
    }

你可能感兴趣的:(Unity Android 多渠道极速打包方案)