低レベルAPI

ウォレットを取得

ここからは Unity での実装になります。

仮想通貨の所持数を取得します。

public class GameLogic : MonoBehaviour {

    private const string CLIENT_ID = "your client id";
    private const string CLIENT_SECRET = "your client secret";

    private const string MONEY_NAME = "money-0001";

    Gs2.Client gs2;
    Gs2.GameSession session;

    void Start () {
        gs2 = new Gs2.Client (new Gs2.Profile ()
            .WithClientId (CLIENT_ID)
            .WithClientSecret (CLIENT_SECRET));

        StartCoroutine (Login ());
    }

    IEnumerator Login() {
        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);
        yield return gs2.Auth.Login (
            result => {
                if(result.Error != null) throw result.Error;
                session = result.Result;
             },
            USER_ID,
            KEY_NAME,
            authenticationToken);

        StartCoroutine (Initialize ());
     }

     IEnumerator Initialize()
     {
          while(gs2.Money.Status != Status.INITIALIZED) {
               yield return gs2.Money.Dispatch(
                    result => {
                         if(result.Error != null) throw result.Error;
                    },
                    session,
                    MONEY_NAME);
               Thread.Sleep(100);
          }

        StartCoroutine (GetWallet ());
    }

    IEnumerator GetWallet()
    {
        Gs2.Money.Wallet wallet = null;
        yield return gs2.Money.GetWallet(
            result => {
                if(result.Error != null) throw result.Error;
                wallet = result.Result;
            },
            session,
            MONEY_NAME,
            0 // スロット番号
        );
        Debug.Log("userId: " + wallet.UserId + ", slot: " + wallet.Slot + ", paid: " + wallet.Paid + ", free: " + wallet.Free);
    }

    void Update () {

    }
}

販売中の商品(仮想通貨)一覧を取得

販売している商品(仮想通貨)一覧を取得します。

この情報を利用してUIを組み立てて、次にサンプルを示す購入に繋げてください。

public class GameLogic : MonoBehaviour {

    private const string CLIENT_ID = "your client id";
    private const string CLIENT_SECRET = "your client secret";

    private const string MONEY_NAME = "money-0001";

    Gs2.Client gs2;
    Gs2.GameSession session;

    void Start () {
        gs2 = new Gs2.Client (new Gs2.Profile ()
            .WithClientId (CLIENT_ID)
            .WithClientSecret (CLIENT_SECRET));

        StartCoroutine (Login ());
    }

    IEnumerator Login() {
        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);
        yield return gs2.Auth.Login (
            result => {
                if(result.Error != null) throw result.Error;
                session = result.Result;
             },
            USER_ID,
            KEY_NAME,
            authenticationToken);

        StartCoroutine (Initialize ());
     }

     IEnumerator Initialize()
     {
          while(gs2.Money.Status != Status.INITIALIZED) {
               yield return gs2.Money.Dispatch(
                    result => {
                         if(result.Error != null) throw result.Error;
                    },
                    session,
                    MONEY_NAME);
               Thread.Sleep(100);
          }
          Product[] products = gs2.Money.ListItem();
        foreach(var product in products) {
            Debug.Log("title: " + product.metadata.localizedTitle + ", description: " + product.metadata.localizedDescription + ", price: " + product.metadata.localizedPriceString);
        }
     }

    void Update () {

    }
}

仮想通貨の購入

商品を指定して、商品に設定された数量の仮想通貨を購入します。

内部処理で 各プラットフォームのレシート検証と仮想通貨の付与処理が実行されます。

public class GameLogic : MonoBehaviour {

    private const string CLIENT_ID = "your client id";
    private const string CLIENT_SECRET = "your client secret";

    private const string MONEY_NAME = "money-0001";

    Gs2.Client gs2;
    Gs2.GameSession session;

    void Start () {
        gs2 = new Gs2.Client (new Gs2.Profile ()
            .WithClientId (CLIENT_ID)
            .WithClientSecret (CLIENT_SECRET));

        StartCoroutine (Login ());
    }

    IEnumerator Login() {
        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);
        yield return gs2.Auth.Login (
            result => {
                if(result.Error != null) throw result.Error;
                session = result.Result;
             },
            USER_ID,
            KEY_NAME,
            authenticationToken);

