question

daksshbhambre avatar image
daksshbhambre asked

How to upload data to playfab in game

So i want to make players upload 3d models in playfab from their device, and upload it to the catalog store but i don't know how to do that and there is no tutorials on this please help.

I am using Unity and the target platform is android

Player Dataunity3ddatagame manager
10 |1200

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

Made Wang avatar image
Made Wang answered

You can refer to the following sample.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
using PlayFab.EconomyModels;
using System.Text;
using UnityEngine.Networking;

public class UGCTest : MonoBehaviour
{
    void Start()
    {
        PlayFabClientAPI.LoginWithCustomID(new LoginWithCustomIDRequest
        {
            CustomId = " "
        },
        (result) =>
        {
            Debug.Log("login success");
        },
        (error) =>
        {
            Debug.LogError(error.GenerateErrorReport());
        });
    }

    string uploadUrl;
    string uploadId;
    string itemId;
    string payloadStr;
    byte[] payload;
    /// <summary>
    /// 1.CreateUploadUrls
    /// </summary>
    public void CreateUrl()
    {
        PlayFabEconomyAPI.CreateUploadUrls(new CreateUploadUrlsRequest
        {
            Files = new List<UploadInfo>
            {
                new UploadInfo
                {
                    FileName="test.txt"
                }
            }
        }, CreateUrlsSuccess, OnError);
    }

    public void CreateUrlsSuccess(CreateUploadUrlsResponse result)
    {
        uploadUrl = result.UploadUrls[0].Url;
        uploadId = result.UploadUrls[0].Id;
        Debug.Log(result.UploadUrls[0].FileName + ":" + result.UploadUrls[0].Id + ":" + result.UploadUrls[0].Url);
    }

    /// <summary>
    /// 2.upload
    /// </summary>
    public void Upload()
    {
        ResourcesFile();
        StartCoroutine(Upload_Put());
    }

    IEnumerator Upload_Put()
    {
        UnityWebRequest request = UnityWebRequest.Put(uploadUrl, payload);
        request.SetRequestHeader("Content-Type", "text/plain");
        request.SetRequestHeader("comp", "blob");
        request.SetRequestHeader("x-ms-blob-type", "blockblob");
        yield return request.SendWebRequest();
        if(request.result==UnityWebRequest.Result.ProtocolError||request.result==UnityWebRequest.Result.ConnectionError)
        {
            Debug.LogError(request.error);
        }
        else
        {
            Debug.Log("put success");
        }
    }

    /// <summary>
    /// 3.CreateDraftItem
    /// </summary>
    public void CreateDraft()
    {
        PlayFabEconomyAPI.CreateDraftItem(new CreateDraftItemRequest
        {
            Publish = false,
            Item = new PlayFab.EconomyModels.CatalogItem
            {
                Type = "ugc",
                Title = new Dictionary<string, string>
                {
                    {
                        "neutral","UGCTest"
                    }
                },
                Description = new Dictionary<string, string>
                {
                    {
                        "neutral","A ugc item"
                    }
                },
                ContentType = "example item",
                IsHidden = false,
                Contents = new List<Content>
                {
                    new Content
                    {
                        Id=uploadId,
                        Url=uploadUrl
                    }
                }
            }
        },
        (result) =>
        {
            itemId = result.Item.Id;
            Debug.Log(result.Item.Id + ":" + result.Item.Contents[0].Id + ":" + result.Item.Contents[0].Url);
        }, OnError);
    }

    /// <summary>
    /// 4.Publish
    /// </summary>
    public void Publish()
    {
        PlayFabEconomyAPI.PublishDraftItem(new PublishDraftItemRequest
        {
            Id = itemId
        },
        (result) =>
        {
            Debug.Log("Publish Success");
        }, OnError);
    }

    /// <summary>
    /// 5.GetStatus
    /// </summary>
    public void GetPublishStatus()
    {
        PlayFabEconomyAPI.GetItemPublishStatus(new GetItemPublishStatusRequest
        {
            Id = itemId
        },
        (result) =>
        {
            Debug.Log(result.StatusMessage + ":" + result.Result);
        }, OnError);
    }

    public void OnError(PlayFabError error)
    {
        Debug.LogError(error.GenerateErrorReport());
    }

    public void ResourcesFile()
    {
        TextAsset txt = Resources.Load("test") as TextAsset;
        payloadStr = txt.text;
        payload = Encoding.UTF8.GetBytes(payloadStr);
    }
}
5 comments
10 |1200

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

daksshbhambre avatar image daksshbhambre commented ·

Ok thanks, but it uses unity Web url i want to make the user directly select file from his device and upload it.

0 Likes 0 ·
Made Wang avatar image Made Wang daksshbhambre commented ·

Uploading files to urls is by design, but UnityWebRequest is not necessary, you can upload files using whatever method you are good at. As for how to use Unity to read files in the user's device, please go to the Unity community for professional support.

0 Likes 0 ·
noelolsonxyz avatar image noelolsonxyz commented ·

What could go wrong if we call playfab's cloudscript getTime method?

-1 Like -1 ·
daksshbhambre avatar image daksshbhambre commented ·

Can you give me a link that shows how to read files from user's device

-1 Like -1 ·
Made Wang avatar image Made Wang daksshbhambre commented ·

We are not experts in Unity and recommend seeking professional support from the Unity community.

0 Likes 0 ·
Made Wang avatar image
Made Wang answered

Can you explain what it means to upload to catalog store? PlayFab doesn't provide such a feature. Did you mean UGC? If so, please refer to Publish your first user generated content - PlayFab | Microsoft Docs to implement it.

For storing files you can also use Entity File, please refer to Entity files - PlayFab | Microsoft Docs.

If you just store string/json, you can also use Player Data, refer to Quickstart Player Data - PlayFab | Microsoft Docs.

3 comments
10 |1200

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

daksshbhambre avatar image daksshbhambre commented ·

Can you please send the script to do that? I don't understand the tutorial

0 Likes 0 ·
Made Wang avatar image Made Wang daksshbhambre commented ·

Please tell me which one you need and which step you cannot understand.

0 Likes 0 ·
daksshbhambre avatar image daksshbhambre commented ·

In the ugc tutorial there are no unity c# tutorials ):

0 Likes 0 ·

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.