Eduardo Coelho
Eduardo Coelho

Reputation: 1963

Localize iOS App name in Unity

I'm'developing a Unity3D game that shows a different (localized) app name in the iPhone's home screen according to the user local language. Note that:

  1. I already know how to localize the iOS app name by editing the Xcode project (create a InfoPlist.string file, localize it, add the CFBundleDisplayName key to it, etc.)

  2. I also know how automatically localize an Android app name within the Unity editor (add a values-XX.xml file with the app_name property onto Assets/Plugins/Android/res/ folder, etc.)

The question is: how can I automatically localize my iOS app name within the Unity Editor so that I don't need to perform the error-prone task 1. every time I build the project?

I think that PostprocessBuildPlayer should be the way to go, however I haven't found any documentation on how to parse it and/or modify the Xcode project file correctly to achieve this.

Upvotes: 4

Views: 5390

Answers (1)

Kay
Kay

Reputation: 13146

Long time ago I ran into trouble when I tried to modify info.plist via the Build Player Pipeline especially when doing it in Append mode. It works only once and then subsequent builds fail with "The data couldn’t be read because it isn’t in the correct format." (s. Unity forum posts like this one and my blog posting about this problem) So I decided to take the alternative way combining a customised build with an Xcode Build Pre-action.

Three steps are required:

(1) Xcode setup:

In Xcode go to Edit Scheme / Build / Pre-actions. Then click the + sign to add a New Run Script Action.

  • In Provide build settings select Unity-iPhone.
  • Paste . ${PROJECT_DIR}/modify_info_plist.sh (note the dot and blank at the beginning, is ensures that the script is executed in the caller's shell)

So it should look like this: Xcode screenshot

(2) Script modify_info_plist.sh:

Within your script you have access to all environmet variables from Xcode (s. Xcode Build Setting Reference) and you can manipulate Info.plist using the defaults command (man page). Here is a sample I used to add gyroscope to the UIRequiredDeviceCapabilities:

# Code snippet used in Unity-iPhone scheme as "Build Pre-Action"

my_domain=${PROJECT_DIR}/Info.plist
status_bar_key=UIViewControllerBasedStatusBarAppearance

logger "Start adding keys to info.plist"
defaults write $my_domain $status_bar_key -boolean NO

if [ `defaults read $my_domain UIRequiredDeviceCapabilities | grep "gyroscope" | wc -l` = "0" ]; then
    defaults write $my_domain UIRequiredDeviceCapabilities -array-add  "gyroscope"
fi
logger "Keys added to info.plist successfully"

(3) Build Pipeline:

Put the following code in a static editor class to create a new menu item Tools / My iOS Build with shortcut cmd+alt+b:

static string IOSBuildDir= "Develop";

[MenuItem("Tools/My iOS Build %&b")]
public static void IOSBuild () {
    string[] levels = { "Assets/Scenes/Boot.unity",
        "Assets/Scenes/Level-1.unity",
        // ...
        "Assets/Scenes/Menu.unity"
    };
    string path = Directory.GetCurrentDirectory ();
    path += "/" + IOSBuildDir + "/Info.plist";
    if (File.Exists (path)) {
        Debug.Log ("Removing file " + path);
        File.Delete (path);
    }
    BuildPipeline.BuildPlayer (levels, "Develop", BuildTarget.iPhone, 
        BuildOptions.AcceptExternalModificationsToPlayer);
}

I know this is no perfect solution but it's the only one I found to work stable. Two drawbacks:

  • Step (1) has to be repeated after major Xcode format changes
  • New scenes have to be appended in the editor class code in step (3)

Upvotes: 1

Related Questions