u3d自动打包(ipa/apk)_skybeauty_新浪博客


导出IOS项目或者是android项目

# -*- coding:utf-8 -*-
"""
    Export ios project from unity
"""

import sys
import os
import subprocess
import shutil

BUILD_FUNC = "BuildProject.ExportIOS"
PLAT_FORM = "IOS"
PROJECT_PATH=""

def removeDir(dir_):
    if os.path.exists(dir_):
        shutil.rmtree(dir_)

def curFileDir():
     path = sys.path[0]
     if os.path.isdir(path):
         return path
     elif os.path.isfile(path):
         return os.path.dirname(path)

def mkDir(dir_):
    os.makedirs(dir_)

if __name__ == "__main__":

    if len(sys.argv)<3:
        print("input error ::python excutFile platform projectPath")
        exit(0)

    PLAT_FORM = sys.argv[1] 
    PROJECT_PATH = sys.argv[2]
    if PLAT_FORM == "ANDROID":
        BUILD_FUNC = "BuildProject.ExportAndroid"
    command = ["/Applications/Unity/Unity.app/Contents/MacOS/Unity"]
    command.append("-projectPath")
    command.append(PROJECT_PATH)
    command.append("-executeMethod")
    command.append(BUILD_FUNC)
    command.append("-logFile")
    command.append("/Users/Desktop/Editor.log")
    command.append("-quit")
    command.append("-batchmode")
    

    subprocess.check_output(command)


编译ios项目

# -*- coding:utf-8 -*-
"""
    Build ios project
"""

import sys
import os
import subprocess
import shutil

from time import strftime, localtime

PROVISIONING_PROFILE = "67b8198c-736c-449c-9992-9e9d141c1a00"
CODE_SIGN_IDENTITY="iPhone Distribution:XXX"

IPA_DIR_PATH=""
APP_NAME = "Test"




def removeDir(dir_):
    if os.path.exists(dir_):
        shutil.rmtree(dir_)

def curFileDir():
     path = sys.path[0]
     if os.path.isdir(path):
         return path
     elif os.path.isfile(path):
         return os.path.dirname(path)

def cleanProject():
    command = ["xcodebuild"]
    command.append("clean")

    subprocess.check_output(command)

def buildProject():
    command = ["xcodebuild"]
    command.append("PROVISIONING_PROFILE=%s"%(PROVISIONING_PROFILE))
    command.append("CODE_SIGN_IDENTITY=%s"%(CODE_SIGN_IDENTITY))
    command.append("ENABLE_BITCODE=NO")
    subprocess.check_output(command)

def packageIPA():
    timeStr = strftime("%Y_%m_%d_%H_%M_%S",localtime())
    ipaName = APP_NAME+"_" + timeStr + ".ipa"
    svnDirPath = os.path.join(curFileDir(),"../IOSIPA")
    svnIpaPath = os.path.join(svnDirPath,ipaName)
    ipaPath = os.path.join(IPA_DIR_PATH,ipaName)

    

    command = ["xcrun"]
    command.append("-sdk")
    command.append("iphoneos")
    command.append("PackageApplication")
    command.append("-v")
    command.append("build/Release-iphoneos/%s.app"%APP_NAME)
    command.append("-o")
    command.append(ipaPath)

    subprocess.check_output(command)
    shutil.copyfile(ipaPath,svnIpaPath)



if __name__ == "__main__":

    if len(sys.argv)<2:
        print("error input::python build.py ipaPath")
        exit(0)

    buildDir = curFileDir() + "/proj"
    os.chdir(buildDir)

    IPA_DIR_PATH = sys.argv[1]

    projectFilePath = os.path.join(curFileDir(),"Unity-iPhone.xcodeproj")
    destProjectFilePath = os.path.join(buildDir,"Unity-iPhone.xcodeproj")

    if os.path.exists(projectFilePath):
        if os.path.exists(destProjectFilePath):
            shutil.rmtree(destProjectFilePath)
        shutil.copytree(projectFilePath,destProjectFilePath)

    cleanProject()
    buildProject()
    packageIPA()

    desDirPath = os.path.join(curFileDir(),"../../version/export/"+strftime("%Y_%m_%d_%H_%M_%S",localtime()))

    if os.path.exists(desDirPath):
        shutil.rmtree(desDirPath)
    shutil.copytree(buildDir,desDirPath)




c#脚本文件

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
public class BuildProject
{
    static readonly string IOS_PROJECT_PATH = "../../../../build/proj";
    static readonly string ANDROID_PROJECT_PATH = "../../../../build/android/android.apk";
    static string [] GetBuildScenes()
    {
        List names = new List();

        foreach (EditorBuildSettingsScene e in EditorBuildSettings.scenes)
        {
            if (e == null)
                continue;
            if (e.enabled)
                names.Add(e.path);
        }
        return names.ToArray();
    }

static public void ExportIOS()
{
        if (EditorUserBuildSettings.activeBuildTarget != BuildTarget.iOS)
        {
            EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTarget.iOS);
            EditorUserBuildSettings.activeBuildTargetChanged = delegate () 
            {
                if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS)
                {
                    BuildPipeline.BuildPlayer(GetBuildScenes(), IOS_PROJECT_PATH, BuildTarget.iOS, BuildOptions.None);
                }
            };
        }
        else
        {
            BuildPipeline.BuildPlayer(GetBuildScenes(), IOS_PROJECT_PATH, BuildTarget.iOS, BuildOptions.None);
        }

        
        
}

    static public void ExportAndroid()
    {
        int clientIndex = Application.dataPath.IndexOf("Assets");
        string tempPath = Application.dataPath.Remove(clientIndex);
        PlayerSettings.keyaliasPass = "test";
        PlayerSettings.keystorePass = "test";
        PlayerSettings.Android.keystoreName = tempPath+"user.keystore";
        if (EditorUserBuildSettings.activeBuildTarget != BuildTarget.Android)
        {
            EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTarget.Android);
            EditorUserBuildSettings.activeBuildTargetChanged = delegate ()
            {
                if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
                {
                    BuildPipeline.BuildPlayer(GetBuildScenes(), ANDROID_PROJECT_PATH, BuildTarget.Android, BuildOptions.None);
                }
            };
        }
        else
        {
            BuildPipeline.BuildPlayer(GetBuildScenes(), ANDROID_PROJECT_PATH, BuildTarget.Android, BuildOptions.None);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值