question

babbacorn avatar image
babbacorn asked

Method doen't not get called trying to download a file from CDN using Unity.

Using the CDN to deliver a json file for new levels to a game as and when they are available and a method never gets called. (Any ideas why?)

This is where I am calling the Login method from a call which makes the game world.

 void Awake ()


    {
        PlayFabHelpers.Login     
    }


Below is the class which holds the login method and that seems to work but I have a break point within the DownLoadFile method but it never hits it for some reason. Been looking at this for days and can't solve it.

So any ideas why DownLoadFile from below never gets called ?

using PlayFab;
using PlayFab.ClientModels;
using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;


public static class PlayFabHelpers
{
    public static void Login()
    {
        if (string.IsNullOrEmpty(PlayFabSettings.staticSettings.TitleId))
        {
              PlayFabSettings.staticSettings.TitleId = "blablabla";
        }
        var request = new LoginWithCustomIDRequest { CustomId = "TestGuy", CreateAccount = true };
        PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnLoginFailure);
               
    }


    private static void OnLoginSuccess(LoginResult result)
    {
        GetDownloadUrl("Levels.json", OnSuccess);
    }


    private static 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());
    }


    private static void OnSuccess(string obj)
    {
        DownloadFile(obj);
    }


    public static void AuthenticateWithPlayFab()
    {
        Debug.Log("Logging into PlayFab...");
        LoginWithCustomIDRequest request = new LoginWithCustomIDRequest() { TitleId = PlayFabSettings.staticSettings.TitleId, CustomId = SystemInfo.deviceUniqueIdentifier, CreateAccount = true };
        PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnLoginFailure, null);
    }


    public static void DownloadFileFromCDN(string key)
    {
        GetDownloadUrl(key, presignedUrl =>
        {
            Console.WriteLine("");
        });
    }


    static void GetDownloadUrl(string key, Action<string> onComplete)
    {
        PlayFabClientAPI.GetContentDownloadUrl(new GetContentDownloadUrlRequest()
        {
            Key = key,
            ThruCDN = true
        }, result => onComplete(result.URL),
        error => Debug.LogError(error.GenerateErrorReport()));
    }


    private static IEnumerator DownloadFile(string thePath)
    {
        var uwr = new UnityWebRequest(thePath, UnityWebRequest.kHttpVerbGET);
        string path = Path.Combine(Application.persistentDataPath, "Levels.json");
        uwr.downloadHandler = new DownloadHandlerFile(path);
        yield return uwr.SendWebRequest();
        if (uwr.isNetworkError || uwr.isHttpError)
            Debug.LogError(uwr.error);
        else
            Debug.Log("File successfully downloaded and saved to " + path);
    }
}


10 |1200

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

Hernando avatar image
Hernando answered

Coroutines actually run on MonoBehaviour instances.

You should call a coroutine use StartCoroutine() method, and StartCoroutine()is a member function of MonoBehaviour so you need to pass a reference of MonoBehaviour instance to your Login() mothod, and pass it to OnSuccess() method that is used to call the coroutine DownloadFile(). More information please see: C# Coroutines in static functionsIEnumerator

Following code show how to pass the MonoBehaviour instance, it may be helpful for you:

using PlayFab;
using PlayFab.ClientModels;
using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
 
 
public static class PlayFabHelpers
{
     static public MonoBehaviour  instance; //the instance of our class that will do the work

    public static void Login(MonoBehaviour justToStartCoroutine)
    {
        instance = justToStartCoroutine;

        if (string.IsNullOrEmpty(PlayFabSettings.staticSettings.TitleId))
        {
              PlayFabSettings.staticSettings.TitleId = "blablabla";
        }
        var request = new LoginWithCustomIDRequest { CustomId = "TestGuy", CreateAccount = true };
        PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnLoginFailure);
               
    }
 
 
    private static void OnLoginSuccess(LoginResult result)
    {
        GetDownloadUrl("Levels.json", OnSuccess);
    }
 
 
    private static 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());
    }
 
 
    private static void OnSuccess(string obj)
    {
        instance.StartCoroutine(DownloadFile(obj));
    }
 
 
    public static void AuthenticateWithPlayFab()
    {
        Debug.Log("Logging into PlayFab...");
        LoginWithCustomIDRequest request = new LoginWithCustomIDRequest() { TitleId = PlayFabSettings.staticSettings.TitleId, CustomId = SystemInfo.deviceUniqueIdentifier, CreateAccount = true };
        PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnLoginFailure, null);
    }
 
 
    public static void DownloadFileFromCDN(string key)
    {
        GetDownloadUrl(key, presignedUrl =>
        {
            Console.WriteLine("");
        });
    }
 
 
    static void GetDownloadUrl(string key, Action<string> onComplete)
    {
        PlayFabClientAPI.GetContentDownloadUrl(new GetContentDownloadUrlRequest()
        {
            Key = key,
            ThruCDN = true
        }, result => onComplete(result.URL),
        error => Debug.LogError(error.GenerateErrorReport()));
    }
 
 
    private static IEnumerator DownloadFile(string thePath)
    {
        var uwr = new UnityWebRequest(thePath, UnityWebRequest.kHttpVerbGET);
        string path = Path.Combine(Application.persistentDataPath, "Levels.json");
        uwr.downloadHandler = new DownloadHandlerFile(path);
        yield return uwr.SendWebRequest();
        if (uwr.isNetworkError || uwr.isHttpError)
            Debug.LogError(uwr.error);
        else
            Debug.Log("File successfully downloaded and saved to " + path);
    }
}

10 |1200

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

babbacorn avatar image
babbacorn answered

Many thanks for your input, I can pass an instance of MonoBehaviour into the login method but I am not sure how to pass that onto the other methods if they are successful

So

Login is called as below

 void Awake ()


    {
        PlayFabHelpers.Login(this);    
    }

and the new Playfab helper code is as below. I can't work out how to pass justToStartCoroutine to OnLoginSuccess then onto OnSucess then onto DownloadFile . . .

using PlayFab;
using PlayFab.ClientModels;
using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;


public class PlayFabHelpers: MonoBehaviour
{
        


    public static void Login(MonoBehaviour justToStartCoroutine)
    {
        if (string.IsNullOrEmpty(PlayFabSettings.staticSettings.TitleId))
        {
              PlayFabSettings.staticSettings.TitleId = "FE59A";
        }
        var request = new LoginWithCustomIDRequest { CustomId = "TestGuy", CreateAccount = true };
        PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnLoginFailure);
               
    }


    private static void OnLoginSuccess(LoginResult result)
    {
        GetDownloadUrl("Levels.json", OnSuccess);
    }


    private static 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());
    }


    private static void OnSuccess(string obj)
    {
        DownloadFile(obj);
    }


    public static void AuthenticateWithPlayFab()
    {
        Debug.Log("Logging into PlayFab...");
        LoginWithCustomIDRequest request = new LoginWithCustomIDRequest() { TitleId = PlayFabSettings.staticSettings.TitleId, CustomId = SystemInfo.deviceUniqueIdentifier, CreateAccount = true };
        PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnLoginFailure, null);
    }


    public static void DownloadFileFromCDN(string key)
    {
        GetDownloadUrl(key, presignedUrl =>
        {
            Console.WriteLine("");
        });
    }


    static void GetDownloadUrl(string key, Action<string> onComplete)
    {
        PlayFabClientAPI.GetContentDownloadUrl(new GetContentDownloadUrlRequest()
        {
            Key = key,
            ThruCDN = true
        }, result => onComplete(result.URL),
        error => Debug.LogError(error.GenerateErrorReport()));
    }


    private static IEnumerator DownloadFile(string thePath)
    {
        var uwr = new UnityWebRequest(thePath, UnityWebRequest.kHttpVerbGET);
        string path = Path.Combine(Application.persistentDataPath, "Levels.json");
        uwr.downloadHandler = new DownloadHandlerFile(path);
        yield return uwr.SendWebRequest();
        if (uwr.isNetworkError || uwr.isHttpError)
            Debug.LogError(uwr.error);
        else
            Debug.Log("File successfully downloaded and saved to " + path);
    }
}


10 |1200

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

Hernando avatar image
Hernando answered
using PlayFab;
using PlayFab.ClientModels;
using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
 
 
public static class PlayFabHelpers
{
     static public MonoBehaviour  instance; 

    public static void Login(MonoBehaviour justToStartCoroutine)
    {
        instance = justToStartCoroutine;

        if (string.IsNullOrEmpty(PlayFabSettings.staticSettings.TitleId))
        {
              PlayFabSettings.staticSettings.TitleId = "blablabla";
        }
        var request = new LoginWithCustomIDRequest { CustomId = "TestGuy", CreateAccount = true };
        PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnLoginFailure);
               
    }
 
 
    private static void OnLoginSuccess(LoginResult result)
    {
        GetDownloadUrl("Levels.json", OnSuccess);
    }
 
 
    private static 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());
    }
 
 
    private static void OnSuccess(string obj)
    {
        instance.StartCoroutine(DownloadFile(obj));
    }
 
 
    public static void AuthenticateWithPlayFab()
    {
        Debug.Log("Logging into PlayFab...");
        LoginWithCustomIDRequest request = new LoginWithCustomIDRequest() { TitleId = PlayFabSettings.staticSettings.TitleId, CustomId = SystemInfo.deviceUniqueIdentifier, CreateAccount = true };
        PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnLoginFailure, null);
    }
 
 
    public static void DownloadFileFromCDN(string key)
    {
        GetDownloadUrl(key, presignedUrl =>
        {
            Console.WriteLine("");
        });
    }
 
 
    static void GetDownloadUrl(string key, Action<string> onComplete)
    {
        PlayFabClientAPI.GetContentDownloadUrl(new GetContentDownloadUrlRequest()
        {
            Key = key,
            ThruCDN = true
        }, result => onComplete(result.URL),
        error => Debug.LogError(error.GenerateErrorReport()));
    }
 
 
    private static IEnumerator DownloadFile(string thePath)
    {
        var uwr = new UnityWebRequest(thePath, UnityWebRequest.kHttpVerbGET);
        string path = Path.Combine(Application.persistentDataPath, "Levels.json");
        uwr.downloadHandler = new DownloadHandlerFile(path);
        yield return uwr.SendWebRequest();
        if (uwr.isNetworkError || uwr.isHttpError)
            Debug.LogError(uwr.error);
        else
            Debug.Log("File successfully downloaded and saved to " + path);
    }
}

This code show how to pass the MonoBehaviour instance, it may be helpful for you.

10 |1200

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

babbacorn avatar image
babbacorn answered

Working now , thanks very much for your help

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.