Upload
ryota-murohoshi
View
471
Download
0
Embed Size (px)
Citation preview
Parse• MBaaS(Mobile,Backend,as,a,Service)
• データストアとかユーザー管理とかできる
• UnityのSDKがあって簡単に試せる
細かいところは各自ググってください
次のようなクラスの情報をParseで保存しますpublic class Person{ public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public int Sex { get; set; }}
普通にデータを保存するときParseObject gameScore = new ParseObject("Person"){ {"firstName", "Ryota"}, {"lastName", "Murohoshi"}, {"age", 26}, {"sex", 0}, // 男性};
Task saveTask = gameScore.SaveAsync(); // 保存
// 細かいところは各自(略)
こんな感じのサブクラスを作って使う[ParseClassName("Person")]public class Person : ParseObject{ [ParseFieldName("firstName")] public string FirstName { get { return GetProperty<string>("FirstName"); } set { SetProperty<string>(value, "FirstName"); } }
/* 略、他3個のプロパティ */
}
// 細かいところは(略)
ParseObjectのサブクラスを作る• 基本的に同じようなものの繰り返し
• サブクラス作る時にうっかり文字を間違えてしまいそう
• なにより長い、そしてだるい
• こんなクラスをいくつも作るのは非常にだるい
自己紹介• 名前:室星亮太
• 所属:Fuller,)Inc.
• 仕事:Unityを用いたゲーム開発
• 投稿先:h3p://qiita.com/RyotaMurohoshi
• Twi3er:@RyotaMurohoshi
• コミュニティ:Androidの会Unity部
T4とはVisual'Studio'の'T4'テキスト'テンプレートは、テキスト'ファイルを生成できる、テキスト'ブロックと制御ロジックが混在するファイルです。
コード生成とT4テキストテンプレート#h%ps://msdn.microso2.com/ja5jp/library/bb126445.aspx#より
ざっくりとT4
• テキストテンプレート
• コード生成に超便利
• C#などで書いた制御ロジックと出力したいテキストをまとめる
• Visual*Studioからめっちゃ使いやすい
• 使いやすく導入楽
• Xamarinでも使えた
T4を新規作成するとこんな感じ<#@ template language="C#" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Text" #><#@ import namespace="System.Collections.Generic" #>
付け加えると、<#@ template language="C#" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Text" #><#@ import namespace="System.Collections.Generic" #>
public class HelloT4{}
こんな感じで書くと、<#@ template language="C#" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Text" #><#@ import namespace="System.Collections.Generic" #>
public class HelloT4{<# foreach(int num in Enumerable.Range(0, 3)) { #> ここにフィールドを定義したい!<# } #>}
ちょっと改造して!<#@ template language="C#" #><#@ assembly name="System.Core" #><#@ import namespace="System.Linq" #><#@ import namespace="System.Text" #><#@ import namespace="System.Collections.Generic" #>
public class HelloT4{<# foreach(int num in Enumerable.Range(0, 3)) { #> public const int Num<#= num #> = <#= num #>;<# } #>}
繰り返しを用いたコード生成public class HelloT4{ public const int Num0 = 0; public const int Num1 = 1; public const int Num2 = 2;}
これをT4でつくるで![ParseClassName("Person")]public class Person : ParseObject{ [ParseFieldName("firstName")] public string FirstName { get { return GetProperty<string>("FirstName"); } set { SetProperty<string>(value, "FirstName"); } }
/* 略、他3個のプロパティ */}
こんな感じのテンプレートを用意して!namespace ParseObjects{using Parse;
<# foreach (var definedClass in definedClasses) { #> [ParseClassName("<#= definedClass.ClassName #>")] public class <#= definedClass.ClassName #> : ParseObject { <# foreach (var property in definedClass.Propeties) { string typeName = property.Type.ToString (); string fieldName = ToLowerCamel (property.Name); #> [ParseFieldName("<#= fieldName#>")] public <#= typeName #> <#= property.Name #> { get { return GetProperty<<#= typeName #> > ("<#= property.Name #>"); } set { SetProperty<<#= typeName #> > (value, "<#= property.Name #>"); } } <# } #> }
<# } #>}
こんな感じの元データを定義して!(複数クラス対応!)
var definedClasses = new [] { new { ClassName = "Person", Propeties = new [] { new { Type = typeof(string), Name = "FirstName" }, new { Type = typeof(string), Name = "LastName" }, new { Type = typeof(int), Name = "Age" }, new { Type = typeof(int), Name = "Sex" }, } }, new { ClassName = "PlayLog", Propeties = new [] { new { Type = typeof(int), Name = "StageId" }, new { Type = typeof(int), Name = "Score" }, new { Type = typeof(DateTime), Name = "PlayedAt" }, } },};
できた![ParseClassName("Person")]public class Person : ParseObject{ [ParseFieldName("firstName")] public string FirstName { get { return GetProperty<string>("FirstName"); } set { SetProperty<string>(value, "FirstName"); } }
/* 略、他3個のプロパティ */}