Last active
August 29, 2015 14:12
-
-
Save kankikuchi/d3b8e128ab8747c53430 to your computer and use it in GitHub Desktop.
Unity+Parseのサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using Parse; | |
public class SampleParse : MonoBehaviour { | |
//入力用UI | |
public UIInput StringInput, IntInput; | |
//Parseに保存するオブジェクト名 | |
private const string PARSE_OBJECT_NAME = "SampleClass"; | |
//Parseに保存するカラム名 | |
private const string PARSE_COLUMN_NAME_1 = "Column1"; | |
private const string PARSE_COLUMN_NAME_2 = "Column2"; | |
/// <summary> | |
/// ParseのDBにデータを保存する | |
/// </summary> | |
public void Save(){ | |
ParseObject parseObject = new ParseObject (PARSE_OBJECT_NAME); | |
//入力された値をパースオブジェクトに設定、PARSE_COLUMN_NAME_1はstring、PARSE_COLUMN_NAME_2はint型 | |
parseObject[PARSE_COLUMN_NAME_1] = StringInput.value; | |
parseObject[PARSE_COLUMN_NAME_2] = int.Parse (IntInput.value); | |
parseObject.SaveAsync ().ContinueWith(task => { | |
if(CheckTask("Save", task)){ | |
//保存が成功するとObjectIdに一意の文字列が設定される | |
Debug.Log("ObjectId : " + parseObject.ObjectId); | |
} | |
}); | |
} | |
/// <summary> | |
/// ParseのDBからデータを読み込み、ログで表示 | |
/// </summary> | |
public void Load(){ | |
//クエリ作成 | |
ParseQuery<ParseObject> query = new ParseQuery<ParseObject> (PARSE_OBJECT_NAME); | |
//クエリを投げる | |
query.FindAsync().ContinueWith(task => | |
{ | |
if(CheckTask("Load", task)){ | |
//取得した値をログで表示する | |
IEnumerable<ParseObject> results = task.Result; | |
foreach(ParseObject parseObject in results){ | |
Debug.Log(parseObject[PARSE_COLUMN_NAME_1]); | |
Debug.Log(parseObject[PARSE_COLUMN_NAME_2]); | |
} | |
} | |
}); | |
} | |
//タスクのチェックを行い、タスク成功時のみTrueを返す | |
private bool CheckTask(string taskName, System.Threading.Tasks.Task task){ | |
if (task.IsCanceled) | |
{ | |
Debug.Log(taskName + "キャンセル"); | |
} | |
else if (task.IsFaulted) | |
{ | |
Debug.Log(taskName + "失敗"); | |
//エラーメッセージ | |
using (IEnumerator<System.Exception> enumerator = task.Exception.InnerExceptions.GetEnumerator()) { | |
if (enumerator.MoveNext()) { | |
ParseException error = (ParseException) enumerator.Current; | |
Debug.Log(error.Message); | |
} | |
} | |
} | |
else | |
{ | |
Debug.Log(taskName + "成功"); | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment