question

AJ avatar image
AJ asked

Upload Photo to PlayFab files and then download it to a texture

Hi,

I would like my app users to be able to upload a photo from their phone and then show this photo as an avatar image. For this purpose I gues I need to download it to a texture. I tried using entities and follow the documentation without any success.

Could you please help me with this functionality?

entities
10 |1200

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

AJ avatar image
AJ answered

Hi @Rick Chen,

Thank you very much for pointing me in that direction. I made a research and I found the solution that I needed, using UnityWebRequest.Texture. "This function attaches a DownloadHandlerTexture object to the UnityWebRequest. DownloadHandlerTexture is a specialized Download Handler which is optimized for storing images which are to be used as Textures in the Unity Engine. Using this class significantly reduces memory reallocation compared with downloading raw bytes and creating a Texture manually in script." as per Unity's documentation.

Now my working code looks like this:

* for _profilePhoto I used RawImage UI component

However, the photo loads a bit slow after the request is made and there is a gap from 1-2sec. How could I store it in the app, something like the PlayerPrefs effect?

void GetActualFile(PlayFab.DataModels.GetFileMetadata fileData)
    {
        Debug.Log("File name: " + fileData.FileName);
        Debug.Log("File URL: " + fileData.DownloadUrl);
        GlobalFileLock += 1; // Start Each SimpleGetCall
        PlayFabHttp.SimpleGetCall(fileData.DownloadUrl,
            result => { _entityFileJson[fileData.FileName] = Encoding.UTF8.GetString(result); GlobalFileLock -= 1; },
            error => { Debug.Log(error); }
        );


        // Write the picture to a texture
        StartCoroutine(SetImage(fileData.DownloadUrl));
    }


    IEnumerator SetImage(string url)
    {
        UnityWebRequest request = UnityWebRequestTexture.GetTexture(url);
        yield return request.SendWebRequest();
        if (request.isNetworkError || request.isHttpError)
            Debug.Log(request.error);
        else
            _profilePhoto.texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
    }
1 comment
10 |1200

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

Rick Chen avatar image Rick Chen ♦ commented ·

I am glad that you have found the solution you needed. I am not an expert in Unity, as far as I know, to store a png when running on Unity Editor, you could use the code example I provided previously (and modify the line 7 in your code snippet). But to store the photo in your App, please search for Unity's document or community for help. For example, you could refer to this Unity thread: https://forum.unity.com/threads/help-with-access-write-files-on-android.72819/

0 Likes 0 ·
Rick Chen avatar image
Rick Chen answered

You mentioned that you tried using entities and follow the documentation without any success. Which document have you followed? Is it the Entity files? Please note that the code sample only showed you how to get binary file and encode the binary into string, not image. Please search for unity forum about how to save the binary into an image file. For example, to write the binary into a png file you could use the following code in Unity:

using System.IO;
File.WriteAllBytes(Application.dataPath + "/SavedScreen.png", your_binary);

Where “your_binary” is the binary you retrieved from entity files.

1 comment
10 |1200

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

omerdruu avatar image omerdruu commented ·

Saving the binary to image file and then downloading it becomes high storage and takes about 20-30 seconds while downloading. Any chance I can save the binary as a string and then convert it to png while downloading? how should i do this? I followed Entity files document.

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.