# Continous Integretion for Unity

## 起因

* 公司有 Unity 專案，需輸出 iOS project 並打包成 ipa 的需求，ipa 有可能是Enterprise program(for BU 測試用)，以及 Developer program(上架到 AppStore)
* 測試期間常重複上述建置動作，相當繁瑣且耗時，思考將流程自動化

## 建置流程如下

1. Unity build iOS project
2. Set iOS project configure, Such as Code sign, Provision Profile, Build Settings etc
3. Export ipa for Enterprise or Appstore

## Unity build iOS project

* Unity 有 API 專門在處理 iOS Build，先將 Builder.cs 和 XcodeProjectConfigurator.cs 這兩個檔案放到 Unity 專案下的 Assets/Editor裡
  * [Unity iOS で fastlane を使って ipa をビルドする（前編）](http://blog.kakeragames.com/2016/04/16/unity-ios-fastlane.html)
  * [Unity - Scripting API: BuildPipeline.BuildPlayer](https://docs.unity3d.com/ScriptReference/BuildPipeline.BuildPlayer.html)
* 進行指令輸出，這邊要注意的是 -projectPath，Folder名稱不能有空白

  ```
  /Applications/Unity/Unity.app/Contents/MacOS/Unity \
  -projectPath "/path/to/your/Product" \
  -quit \
  -batchmode \
  -executeMethod "Builder.BuildiOS"
  ```
* 輸出過程不順利，遇到 Plugins coliding with each other 的錯誤訊息，更慘的是第一時間完全沒有 Console 可以看 log～Or2，好在有 log 檔，在 \~/Library/Logs/Unity/Editor.log
  * [Unity Build若出錯，查log的地方](https://docs.unity3d.com/Manual/LogFiles.html)
* 查到關鍵的錯誤訊息，解決方法是去調整該檔的 plugin settings，把OS改成OSX就可以了，x64和x86下的VuforiaWrapper.dll都要調

```
Plugin 'VuforiaWrapper.dll' is used from several locations:
Assets/Plugins/x64/VuforiaWrapper.dll would be copied to <PluginPath>/VuforiaWrapper.dll
Assets/Plugins/x86/VuforiaWrapper.dll would be copied to <PluginPath>/VuforiaWrapper.dll

Please fix plugin settings and try again.
```

![](https://1934061725-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4XrCDvjc3xNfiB-3eT%2F-M4Y-TstkTXMD9Y8W9l7%2F-M4Y03bTm9y0g5PHoO16%2Fpluginsetting.png?alt=media\&token=2bc7972f-bf27-48fe-a88c-531df4eefd26)

* [Error when building: Plugins colliding with each other](https://github.com/mixpanel/mixpanel-unity/issues/9)

## Set iOS project configure

* Builder.cs 負責 Unity 建置 iOS 專案，在這個檔案裡我額外加了一個設定，把 AutomaticSigning 的功能關掉

  ```c
  PlayerSettings.iOS.appleEnableAutomaticSigning = false;
  ```
* XcodeProjectConfigurator.cs 在 Build 完之後，針對 iOS build property 進行設定，例如Code Sign 和 Provision\_Profile

```c
project.SetBuildProperty(target, "CODE_SIGN_IDENTITY", "iPhone Distribution: XXXXXXXX CO.,LTD.");
project.SetBuildProperty(target, "PROVISIONING_PROFILE", "6999xxxx-1b0f-4c19-be86-62bbb3d6xxxx");
```

* [Unity iOS 省心打包（二）](https://www.jianshu.com/p/dbd7c4b205b0)

## Export ipa for Enterprise or Appstore

* 在這一階段，我們將之前建置 iOS project 的這段功能擴充到 fastlane，再和 gym 一起使用，就可以將所有步驟整合起來，照著下面網頁教學步驟，很順利的就完成了
  * [Unity iOS で fastlane を使って ipa をビルドする（後編）](http://blog.kakeragames.com/2016/06/04/unity-ios-fastlane.html)
  * [在 fastlane 擴充新的 action: unity](https://github.com/thedoritos/flunity/tree/master/fastlane/actions)

```ruby
desc "Submit a new Beta Build"
  desc "This will also make sure the profile is up to date"
  lane :beta do
    unity(
      execute_method: 'Builder.BuildiOS'
    )    

  provisioningProfiles={"tw.com.xxx.EnterpriseTestApp": "EnterpriseTestApp_InHouse_2018"}

    gym(
        project: './Build/Unity-iPhone.xcodeproj',
        scheme: 'Unity-iPhone',
        export_method: 'enterprise',
        export_options:{provisioningProfiles: provisioningProfiles},
        output_directory: './Build'
    )
```
