Advertisement
thieumao

Send/receive Player Data (Title) with PlayFab Unity Client

Jun 29th, 2016
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using PlayFab;
  7. using PlayFab.ClientModels;
  8.  
  9. public class PlayFabManager : MonoBehaviour
  10. {
  11.     public string PlayfabID = "D29F";
  12.     public string CustomAccountID = "";
  13.  
  14.     public static PlayFabManager Instance;
  15.  
  16.     void Awake()
  17.     {
  18.         DontDestroyOnLoad(this);
  19.         Instance = this;
  20.         PlayFabSettings.TitleId = PlayfabID;
  21.  
  22.         Login("iduser");
  23.     }
  24.  
  25.     [ContextMenu("Login PlayFab")]
  26.     public void Login(string CustomAccountLogin)
  27.     {
  28.         print("Login with " + CustomAccountLogin);
  29.  
  30.         LoginWithCustomIDRequest request = new LoginWithCustomIDRequest()
  31.         {
  32.             TitleId = PlayfabID,
  33.             CreateAccount = true,
  34.             CustomId = CustomAccountLogin
  35.         };
  36.  
  37.         PlayFabClientAPI.LoginWithCustomID(
  38.             request,
  39.             (result) =>
  40.             {
  41.                 CustomAccountID = result.PlayFabId;
  42.                 Debug.Log("Got PlayFabID: " + CustomAccountLogin);
  43.                 if (result.NewlyCreated)
  44.                 {
  45.                     Debug.Log("(new account)");
  46.                     setDisplayName("Name Display In PlayFab");
  47.                 }
  48.                 else
  49.                 {
  50.                     Debug.Log("(existing account)");
  51.                 }
  52.                 // up test
  53.                 Dictionary<string, string> listUserData = new Dictionary<string, string>();
  54.                 listUserData.Add("keytest1", "Noi dung test 1");
  55.                 listUserData.Add("keytest2", "Noi dung test 2");
  56.                 UpData(listUserData);
  57.                 // down test
  58.                 DownData(null);
  59.             },
  60.             (error) =>
  61.             {
  62.                 Debug.Log("Error logging in player with custom ID:");
  63.                 Debug.Log(error.ErrorMessage);
  64.             }
  65.         );
  66.     }
  67.  
  68.     private void setDisplayName(String displayName)
  69.     {
  70.         UpdateUserTitleDisplayNameRequest request = new UpdateUserTitleDisplayNameRequest()
  71.         {
  72.             DisplayName = displayName
  73.         };
  74.  
  75.         PlayFabClientAPI.UpdateUserTitleDisplayName(
  76.             request,
  77.             (result) =>
  78.             {
  79.                 print("Current name in playfab: " + result.DisplayName);
  80.             },
  81.             (error) =>
  82.             {
  83.                 Debug.Log(error.ErrorMessage);
  84.             }
  85.         );
  86.     }
  87.  
  88.     private void UpData(Dictionary<string, string> listKeyValue)
  89.     {
  90.         UpdateUserDataRequest request = new UpdateUserDataRequest()
  91.         {
  92.             Data = listKeyValue
  93.         };
  94.  
  95.         PlayFabClientAPI.UpdateUserData(
  96.             request,
  97.             (result) =>
  98.             {
  99.                 Debug.Log("Successfully updated user data");
  100.             },
  101.             (error) =>
  102.             {
  103.                 Debug.Log("Got error setting user data Ancestor to Arthur");
  104.                 Debug.Log(error.ErrorDetails);
  105.             }
  106.         );
  107.     }
  108.  
  109.     // if listKey = null then down all
  110.     private void DownData(List<string> listKey)
  111.     {
  112.         GetUserDataRequest request = new GetUserDataRequest()
  113.         {
  114.             PlayFabId = CustomAccountID,
  115.             Keys = listKey
  116.             //Keys = new List<string>(){"hightscore",...}
  117.         };
  118.  
  119.         PlayFabClientAPI.GetUserData(
  120.             request,
  121.             (result) =>
  122.             {
  123.                 if ((result.Data == null) || (result.Data.Count == 0))
  124.                 {
  125.                     Debug.Log("No user data available");
  126.                 }
  127.                 else
  128.                 {
  129.                     foreach (var item in result.Data)
  130.                     {
  131.                         Debug.Log("    " + item.Key + " == " + item.Value.Value);
  132.                     }
  133.                 }
  134.             },
  135.             (error) =>
  136.             {
  137.                 Debug.Log("Got error retrieving user data:");
  138.                 Debug.Log(error.ErrorMessage);
  139.             }
  140.         );
  141.     }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement