unity3d打包辅助工具

辅助工具主要使用的库有mod_pbxproj与plistlib,可以在github中搜到。

主要过程是:

  1. 调用 unity -batchmode -quit -projectPath去生成对应的Xcode项目;

  2. 添加Xcode需要的第三方库;

  3. 使用不同的签名调用 Xcodebuild -project打包,并生成ipa包;

示例代码如下:

#coding=utf-8

import plistlib
import sys,os
import re

from mod_pbxproj import XcodeProject


global config

global xocedeinfo

#读取plist配置
def readsohaConfig():
    print "Step:readsohaConfig"
    global soha_config
    soha_config= plistlib.readPlist('../config_soha.plist')
    print soha_config

def readConfig():
    print "Step:readConfig"
    global config
    config = plistlib.readPlist(os.path.join( soha_config["ProjectPath"],'soha_break/info.plist'))
    print config

#编译Unity项目
def buildUnityProject():
    print "Step:buildUnityProject"
    cmd = soha_config["UnityPath"] + " -batchmode -quit -projectPath " + soha_config["ProjectPath"] + "/ClientProject -executeMethod AutoEdit.SohaiOSAll"
    print cmd
    os.system(cmd)

#配置xcode
def updateXcodeSetting(prj):
    print "Step:updateXcodeSetting"
    proj = XcodeProject.Load(soha_config["ProjectPath"] + '/'+prj+'/Unity-iPhone.xcodeproj/project.pbxproj')
    #添加库
    new_group = proj.get_or_create_group('Frameworks')
    code_group = proj.get_or_create_group('MainPlugin')
    proj.add_folder(soha_config["ProjectPath"]+'/IOS_ThirdParty/Soha/SohaSDK',parent =  new_group)
    proj.add_file(soha_config["ProjectPath"]+'/IOS_ThirdParty/Soha/MainPlugins.h',parent =  code_group)
    proj.add_file(soha_config["ProjectPath"]+'/IOS_ThirdParty/Soha/MainPlugins.m',parent =  code_group)
    proj.add_file(soha_config["ProjectPath"]+'/IOS_ThirdParty/Soha/FacebookSDK.framework', parent =  new_group)

    proj.backup()
    proj.save()


#编译xcode项目
def buildXcodeProject(channel):
    print "Step:buildXcodeProject"

    if channel == "soha_jb":
        cmd = "xcodebuild -project '"+soha_config["ProjectPath"]+"/soha_break/Unity-iPhone.xcodeproj' -target 'Unity-iPhone'"
        cmd = cmd + ' CODE_SIGN_IDENTITY="iPhone Developer: xxx xxx (FKXM6S27R7)"' + ' PROVISIONING_PROFILE="D6EB2EA1-307E-460F-92B3-726B4B3Axxxx"'
    elif channel == "soha_appstore":
        cmd = "xcodebuild -project '"+soha_config["ProjectPath"]+"/soha/Unity-iPhone.xcodeproj' -target 'Unity-iPhone'"
        cmd = cmd + ' CODE_SIGN_IDENTITY="iPhone Distribution: yyy yyy (KH9BY744T4)"' + ' PROVISIONING_PROFILE="3CE90DAD-1BC7-4FDA-BF6D-49500248yyyy"'
    elif channel == "soha_adhoc":
        cmd = "xcodebuild -project '"+soha_config["ProjectPath"]+"/soha/Unity-iPhone.xcodeproj' -target 'Unity-iPhone'"
        cmd = cmd + ' CODE_SIGN_IDENTITY="iPhone Distribution: zzz zzz (KH9BY744T4)"' + ' PROVISIONING_PROFILE="261BCDAD-4524-4B90-BD9F-499E5E43zzzz"'
    print cmd
    os.system(cmd)

#生成IPA
def generateIPA(channel):
    print "Step:generateIPA"
    appname = ""
    ipaname = ""
    if channel == "soha_jb":
        appname = "tlbbkdtl.app"
        ipaname = "tlbbkdtl_"+ config["CFBundleVersion"] + "soha_jb.ipa"
        cmd = "/usr/bin/xcrun -sdk iphoneos PackageApplication -v '" + soha_config["ProjectPath"] +"/soha_break/Build/" + appname + "' -o '" + soha_config["ReleaseFolder"] +"/"+ ipaname + "'"
    elif channel == "soha_appstore":
        appname = "sg20.app"
        ipaname = "tlbbkdtl_"+ config["CFBundleVersion"] + "soha_appstore.ipa"
        cmd = "/usr/bin/xcrun -sdk iphoneos PackageApplication -v '" + soha_config["ProjectPath"] +"/soha/Build/" + appname + "' -o '" + soha_config["ReleaseFolder"] +"/"+ ipaname + "'"
    elif channel == "soha_adhoc":
        appname = "sg20.app"
        ipaname = "tlbbkdtl_"+ config["CFBundleVersion"] + "soha_adhoc.ipa"
        cmd = "/usr/bin/xcrun -sdk iphoneos PackageApplication -v '" + soha_config["ProjectPath"] +"/soha/Build/" + appname + "' -o '" + soha_config["ReleaseFolder"] +"/"+ ipaname + "'"

    print cmd
    os.system(cmd)

#编译项目并生成IPA
def buildXcodeIPA(channel):
    print "Step:buildIPA of " + channel
    buildXcodeProject(channel)
    generateIPA(channel)

#清除上次项目
def clean(prj):
    print "Step:clean"
    olddir = soha_config["ProjectPath"] + "/"+prj
    if os.path.exists(olddir):
        cmd = "rm -rf " + olddir
        os.system(cmd)

def main():
    soha_jb = "soha_jb"
    soha_appstore = "soha_appstore"
    soha_adhoc = "soha_adhoc"



    readsohaConfig()
    clean('soha_break')
    clean('soha')
    buildUnityProject()

    readConfig()
    updateXcodeSetting('soha_break')
    buildXcodeIPA(soha_jb)
    updateXcodeSetting('soha')
    buildXcodeIPA(soha_appstore)
    buildXcodeIPA(soha_adhoc)

if __name__ == '__main__':
    main()

在windows下可以类似处理,但是windows下因为不需要中间把unity项目转换成java项目,一般直接在unity下用编辑类处理即可了。

转载于:https://my.oschina.net/u/212831/blog/301667

你可能感兴趣的:(unity3d打包辅助工具)