Unity-iOS project export Xcode automatic construction method

The basic process of Unity-iOS publishing first exports the Xcode project in Unity, and then sets up some processes of IOS packaging in the Xcode project, such as importing lib, framework or other resources, setting signatures and other compilation settings, adding compilation scripts, and so on.

If these operations are performed manually after exporting Xcode every time, it will waste time and be error-prone. At present, there are mainly the following methods to automatically set up Xcode projects:

  1. Unity sets the Xcode project settings through the PostProcessBuild callback when exporting the Xcode project
  2. Another method is to set it through the ruby ​​library xcodeproj. This method is separated from the Unity project, and it is a way to specifically set up the xcode project.

After automatically setting up the Xcode project, we can use the xcodebuild command to automatically build the Xcode project, and use ExportOptions.plist to configure the process of automatically releasing the ipa or official package. Through such a whole set of processes, the overall automated packaging process of Xcode can be realized. This article mainly talks about the automatic configuration of the Xcode project. If you think it is necessary, I will talk about the process of exporting ipa later.

Xcode project configuration file

Before talking about automatic builds, let's briefly go through the Xcode project configuration file. If your Unity project imports the SDK through CocoaPods, we need to open *.xcworkspace, which contains two xcodeproj

  1.  Unity-iPhone is the project exported by our Unity
  2. Pods is the pod project we installed using pod install

If we still need to set the Pods project settings, such as setting the signature of the framework or some other settings, there is no way to use the callback of Unity. The pod installation can only be generated in the PodFile after the xcode project is exported by unity. Set during the Pod project or use the ruby ​​library xcodeproj to set it uniformly.

The *.pbxproj file in *.xcodeproj records all our Xcode project settings. You can open the project.pbxproj file under Unity-iPhone.xcodeproj to view all project settings.

Analysis of pbxproj project structure

Xcode reads the rootObject value, obtains the uuid of the project, and parses the entire project configuration starting from the project. This file is easy to understand, and the comments are very clear. This is very convenient when we need to set the value later, we only need to refer to it.

isa is equivalent to the type, we can easily query the modified interface in  xcodeproj

Generic way to implement automatic setup

Determine what to modify

The premise of automatic build is to be able to manually configure the build, which is very convenient to understand where we need to configure automatic build information. For example, if I want to add a shell script in Build Phase, I can add it manually in Xcode first, and then pass The git comparison tool is very convenient to see the modified content.

 We can easily find out that a script object has been added to the buildPhases array in PBXNativeTarget. This method is very simple and direct for novices, and the medicine can cure the disease

Find the right tools (interfaces) to implement the functionality

use xcodeproj

require "xcodeproj"
# @添加shell脚本
# 打开项目
project_path = "Unity-iPhone.xcodeproj"
# PBXProject
project = Xcodeproj::Project.open(project_path)
#@PBXNativeTarget
$pbx_native_target = project.targets[0]
build_phases = $pbx_native_target.build_phases
# 生成一个uuid
uuid = project.generate_uuid
puts "uuid===>" + uuid
# 设置脚本属性
script = Xcodeproj::Project::Object::PBXShellScriptBuildPhase.new(project,uuid)
script.name = "test shell"
script.shell_path = "/bin/sh"
script.shell_script = "ruby test.rb"
script.run_only_for_deployment_postprocessing = "0"
script.always_out_of_date = "1"
# 插入位置
build_phases.insert(3,script)
# 保存项目
project.save

 We can know that the ShellScript type is PBXShellScriptBuildPhase through the comment, we can add shellscript by creating this object, and then the values ​​inside can be set by us after manual setting

Use the unity callback method to set

Add scripts through PBXProject->AddShellScriptBuildPhase, but this way of setting is not as much as using ruby. Therefore, it is recommended to use the xcodeproj library to configure the project if possible.

Guess you like

Origin blog.csdn.net/weixin_36719607/article/details/128179021