はじめに
前回書いた記事「Unity+VuforiaがiPhone6+でクラッシュする」では、手動で対処していたInfo.plistの修正を自動にしてみました。
コード
自動で書き換えるためには、以下のコードをAssets/Editorフォルダにおいてください。
Info.plistへのパスを決め打ちにしていますので、各自の環境にあわせて適当に書き換えてください。
なお、以下のコードは”エンジニア Blog”さんの「Unity iOSビルド時にinfo.plistに設定を自動で登録する」という記事のコードに追加・修正する部分のみのせています。最初に該当する記事のコードを参照してください。
PlistMod.cs(変更部分のみ)
using UnityEngine;
using UnityEditor; // 追加
using UnityEditor.Callbacks; // 追加
using System.IO;
using System.Xml;
public class PlistMod {
//
// (省略)
//
// 引数を若干変更しています。
public static void UpdatePlist(string path, string key, string val) {
//
// (省略)
//
// 1. 設定値
// 以下の6行を修正
if(!HasKey (dict, key)) {
AddChildElement (doc, dict, "key", key);
AddChildElement (doc, dict, "string", val);
} else {
UpdateKeyValue (dict, key, "string", val);
}
//
// (省略)
//
}
// 関数OnPostprocessBuildを追加。
// Assets/Editorフォルダに、このスクリプトをいれておくと自動でInfo.plistを書き換える。
// UIInterfaceOrientationは次の4つの値をとりうる。
// UIInterfaceOrientationPortrait
// UIInterfaceOrientationPortraitUpsideDown
// UIInterfaceOrientationLandscapeLeft
// UIInterfaceOrientationLandscapeRight
[PostProcessBuild]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) {
// "iOS"のところは、Info.plistがあるフォルダへのパス(Xcodeプロジェクトを保存したパス)を指定する。
UpdatePlist("iOS", "UIInterfaceOrientation", "UIInterfaceOrientationPortrait");
}
}