PlayFabとSteamworks使ってみたよー2

前回やったことを少し変更してみました。

PlayerDataというスクリプトを作成し、このように書きました。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
using Steamworks;

public class PlayerData : MonoBehaviour
{
   [Header("Data")]
   public string playerName;
   [Header("PlayFab")]
   public GetPlayerCombinedInfoRequestParams info;
   public string myPlayFabId;
   // Start is called before the first frame update
   void Start()
   {
       
   }

   // Update is called once per frame
   void Update()
   {
       
   }
}

今現在はこれだけで大丈夫です。これをDataというオブジェクトを作成し、そこに入れます。

画像1

このようになるはずです。次に前回のLoginSystemを変更します。

LoginSystemの全体のコードを先に表示します。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
using Steamworks;
using System;
using System.Text;

public class LoginSystem : MonoBehaviour
{
   [SerializeField] private PlayerData playerData;

   


   private void Awake()
   {

       SteamLogin();
       //playerDataを取得していなかったらDataオブジェクトから取得する
       if (playerData == null) GameObject.Find("Data").GetComponent<PlayerData>();
   }

   // Start is called before the first frame update
   void Start()
   {
       
   }

   // Update is called once per frame
   void Update()
   {
       
   }

   //Steamでログインする
   void SteamLogin()
   {

       if (SteamManager.Initialized)
       {
           PlayFabClientAPI.LoginWithSteam(new LoginWithSteamRequest
           {
               CreateAccount = true,
               SteamTicket = GetSteamAuthTicket(),
               InfoRequestParameters = playerData.info,
           }, result =>
           {
               //プレイヤー名をSteamの名前から取得
               playerData.playerName = SteamFriends.GetPersonaName();
               //PlayFabIdを使う機会があるので取得しとく
               playerData.myPlayFabId = result.PlayFabId;
               Debug.Log("ログイン成功!");
               
           }
           , error => { Debug.Log("ログイン失敗... " + error.GenerateErrorReport()); });
       }
   }

   public string GetSteamAuthTicket()
   {
       byte[] ticketBlob = new byte[1024];

       // Retrieve ticket; hTicket should be a field in the class so you can use it to cancel the ticket later
       // When you pass an object, the object can be modified by the callee. This function modifies the byte array you've passed to it.
       HAuthTicket hTicket = SteamUser.GetAuthSessionTicket(ticketBlob, ticketBlob.Length, out uint ticketSize);

       // Resize the buffer to actual length
       Array.Resize(ref ticketBlob, (int)ticketSize);

       // Convert bytes to string
       StringBuilder sb = new StringBuilder();
       foreach (byte b in ticketBlob)
       {
           sb.AppendFormat("{0:x2}", b);
       }
       return sb.ToString();
   }

}

変わったコードは以下の通りです。長文のところは変わった位置に※をつけます。LoginSystemのinfoは削除しました。

[SerializeField] private PlayerData playerData;
private void Awake()
   {

       SteamLogin();
       //playerDataを取得していなかったらDataオブジェクトから取得する
       *if (playerData == null) GameObject.Find("Data").GetComponent<PlayerData>();
   }
if (SteamManager.Initialized)
{
    PlayFabClientAPI.LoginWithSteam(new LoginWithSteamRequest
    {
        CreateAccount = true,
        SteamTicket = GetSteamAuthTicket(),
        InfoRequestParameters = *playerData.info,
    }, result =>
           {
               //プレイヤー名をSteamの名前から取得
               *playerData.playerName = SteamFriends.GetPersonaName();
               //PlayFabIdを使う機会があるので取得しとく
               *playerData.myPlayFabId = result.PlayFabId;
               Debug.Log("ログイン成功!");
               
           }
           , error => { Debug.Log("ログイン失敗... " + error.GenerateErrorReport()); });
}

このように変更致しました。次回はインベントリとか作ってみたいな!ではでは!

この記事が気に入ったらサポートをしてみませんか?