Unity에서 Xcode 출력 후 필요한 설정을 자동화하는 방법입니다.
이번에는 잘 사용할 것 같은 것을 샘플 형식으로 정리했습니다.
■ 이용하는 기능
Unity5에서 표준 이용할 수있게되었다 XcodeAPI을 이용
Unity - Scripting API : PBXProject
■ 자동화 항목
빌드 설정 편집
프레임 워크의 추가
컴파일 플래그의 설정
· Info.plist 추가
■ 전 준비
PostProcessBuildAttribute를 이용하여 빌드 후 메소드를 호출
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.iOS.Xcode;
using UnityEditor.Callbacks;
using System.Collections;
public class XcodeSettingsPostProcesser
{
[PostProcessBuildAttribute ( 0 )]
public static void OnPostprocessBuild (BuildTarget buildTarget, string pathToBuiltProject)
{
if (buildTarget! = BuildTarget.iOS) return ;
var projectPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj" ;
PBXProject pbxProject = new PBXProject ();
pbxProject.ReadFromFile (projectPath);
string targetGuid = pbxProject.TargetGuidByName ( "Unity-iPhone" );
File.WriteAllText (projectPath, pbxProject.WriteToString ());
}
}
■ 빌드 설정 편집
pbxProject.AddBuildProperty (targetGuid, "OTHER_LDFLAGS" , "-all_load" );
pbxProject.SetBuildProperty (targetGuid, "ENABLE_BITCODE" , "NO" );
pbxProject.UpdateBuildProperty (targetGuid, "OTHER_LDFLAGS" , new string [] { "-ObjC" }, new string [] { "- weak_framework " });
■ 프레임 워크 추가
pbxProject.AddFrameworkToProject (targetGuid, "Security.framework" , false );
pbxProject.AddFrameworkToProject (targetGuid, "SafariServices.framework" , true );
■ 컴파일 플래그 설정
var guid = pbxProject.FindFileGuidByProjectPath ( "Classes / UI / Keyboard.mm" );
var flags = pbxProject.GetCompileFlagsForFile (targetGuid, guid);
flags.Add ( "-fno-objc-arc" );
pbxProject.SetCompileFlagsForFile (targetGuid, guid, flags);
■ Info.plist 추가
var plistPath = Path.Combine (pathToBuiltProject, "Info.plist" );
var plist = new PlistDocument ();
plist.ReadFromFile (plistPath);
plist.root.SetString ( "hogehogeId" , "dummyid" );
var urlDict = array.AddDict ();
urlDict.SetString ( "CFBundleURLName" , "hogehogeName" );
var urlInnerArray = urlDict.CreateArray ( "CFBundleURLSchemes" );
urlInnerArray.AddString ( "hogehogeValue" );
plist.WriteToFile (plistPath);
■ 정리
Unity5되어 설정이 상당히 쉬워졌습니다
■ 코드 전체
| using System.IO ; |
| using UnityEngine ; |
| using UnityEditor ; |
| using UnityEditor.iOS.Xcode ; |
| using UnityEditor.Callbacks ; |
| using System.Collections ; |
|
|
| public class XcodeSettingsPostProcesser |
| { |
|
|
| [PostProcessBuildAttribute ( 0 ) |
| public static void OnPostprocessBuild ( BuildTarget buildTarget , string pathToBuiltProject ) |
| { |
|
|
| // Stop processing if targe is NOT iOS |
| if (buildTarget! = BuildTarget.iOS) |
| return ; |
|
|
| // Initialize PbxProject |
| var projectPath = pathToBuiltProject + " /Unity-iPhone.xcodeproj/project.pbxproj " ; |
| PBXProject pbxProject = new PBXProject (); |
| pbxProject.ReadFromFile (projectPath); |
| string targetGuid = pbxProject.TargetGuidByName ( " Unity-iPhone " ); |
|
|
| // Sample of adding build property |
| pbxProject.AddBuildProperty (targetGuid, " OTHER_LDFLAGS " , " -all_load " ); |
|
|
| // Sample of setting build property |
| pbxProject.SetBuildProperty (targetGuid, " ENABLE_BITCODE " , " NO " ); |
|
|
| // Sample of update build property |
| pbxProject.UpdateBuildProperty (targetGuid, " OTHER_LDFLAGS " , new string [] { " -ObjC " }, new string [] { " -weak_framework " }); |
|
|
| // Sample of adding REQUIRED framwrok |
| pbxProject.AddFrameworkToProject (targetGuid, " Security.framework " , false ); |
|
|
| // Sample of adding OPTIONAL framework |
| pbxProject.AddFrameworkToProject (targetGuid, " SafariServices.framework " , true ); |
|
|
| // Sample of setting compile flags |
| var guid = pbxProject.FindFileGuidByProjectPath ( " Classes / UI / Keyboard.mm " ); |
| var flags = pbxProject.GetCompileFlagsForFile (targetGuid, guid); |
| flags.Add ( " -fno-objc-arc " ); |
| pbxProject.SetCompileFlagsForFile (targetGuid, guid, flags); |
|
|
| // Apply settings |
| File.WriteAllText (projectPath, pbxProject.WriteToString ()); |
|
|
| // Samlpe of editing Info.plist |
| var plistPath = Path.Combine (pathToBuiltProject, " Info.plist " ); |
| var plist = new PlistDocument (); |
| plist.ReadFromFile (plistPath); |
|
|
| // Add string setting |
| plist.root.SetString ( " hogehogeId " , " dummyid " ); |
|
|
| // Add URL Scheme |
| var array = plist.root.CreateArray ( " CFBundleURLTypes " ); |
| var urlDict = array.AddDict (); |
| urlDict.SetString ( " CFBundleURLName " , " hogehogeName " ); |
| var urlInnerArray = urlDict.CreateArray ( " CFBundleURLSchemes " ); |
| urlInnerArray.AddString ( " hogehogeValue " ); |
|
// Localizations [added blueasa / 2018-03-28]
// need Language Code(ref:https://ko.wikipedia.org/wiki/ISO_639)
var arrayLocalizations = plist.root.CreateArray("CFBundleLocalizations");
arrayLocalizations.AddString("en"); // 영어
arrayLocalizations.AddString("ko"); // 한국어 //arrayLocalizations.AddString("zh"); // 중국어 arrayLocalizations.AddString("zh_CN"); // 중국어(간체) : 중국
arrayLocalizations.AddString("zh_TW"); // 중국어(번체) : 대만
arrayLocalizations.AddString("ja"); // 일본어
arrayLocalizations.AddString("de"); // 독일어
|
| // Apply editing settings to Info.plist |
| plist.WriteToFile (plistPath); |
| } |
| } |
gist.github.com
[출처] http://smartgames.hatenablog.com/entry/2016/06/19/164052