        StartCoroutine (Initialize ());
     }

     IEnumerator Initialize()
     {
          while(gs2.Money.Status != Status.INITIALIZED) {
               yield return gs2.Money.Dispatch(
                    result => {
                         if(result.Error != null) throw result.Error;
                    },
                    session,
                    MONEY_NAME);
               Thread.Sleep(100);
          }
          Product[] products = gs2.Money.ListItem();
          StartCoroutine (Buy (products[0]));
     }

     IEnumerator Buy(Product product)
     {
          gs2.Money.Buy(0, product);
          bool purchaseComplete = false;
          Gs2.Money.Wallet wallet = null;
          while(!purchaseComplete) {
               yield return gs2.Money.Dispatch(
                    result => {
                         Debug.Log(result.Error);
                         if(result.Error != null) throw result.Error;
                         if(result.Result.Type == Gs2.Money.EventType.PURCHASE_COMPLETE) {
                              purchaseComplete = true;
                              wallet = result.Result.Wallet;
                         }
                    },
                    session,
                    MONEY_NAME);
               Thread.Sleep(100);
           }
          Debug.Log("userId: " + wallet.UserId + ", slot: " + wallet.Slot + ", paid: " + wallet.Paid + ", free: " + wallet.Free);
     }

    void Update () {

    }
}

仮想通貨を消費

本来、仮想通貨の消費は対価の引き渡しと同時にサーバサイドで実行するべきです。

なぜなら、チート行為によって仮想通貨の消費ロジックを回避し、対価だけ得ることが出来てしまうからです。

GS2 はガチャ・ストアなど、仮想通貨の消費をしつつ対価を渡せるサービスを拡充する予定です。

(サーバサイドSDKを使用して、仮想通貨の消費・対価の付与をすればGS2の対応を待つ必要はありません)

public class GameLogic : MonoBehaviour {

    private const string CLIENT_ID = "your client id";
    private const string CLIENT_SECRET = "your client secret";

    private const string MONEY_NAME = "money-0001";

    Gs2.Client gs2;
    Gs2.GameSession session;

    void Start () {
        gs2 = new Gs2.Client (new Gs2.Profile ()
            .WithClientId (CLIENT_ID)
            .WithClientSecret (CLIENT_SECRET));

        StartCoroutine (Login ());
    }

    IEnumerator Login() {
        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);
        yield return gs2.Auth.Login (
            result => {
                if(result.Error != null) throw result.Error;
                session = result.Result;
             },
            USER_ID,
            KEY_NAME,
            authenticationToken);

        StartCoroutine (Initialize ());
     }

    IEnumerator Initialize()
    {
        while(gs2.Money.Status != Status.INITIALIZED) {
            yield return gs2.Money.Dispatch(
                result => {
                     if(result.Error != null) throw result.Error;
                },
                session,
                MONEY_NAME);
        }

        StartCoroutine (GetWallet ());
    }

    IEnumerator GetWallet()
    {
        Gs2.Money.Wallet wallet = null;
        yield return gs2.Money.GetWallet(
            result => {
                if(result.Error != null) throw result.Error;
                wallet = result.Result;
            },
            session,
             MONEY_NAME,
            0 // スロット番号
        );

        Debug.Log("[Before] userId: " + wallet.UserId + ", slot: " + wallet.Slot + ", paid: " + wallet.Paid + ", free: " + wallet.Free);

        StartCoroutine (ConsumeWallet ());
    }

    IEnumerator ConsumeWallet()
    {
        Gs2.Money.Wallet wallet = null;
        yield return gs2.Money.ConsumeWallet(
            result => {
                if(result.Error != null) throw result.Error;
                wallet = result.Result;
            },
            session,
            MONEY_NAME,
            0, // スロット番号
            100, // 消費量
            0, // 用途番号(用途ごとの消費割合を集計できます)
            false // 有償仮想通貨のみ消費対象とするか(有償仮想通貨でしか買えない商品の場合は true を指定します)
            );

        Debug.Log("[After] userId: " + wallet.UserId + ", slot: " + wallet.Slot + ", paid: " + wallet.Paid + ", free: " + wallet.Free);
    }

    void Update () {

    }
}