question

mohamed-sunoob avatar image
mohamed-sunoob asked

How to Show user currency in my UI? Please refer the code below and i want see in "GoldText"

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




public class PlayFabLogin : MonoBehaviour
{


    public GetPlayerCombinedInfoRequestParams Info;
    public ItemPlayfab[] Items;
    public GameObject Buttonpref;
    public GameObject Container;
    public int Gold = 0;
    public Text GoldText;
    public void Start()
    {
        //Note: Setting title Id here can be skipped if you have set the value in Editor Extensions already.
        if (string.IsNullOrEmpty(PlayFabSettings.TitleId))
        {
            PlayFabSettings.TitleId = "EC30D"; // Please change this value to your own titleId from PlayFab Game Manager
        }
        var requestAndroid = new LoginWithAndroidDeviceIDRequest { AndroidDeviceId = ReturnMobileID(), CreateAccount = true };
        PlayFabClientAPI.LoginWithAndroidDeviceID(requestAndroid, OnLoginMobileSuccess, OnLoginMobileFailure);
        requestAndroid.InfoRequestParameters = Info;


        //PlayFabClientAPI.LoginWithAndroidDeviceID(requestAndroid, result =>
        //{
        //    Gold = result.InfoResultPayload.UserVirtualCurrency["GC"];
          
        //}, error => { });


       
      




       //PlayFabClientAPI.LoginWithAndroidDeviceID(requestAndroid, OnLoginMobileSuccess =>
        //{
        //    Gold = OnLoginMobileSuccess.InfoResultPayload.UserVirtualCurrency["GC"];
        //}, error => { });












        //PlayFabClientAPI.GetPlayerCombinedInfo(requestAndroid, (result) =>
        //{
        //    Gold = result.InfoResultPayload.UserVirtualCurrency["GC"];
        //});









        //var request = new LoginWithCustomIDRequest { CustomId = "GettingStartedGuide", CreateAccount = true };
        //PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnLoginFailure);


        foreach (ItemPlayfab i in Items)
        {
            GameObject o = Instantiate(Buttonpref,Container.transform.position, Quaternion.identity);
            o.transform.GetChild(0).GetComponent<Text>().text = i.Name;
            o.transform.GetChild(1).GetComponent<Text>().text = i.Cost.ToString();/*+ '\n'+i.Cost;*/
            o.GetComponent<Image>().sprite = i.GetComponent<SpriteRenderer>().sprite;
            o.GetComponent<Image>().preserveAspect = true;
            o.transform.SetParent(Container.transform);
        }
    }




    public void AddVirtualCurrencyinAirwar()
    {
        AddUserVirtualCurrencyRequest request = new AddUserVirtualCurrencyRequest();
        request.VirtualCurrency = "GC";


        request.Amount = Gold;
        GoldText.text = request.Amount + "";
    }






    private void OnLoginMobileSuccess(LoginResult result)
    {
       
       
        Debug.Log("Congratulations, you made your first successful API call!");
        // loginPanel.SetActive(false);


    }






    private void OnLoginMobileFailure(PlayFabError error)
    {
        Debug.Log(error.GenerateErrorReport());
    }


    public static string ReturnMobileID()
    {
        string deviceID = SystemInfo.deviceUniqueIdentifier;
        return deviceID;
    }




    private void OnLoginSuccess(LoginResult result)
    {
        Debug.Log("Congratulations, you made your first successful API call!");
        
    }


    private void OnLoginFailure(PlayFabError error)
    {
        Debug.LogWarning("Something went wrong with your first API call.  :(");
        Debug.LogError("Here's some debug information:");
        Debug.LogError(error.GenerateErrorReport());
    }
     void Update()
    {
       GoldText.text = ""+Gold;
       // AddVirtualCurrencyinAirwar();
    }
}
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

Please refer to the following code, besides, you can check the API reference Authentication - Login With Android Device ID for more details of this API.

...
 private void OnLoginMobileSuccess(LoginResult result)
    {
        Debug.Log("Congratulations, you made your first successful API call!");
       
        foreach (var item in result.InfoResultPayload.UserVirtualCurrency)
        {
            if (item.Key == "GC")
            {
                Gold = item.Value;
            }
            
        } 
    }

...
  void Update()
    {
        GoldText.text = "" + Gold;
    }
...
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.