
iCloud for Unity let’s you put your data in the cloud as easy as A.B.C.
Available from those stores:
v2.1.1 (v2.2 pending) – Unity 3.5.7+ – Requires Unity iOS (Free or Pro) or Unity Pro
Foreword – Introduction
Quick start instructions – iOS instructions – OS X instructions
JCloudDocument – Files and directories – JCloudData – Player preferences
Foreword
iCloud for Unity is a Unity plugin for using iOS and Mac OS iCloud features in your projects. It allows you to store documents and data (in other words, files and preferences) using Apple’s iCloud storage for easy and reliable sharing between your iOS and Mac OS devices. iCloud for Unity provides you with a simple, intuitive and straight-forward application programming interface to manipulate files and folders in the cloud as well as key-value storage in the cloud. Because it implements fall backs for unsupported platforms, you can use this plugin for all your deployment targets and it will detect that iCloud is either unsupported or unavailable and will rely on local storage to continue to provide its services.
While iCloud storage is limited to Apple’s devices, it has several advantages over other cloud services. First of all, because this is Apple’s official solution, it is heavily promoted and often enabled as soon as the user activates his device for the first time. Therefore, we feel that for Apple devices this is the cloud storage solution that will have the most widespread use (fact: Apple had more than 300 million active iCloud users by Q1 2013). Once it is activated, users won’t need to provide additional login credentials or enable settings in your application to benefit from its features.
Then, contrary to other cloud services, iCloud is a system-wide feature of iOS and Mac OS, meaning you don’t have to wait for your files to be uploaded to the cloud and writing or reading files will be just as fast as local storage. This means that you can use it for anything ranging from simple preferences to save game files or high-resolution screenshots taking several megabytes (of course you want to be careful not using too much space). Your application will behave just like you’re saving your files and data locally (the actual upload and download to other devices will obviously take some time but will happen in the background even after you exit your app).
Introduction
We are providing you with two interfaces to implement iCloud storage. On the one hand, JCloudDocument is used to manipulate files and folders in the cloud with methods that matches closely File.IO and Directory.IO using straightforward function names. On the other hand, JCloudData is used to store user/player preferences in the same manner as PlayerPrefs.
Quick Start instructions
Quick-start instructions are a jump start for those that are already familiar with concepts of codesigning, provisioning profiles and entitlements and know how to configure an iOS or Mac OS project for iCloud. Should you have any issue with those instructions, you should read the in-depth detailed documentation in the following sections.
- Make sure your OS X (10.7+) installation is up to date (Apple Menu > Software Updates).
- Make sure you have Xcode installed and it is updated to the latest version (via Mac App Store, Mac/iOS Dev Center or Software Updates).
- Make sure you have a valid Mac/iOS development or release provisioning profile setup.
- You should backup and/or commit (version control) your project before you continue.
- MacOS: Use Mac App Store Toolset or Unity Entitlements Tool to add a post-process script that will codesign your app on each Mac OS build.
- iOS: Enable entitlements in the built Xcode project and pick the matching provisioning profile for code signing.
- Import iCloud Plugin package in your Unity project (untick Tutorial folder if you don’t want it).
- You can check if iCloud is enabled and working using the PollCloudDocumentAvailability and PollCloudDataAvailability methods (please note that iCloud is not available in the editor and will always fall back to local storage — you should build your app to test iCloud).
Setting up your project for iCloud on iOS
Because iCloud relies on compliance of your application with Apple requirements, you need to tweak a few settings in your Unity project so that you can effectively use iCloud for development or release purposes. If you do not set up correctly your project, iCloud won’t be enabled for your application and this plugin will fall back to local storage.
Quick Start Instructions
- Create or enable an AppID with iCloud in iOS provisioning portal.
- Create or renew a provisioning profile using this AppID in iOS provisioning portal.
- Download and install the new or updated provisioning profile.
- In your Unity project, go to Player settings for iOS and set a valid Bundle Identifier that matches the AppID pattern.
- Build your Unity project for iOS and open the resulting Xcode project.
- In Xcode, enable entitlements and iCloud for you project and make sure code signing is using the appropriate profile.
- You can now use iCloud in your Unity project. If you delete your Xcode project directory, you will need to reapply those settings in the new generated project.
Detailed Instructions
First of all, if you are not familiar with Apple development assets or don’t understand any of the next step, you should read the development assets’ section of Apple’s iOS App Development Workflow Guide. If you don’t already have one, you need to either create an AppID with iCloud enabled or enable iCloud on one of your available AppIDs. For iOS deployment, you should head to the App IDs – iOS Provisioning Portal page. Unless you are using a custom AppID, you can configure the Xcode generated wildcard AppID with iCloud.
To enable iCloud on any AppID, click ‘Configure’ under the ‘Action’ column, then enable the ‘Enable for iCloud’ checkbox and click the ‘Done’ button. Make sure you also have a valid provisioning profile that is using this AppID, as modifying AppIDs will invalidate associated profiles and you will need to renew them (to renew Xcode’s automatic wildcard provisioning profile, launch Xcode, open Organizer and under Devices tab pick any of your iOS development devices and click the ‘Add to Portal’ button). You can check, create and update provisioning profiles under the Provisioning Profiles – iOS Provisioning Portal page. Remember to download and install (drag and drop to Xcode) any updated or new provisioning profile so you can use it in your projects.
Once you have an AppID enabled for iCloud, you should check your iOS Player settings in your Unity project (Edit Menu > Project Settings > Player). Under the ‘Other Settings’ pane for iOS platform, make sure that your ‘Bundle Identifier’ matches the pattern of your AppID : if your AppID is a wildcard AppID in the form APPID.*, you can put anything here (though it is recommended you use something in the form of com.company.app) ; if it is a precise AppID such as APPID.com.mycompany.mycoolapp then your bundle identifier must be com.mycompany.mycoolapp (otherwise you won’t be able to pick the appropriate provisioning profile for building in Xcode).
You can now build your Unity project for iOS. Once your project is built and opened in Xcode, you need to make a one-time setup (unless you later delete your build directory then you’ll need to reapply those settings). In the project navigator (that lists all the files and frameworks in your project), click on the project itself (first element, title like ‘Unity-iPhone’ and subtitle like ’2 targets, iOS SDK x.y’), then in the center pane pick ‘Unity-iPhone’ under ‘Targets’ in the left-side list. On the top-side of the center pane, click ‘Summary’ from the horizontal list. Scroll down to the bottom of the center pane and enable ‘Use Entitlements file’, ‘Enable iCloud’ and ‘Use store with identifier’. Also click once the ‘+’ button in ‘Ubiquity Containers’ to add your bundle identifier. After that is done, on the top-side of the center pane, click ‘Build Settings’ from the horizontal list. Enable ‘All’ and ‘Combined’ from the horizontal list just below. Locate the ‘Code Signing’ drop-down in the list below and specifically the ‘Code Signing Identity’ drop-down contained inside. Now pick the provisioning profiles that are using our iCloud enabled AppID for Debug purposes and/or Release purposes. If the profile you are looking for is not in the list, it is either not installed on your computer or expired, or you bundle identifier does match the pattern of the associated AppID.
You can now build and run your app on your device with iCloud enabled. You can poll iCloud availability in any of your script using the PollCloudDocumentAvailability and PollCloudDataAvailability methods.
Setting up your project for iCloud on OS X
Because iCloud relies on compliance of your application with Apple requirements, you need to tweak a few settings in your Unity project so that you can effectively use iCloud for development or release purposes. If you do not set up correctly your project, iCloud won’t be enabled for your application and this plugin will fall back to local storage.
Quick Start Instructions
- Create or enable an AppID with iCloud in Mac OS Developer Certificate Utility.
- Create or renew a provisioning profile using this AppID in Mac OS Developer Certificate Utility.
- Download and install the new or updated provisioning profile (check System Preferences > Profiles).
- Use Mac App Store Toolset or Unity Entitlements Tool to enable codesigning, entitlements and sandboxing on your project.
- You can now use iCloud in your Unity project.
Detailed Instructions
First of all, if you are not familiar with Apple development assets or don’t understand any of the next step, you should read the development assets’ section of Apple’s Tools Workflow Guide for Mac. If you don’t already have one, you need to either create an AppID with iCloud enabled or enable iCloud on one of your available AppIDs. For Mac OS deployment, you should head to the App IDs – Developer Certificate Utility page. Unless you are using a custom AppID, you can configure the Xcode generated wildcard AppID with iCloud.
To enable iCloud on any AppID, click ‘Configure’ button in the AppID’s line, then enable the ‘Enable for iCloud’ checkbox. Make sure you also have a valid provisioning profile that is using this AppID, as modifying AppIDs will invalidate associated profiles and you will need to renew them (to renew Xcode’s automatic wildcard provisioning profile, launch Xcode, open Organizer and under Devices tab pick any of your Mac OS development devices and click the ‘Add to Portal’ button). You can check, create and update provisioning profiles under the Provisioning Profiles – Developer Certificate Utility page. Remember to download and install (double click on profile file) any updated or new provisioning profile so you can use it in your projects. Installed profiles should appear in Mac OS System Preferences under the Profiles category.
You’ll then need to enable code signing, entitlements and sandboxing on your Mac OS builds so that it can use iCloud. To facilitate and automate this process, we have created a paid Unity plugin called Mac App Store Toolset and a free small app called Unity Entitlements Tool that both tweak your Unity project by adding a post-process script that will automatically codesign your application on each build. Follow the tools’ instructions and make sure you pick the provisioning profile you have just configured and installed for eihter development or release purposes.
JCloudDocument – Files and Directories in the cloud
JCloudDocument class allows you to easily manipulate files and directories in the cloud. Documents are any type of file or folder contained in your application specific documents container that is hidden from standard file and directory input/output mechanisms.
Because of this, when specifing paths you must provide a relative path (starting with the name of the file or directory without ‘/’ character at the beginning) and need not to worry about path resolution to your document container.
Availability: You can test if JCloudDocument will use cloud storage with the PollCloudDocumentAvailability() method.
Fallback: When iCloud is not available or not supported on the target platform, JCloudDocument will fall back to local storage using Unity’s persistent data path. If the user enables iCloud but already has local files and directories stored using JCloudDocument, the plugin will automatically move those to the cloud as you access them with JCloudDocument.
JCloudData – PlayerPrefs using iCloud key-value store
JCloudData allows you to store small chunks of data such as user data or player preferences. It mirrors the PlayerPrefs class from Unity in its form and functionalities. Because it uses Apple’s official iCloud key-value store, it comes with some limiations on size for the entire key-value storage as well as the length of keys.
The key-value store should and will not exceed 1 MB of data, any attempt to store more data will fail ; and keys should not exceed 64 bytes in UTF-8 encoding, providing keys longer than 64 bytes will fail. In addition, you cannot store more than 1024 keys.
Availability: You can test if JCloudData will use cloud storage with the PollCloudDataAvailability() method. This will actually return true even if iCloud is disabled (on devices where it’s available) because iOS handles this automatically (it will push data to iCloud if the user enables it).
Fallback: When iCloud is not available or not supported on the target platform, JCloudData will either fall back to NSUserDefaults or PlayerPrefs.