そういうのがいいブログ

アプリ個人開発 まるブログ

アプリ開発覚え書き

【Unity】Steam向け macOS 多言語化設定 ビルド時にInfo.plistへLocalizationsを設定する方法

要件

Unity 6000.0.26f1
Localization 1.5.3
Xcode 15.4

はじめに

Steam向けにUnityでmacOSアプリを制作しており、
ビルド後に実行しても主要言語が切り替わらないという問題が起きました。

原因と対策を書きます。

原因

Info.plistに多言語化の設定がないことが原因でした。

Xcodeでいうところのinfo>Localizationsの箇所です。

対策

ビルド時に自動で多言語化の設定を追加します。

Editorという名前のフォルダを作成し、下記のスクリプトをフォルダ内に入れてください。

Unityからビルドして.appもしくは.xcodeprojファイルを生成する際に、
どちらの形式でも設定できるようにしています。

using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System.IO;
using UnityEngine;

/// <summary>
/// mac向けビルド「.app」と「.xcodeproj」における
/// info.plist に言語設定を追加するスクリプト
/// </summary>
public class AddLocalizationPostBuild
{
    [PostProcessBuild(999)]//実行順序を最後にする
    public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
    {
        // macOS ビルドでない場合は処理をスキップ
        if (target != BuildTarget.StandaloneOSX) return;

        string plistPath = "";

        // macOS .app ビルドの場合
        // .appの場合のInfo.plistへのパスは、xxx.app/Contents/Info.plist になる
        plistPath = Path.Combine(pathToBuiltProject, "Contents", "Info.plist");
     
        
        // macOS .app で Info.plist が存在するか確認
        if (!File.Exists(plistPath))
        {
            // Xcode ビルドの場合は上記のパスは存在しないため、プロジェクト名を取得して Info.plist のパスを再設定
            
            //plistPathを初期化
            plistPath = "";
            
            //プロジェクト名
            string productName = PlayerSettings.productName;
            
            // .xcodeproj の場合の Info.plist へのパスは、xxx.xcodeproj/プロジェクト名/Info.plist になる
            plistPath = Path.Combine(pathToBuiltProject, productName, "Info.plist");
            
            if (!File.Exists(plistPath))
            {
                Debug.LogError($"❌ Info.plist が見つかりません: {plistPath}");
                return;
            }
        }

        // Info.plist を読み込んで編集
        PlistDocument plist = new PlistDocument();
        plist.ReadFromString(File.ReadAllText(plistPath));

        // CFBundleLocalizations のキーを追加
        PlistElementArray localizations;
        if (plist.root.values.ContainsKey("CFBundleLocalizations"))
        {
            localizations = plist.root["CFBundleLocalizations"].AsArray();
        }
        else
        {
            localizations = plist.root.CreateArray("CFBundleLocalizations");
        }

        // 言語を追加
        AddLocalizationIfNotExists(localizations, "en"); // 英語
        AddLocalizationIfNotExists(localizations, "ja"); // 日本語

        // 変更した内容を保存
        File.WriteAllText(plistPath, plist.WriteToString());
        Debug.Log($"✅ Info.plist に CFBundleLocalizations を追加しました: {plistPath}");
    }

    // 言語がすでに存在していなければ追加
    private static void AddLocalizationIfNotExists(PlistElementArray array, string lang)
    {
        foreach (PlistElement elem in array.values)
        {
            if (elem.AsString() == lang)
                return;
        }
        array.AddString(lang);
    }
}


上記サンプルの対応言語は英語と日本語のみです。
言語を追加する場合は、下記の要領で言語を追加してください。

        // 言語を追加
        AddLocalizationIfNotExists(localizations, "en"); // 英語
        AddLocalizationIfNotExists(localizations, "ja"); // 日本語

おわりに

ChatGPTに聞いてもmacOS向けでXcodeを使用する場合の内容は
見当違いな回答が多く、少し苦戦しました。
お役に立てれば幸いです。