カスタムオブジェクトを使用する。
WIP 書き途中
Cloud Fire Storeではデータを扱いやすくするためにカスタムオブジェクトを作成して使用することができる。こちらの基本的には公式サイトが充実しているのでそちらを参照してくれればよい。
// クラスにつける属性 [FirestoreData] public class City { // プロパティにつける属性 [FirestoreProperty] public string Name { get; set; } [FirestoreProperty] public string State { get; set; } [FirestoreProperty] public string Country { get; set; } [FirestoreProperty] public bool Capital { get; set; } [FirestoreProperty] public long Population { get; set; } }
使用可能の型はこちらに記載されている。基本的な型は大抵使えるが多次元配列を使えないのが注意が必要。一次元配列に落とすか、Maoなどを使う必要がある。 データを保存するためのカスタムオブジェクトクラスと、実際にアプリで使用するクラスは別にしてコンバーターを作るのが良さそうである。コンバータークラスを作るもいいし内部で処理を書いてもよい。
public class City { public CityInGame Convert() { CityInGame cityInGame = new CityInGame(); // 初期化する return cityInGame; } }
拡張メソッドを定義する方法もある。どちらいいけど使いやすいように設計すると良さそう。ゲーム内と全く同じクラスを作るのはあまり好きではない。。。
public class CityExtension { public static CityInGame ConvertToGame(this City self) { CityInGame cityInGame = new CityInGame(); // 初期化する return cityInGame; } }
データの追加は比較的単純に行える。
// インスタンスの取得 FirebaseFirestore db = FirebaseFirestore.DefaultInstance; // ドキュメントへの参照を取得 DocumentReference docRef = db.Collection("cities").Document("LA"); City city = new City { Name = "Los Angeles", State = "CA", Country = "USA", Capital = false, Population = 3900000L }; docRef.SetAsync(city);
まとえm
WIP