Skip to content

Instantly share code, notes, and snippets.

@Aronkey
Created June 9, 2018 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aronkey/67af1b8631c50a8e95e9fa8aa2d323dc to your computer and use it in GitHub Desktop.
Save Aronkey/67af1b8631c50a8e95e9fa8aa2d323dc to your computer and use it in GitHub Desktop.
PlayFab files test
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using PlayFab;
using PlayFab.ClientModels;
using PlayFab.EntityModels;
using PlayFab.Internal;
public class PlayFabFileTest : MonoBehaviour
{
public RawImage image;
public Texture2D texture;
public string titleID;
public string customID;
public string fileName;
private PlayFab.EntityModels.EntityKey entityKey;
private List<string> names;
public void Test()
{
Login();
}
private void Login()
{
names = new List<string>() { fileName };
var request = new LoginWithCustomIDRequest
{
CreateAccount = true,
LoginTitlePlayerAccountEntity = true,
TitleId = titleID,
CustomId = customID
};
PlayFabClientAPI.LoginWithCustomID(request, OnReceivedLoginResult, OnPlayFabError);
}
private void OnReceivedLoginResult(LoginResult result)
{
entityKey = new PlayFab.EntityModels.EntityKey
{
Id = result.EntityToken.Entity.Id,
TypeString = result.EntityToken.Entity.TypeString
};
var request = new InitiateFileUploadsRequest
{
Entity = entityKey,
FileNames = names
};
PlayFabEntityAPI.InitiateFileUploads(request, OnInitiatedFileUpload, OnPlayFabError);
}
private void OnInitiatedFileUpload(InitiateFileUploadsResponse response)
{
PlayFabHttp.SimplePutCall(response.UploadDetails[0].UploadUrl, texture.EncodeToPNG(), OnFileUploaded, OnPlayFabError);
}
private void OnFileUploaded()
{
var request = new FinalizeFileUploadsRequest
{
Entity = entityKey,
FileNames = names
};
PlayFabEntityAPI.FinalizeFileUploads(request, OnFinalizedFileUpload, OnPlayFabError);
}
private void OnFinalizedFileUpload(FinalizeFileUploadsResponse response)
{
LoadFile();
}
private void LoadFile()
{
var request = new GetFilesRequest
{
Entity = entityKey
};
PlayFabEntityAPI.GetFiles(request, OnInitiatedFileDownload, OnPlayFabError);
}
private void OnInitiatedFileDownload(GetFilesResponse response)
{
PlayFabHttp.SimpleGetCall(response.Metadata[names[0]].DownloadUrl, OnFileDownloaded, OnPlayFabError);
}
private void OnFileDownloaded(byte[] data)
{
var texture = new Texture2D(1, 1);
texture.LoadImage(data);
image.texture = texture;
}
private void OnPlayFabError(string error)
{
Debug.Log(error);
}
private void OnPlayFabError(PlayFabError error)
{
Debug.Log(error.GenerateErrorReport());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment