消費型アイテムの管理を GS2 SDK for Unity から利用する手順を解説します。
アイテムプールを作成する
GS2マネージメントコンソールの GS2-ConsumableItem から新しくアイテムプールを作成します。 アイテムプールは複数種類のアイテムの所持数をまとめて管理する単位です。
アイテムプールの名前をつけます。 サービスクラスは想定されるアクセス数に応じたサイズを選択します。
続けて、管理したいアイテムを登録します。
「アイテム」タブを選択し、「アイテムの新規作成」を選択します。
アイテムの付与・消費時に指定することになる名前と最大所持数を指定します。
これで、GS2マネージメントコンソールでおこなう準備は完了です。
消費型アイテム処理を実装
ここからは Unity での実装になります。 まずはログイン処理を実装する必要があります。ログインして Gs2.GameSession を取得するまでの手順は GS2-Auth をご参照ください。 ここからはログインが完了している前提で説明を進めます。
所持アイテム一覧を取得
- public class GameLogic : MonoBehaviour {
-
- private const string CLIENT_ID = "your client id";
- private const string CLIENT_SECRET = "your client secret";
- Gs2.Client gs2;
- Gs2.GameSession session;
- void Start () {
- // GS2 Client を初期化
- gs2 = new Gs2.Client (new Gs2.Profile ()
- .WithClientId (CLIENT_ID)
- .WithClientSecret (CLIENT_SECRET));
-
- StartCoroutine (Login ());
- }
- IEnumerator Login() {
- // GS2-Account を利用して認証します
- string GAME_NAME = "game-0001";
- string KEY_NAME = "account";
- string USER_ID = "user";
- string PASSWORD = "password";
- string authenticationToken = null;
- yield return gs2.Account.Authentication (
- result => {
- if(result.Error != null) throw result.Error;
- authenticationToken = result.Result;
- },
- GAME_NAME,
- KEY_NAME,
- USER_ID,
- PASSWORD);
- // GS2-Account の認証情報を利用して GS2 にログインします
- yield return gs2.Auth.Login (
- result => {
- if(result.Error != null) throw result.Error;
- session = result.Result;
- },
- USER_ID,
- KEY_NAME,
- authenticationToken);
- StartCoroutine (GetInventory ());
- }
- IEnumerator GetInventory() {
- // インベントリの内容を取得
- yield return gs2.ConsumableItem.GetInventory(
- r => {
- if(r.Error != null) throw r.Error;
- var itemName = r.Result[0].ItemName; // アイテム名
- var count = r.Result[0].Count; // 所持数量
- },
- session,
- "itemPool-0001"); // アイテムプール名
- }
- void Update () {
- }
- }
アイテムを消費 - public class GameLogic : MonoBehaviour {
-
- private const string CLIENT_ID = "your client id";
- private const string CLIENT_SECRET = "your client secret";
- Gs2.Client gs2;
- Gs2.GameSession session;
- void Start () {
- // GS2 Client を初期化
- gs2 = new Gs2.Client (new Gs2.Profile ()
- .WithClientId (CLIENT_ID)
- .WithClientSecret (CLIENT_SECRET));
-
- StartCoroutine (Login ());
- }
- IEnumerator Login() {
- // GS2-Account を利用して認証します
- string GAME_NAME = "game-0001";
- string KEY_NAME = "account";
- string USER_ID = "user";
- string PASSWORD = "password";
- string authenticationToken = null;
- yield return gs2.Account.Authentication (
- result => {
- if(result.Error != null) throw result.Error;
- authenticationToken = result.Result;
- },
- GAME_NAME,
- KEY_NAME,
- USER_ID,
- PASSWORD);
- // GS2-Account の認証情報を利用して GS2 にログインします
- yield return gs2.Auth.Login (
- result => {
- if(result.Error != null) throw result.Error;
- session = result.Result;
- },
- USER_ID,
- KEY_NAME,
- authenticationToken);
- StartCoroutine (ListMessage ());
- }
- IEnumerator GetInventory() {
- // アイテムを消費
- yield return gs2.ConsumableItem.ConsumeItem(
- r => {
- if(r.Error != null) throw r.Error;
- },
- session,
- "itemPool-0001", // アイテムプール名
- "item-0001", // 消費するアイテム名
- 1 // 消費数量
- );
- }
- void Update () {
- }
- }
|
|