
iCloud for Unity let’s you put your data in the cloud as easy as A.B.C.
Available from those stores:
v2.0.2 – Unity 3.5.7+ – Requires Unity iOS (Basic or Pro) or Unity Pro
Simple file manipulation with text contents
This example demonstrates straight-forward methods to write contents into a text file, read it back and then delete it.
// Use this variable to store our text file contents for display
string results = "";
// Basic OnGUI buttons for triggering our methods and label for results displaying
void OnGUI() {
if (GUILayout.Button("Save to Text File")) {
StartCoroutine(SaveFile());
}
if (GUILayout.Button("Read from Text File")) {
StartCoroutine(LoadFile());
}
if (GUILayout.Button("Delete Text File")) {
StartCoroutine(DeleteFile());
}
// Display results
GUILayout.Label("Results : " + results);
}
// SaveFile uses JCloudDocument.FileWriteAllBytes
// Please note we do not yield as we don't bother checking if it succeeded here
IEnumerator SaveFile() {
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
byte[] textBytes = encoder.GetBytes("Some simple text.");
JCloudDocument.FileWriteAllBytes("mytextfile.txt", textBytes);
}
// LoadFile uses JCloudDocument.FileReadAllBytes
IEnumerator LoadFile() {
JCloudDocumentOperation operation;
// Let's check file exists first
operation = JCloudDocument.FileExists("mytextfile.txt");
while (operation.finished == false) {
yield return null; // Wait for 1 frame
}
if ((bool)operation.result == false) {
results = "Text file does not exist.";
yield break; // yield break ends the coroutine here
}
// Wait for file reading
operation = JCloudDocument.FileReadAllBytes("mytextfile.txt");
while (operation.finished == false) {
results = "Opening text file (" + operation.progress + "%)";
yield return null; // Wait for 1 frame
}
// Get the result
if (operation.success)
{
byte[] textBytes = operation.result as byte[];
// Make sure we read something
if (textBytes != null) {
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
results = encoder.GetString(textBytes);
} else {
results = "Text file is empty.";
}
} else {
results = "Failed to read text file.";
}
}
// DeleteFile uses JCloudDocument.FileDelete
// Please note we do not yield as we don't bother cheking if it succeeded here
IEnumerator DeleteFile() {
JCloudDocument.FileDelete("mytextfile.txt");
}
Simple preferences settings with key-value store
This example sets an integer key, reads it back and then deletes it.
// Use this variable to store our power status
int powerStatus = 0;
// Basic OnGUI buttons for triggering our methods and label for results displaying
void OnGUI() {
// Set Power Status uses JCloudData.SetInt and JCloudData.Save
if (GUILayout.Button("Set Power Status")) {
JCloudData.SetInt("PowerStatus", 9000);
JCloudData.Save();
}
// Get Power Status uses JCloudData.GetInt
if (GUILayout.Button("Get Power Status")) {
powerStatus = JCloudData.GetInt("PowerStatus");
}
// Reset Power Status uses JCloudData.DeleteKey and JCloudData.Save
if (GUILayout.Button("Reset Power Status")) {
JCloudData.DeleteKey("PowerStatus");
JCloudData.Save();
powerStatus = 0;
}
// Display results
GUILayout.Label("Power status is over " + powerStatus);
}