zoukankan      html  css  js  c++  java
  • Unity3d通过脚本生成apk

    参考链接:http://www.jianshu.com/p/a9261113b4ac

    照着链接的方法并没有正确生成APK,IPA没有测试过,不过大致的方法是正确的,修改如下: 

    Environment.bat

    :: set your own Unity path
    set unity="D:Program FilesUnityEditorUnity.exe"
    :: -debug or -release
    set debugParam=-release
    
    set projectPath="E:mycode	est	estpro"

    UnityToApk.bat

    call Environment.bat
    
    echo "Start Build Unity to Apk"
    
    %unity% -batchmode -projectPath %projectPath% -executeMethod CommandBuilder.PreBuild %debugParam% -quit -logFile ./PreBuild.log
    %unity% -batchmode -projectPath %projectPath% -executeMethod CommandBuilder.Build %debugParam% -android -quit -logFile ./BuildApk.log
    
    
    echo "End Build,please see log PreBuild.log and BuildApk.log"

    cs脚本,放在Editor目录下

    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    using System.IO;
    
    public class CommandBuilder {
    
        private static string[] build_scenes = { "Assets/ddd.unity" };
    
        private static bool ms_isDebugBuild = false;
        private static BuildTarget ms_buildTarget = BuildTarget.Android;
    
        private static string XCODE_PROJECT_NAME = "XCodeProject";
        private static string BUILD_OUTPUT_ANDROID = Application.dataPath + "/Bin/Android/";
    
    
        private static void UpdateBuildFlag()
        {
            string[] args = System.Environment.GetCommandLineArgs();
            foreach (string oneArg in args)
            {
                if (oneArg != null && oneArg.Length > 0)
                {
                    if (oneArg.ToLower().Contains("-debug"))
                    {
                        Debug.Log(""-debug" is detected, switch to debug build.");
                        ms_isDebugBuild = true;
                        return;
                    }
                    else if (oneArg.ToLower().Contains("-release"))
                    {
                        Debug.Log(""-release" is detected, switch to release build.");
                        ms_isDebugBuild = false;
                        return;
                    }
                }
            }
    
    
            if (ms_isDebugBuild)
            {
                Debug.Log("neither "-debug" nor "-release" is detected, current is to debug build.");
            }
            else
            {
                Debug.Log("neither "-debug" nor "-release" is detected, current is to release build.");
            }
    
        }
    
    
        private static void UpdateBuildTarget()
        {
            string[] args = System.Environment.GetCommandLineArgs();
            foreach (string oneArg in args)
            {
                if (oneArg != null && oneArg.Length > 0)
                {
                    if (oneArg.ToLower().Contains("-android"))
                    {
                        Debug.Log(""-android" is detected, switch build target to android.");
                        ms_buildTarget = BuildTarget.Android;
                        return;
                    }
                    else if (oneArg.ToLower().Contains("-iphone"))
                    {
                        Debug.Log(""-iphone" is detected, switch build target to iphone.");
                        ms_buildTarget = BuildTarget.iOS;
                        return;
                    }
                    else if (oneArg.ToLower().Contains("-ios"))
                    {
                        Debug.Log(""-ios" is detected, switch build target to iphone.");
                        ms_buildTarget = BuildTarget.iOS;
                        return;
                    }
                }
            }
    
            Debug.Log("neither "-android", "-ios" nor "-iphone" is detected, current build target is: " + ms_buildTarget);
        }
    
        public static void PreBuild()
        {
            Debug.Log("PreBuild");
            UpdateBuildFlag();
        }
        public static void Build()
        {
            Debug.Log("Build");
            UpdateBuildTarget();
    
            BuildOptions buildOption = BuildOptions.None;
            if (ms_isDebugBuild)
            {
                buildOption |= BuildOptions.Development;
                buildOption |= BuildOptions.AllowDebugging;
                buildOption |= BuildOptions.ConnectWithProfiler;
            }
            else
            {
                buildOption |= BuildOptions.None;
            }
    
            string locationPathName;
            if (BuildTarget.iOS == ms_buildTarget)
            {
                locationPathName = XCODE_PROJECT_NAME;
            }
            else
            {
                locationPathName = BUILD_OUTPUT_ANDROID;

       //原链接此处没有生成目录,而是使用rmdir mkdir生成,实际测试时无法将APK复制进目录中,所以讲目录修改如下
    if (Directory.Exists(locationPathName)) Directory.Delete(locationPathName, true); Directory.CreateDirectory(locationPathName); System.DateTime time = System.DateTime.Now; locationPathName += "test_" + time.Month.ToString("D2") + time.Day.ToString("D2") + "_" + time.Hour.ToString("D2") + time.Minute.ToString("D2") + ".apk"; } BuildPipeline.BuildPlayer(build_scenes, locationPathName, ms_buildTarget, buildOption); } public static void PostBuild() { Debug.Log("PostBuild"); } }
  • 相关阅读:
    从零开始学Kotlin-使用接口(7)
    从零开始学Kotlin-类的继承(6)
    从零开始学Kotlin-类和对象(5)
    从零开始学Kotlin-控制语句(4)
    从零开始学Kotlin-操作符(3)
    从零开始学Kotlin-数据类型(2)
    从零开始学Kotlin-基础语法(1)
    Java设计模式之单例模式(七种写法)
    一个简单的可控的头像列表叠加控件
    使用NestedScrollView+ViewPager+RecyclerView+SmartRefreshLayout打造酷炫下拉视差效果并解决各种滑动冲突
  • 原文地址:https://www.cnblogs.com/scotly/p/5151336.html
Copyright © 2011-2022 走看看