/// <summary>
    /// Gets a random profile from the api.
    /// </summary>
    /// <param name="pUI">The ProfileUI to fill with the random info</param>
    /// <param name="index">The index to save the info at</param>
    /// <param name="scroller">A reference to the scroll script, used for saving the info</param>
    /// <returns></returns>
    public IEnumerator GetRandomProfileAPI(ProfileUI pUI, int index, ContactPersoonScroll scroller)
    {
        //Init default struct
        ProfileInfo pInfo = ProfileInfo.GetDefault();

        //Get a random person json from the api
        UnityWebRequest request = UnityWebRequest.Get("https://randomuser.me/api/?inc=name,picture&nat=nl&noinfo");

        yield return(request.SendWebRequest());

        if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
        {
            Debug.Log(request.error);
        }

        RandomUserJson randomUserJson = null;

        //Try to parse the json, can contain errors from the api if its overloaded
        try
        {
            randomUserJson = JsonUtility.FromJson <RandomUserJson>(request.downloadHandler.text);
        }
        catch
        {
            //If returned bad data from api retry
            StartCoroutine(GetRandomProfileAPI(pUI, index, scroller));
            yield break;
        }

        //Set name from api
        pInfo.name = randomUserJson.results[0].name.first + " " + randomUserJson.results[0].name.last;

        //Get the image from the given url
        request = UnityWebRequestTexture.GetTexture(randomUserJson.results[0].picture.medium);
        yield return(request.SendWebRequest());

        if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
        {
            Debug.Log(request.error);
        }
        else
        {
            Texture2D tex = ((DownloadHandlerTexture)request.downloadHandler).texture;
            pInfo.picture = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f));
        }

        //Get a random job title
        pInfo.function = GetRandomJobFunction();

        //actually update the UI and save it to the dict
        pUI.SetInfo(pInfo);
        scroller.AddToDict(index, pInfo);
        yield break;
    }
Пример #2
0
    /// <summary>
    /// Fill the given gameObject with random info, or saved info if the given index has been saved
    /// </summary>
    /// <param name="uiObject">The object to fill with info</param>
    /// <param name="index">The index of the tab</param>
    private void FillInfo(GameObject uiObject, int index)
    {
        ProfileUI pUi = uiObject.GetComponent <ProfileUI>();

        ProfileInfo pInfo = ProfileInfo.GetDefault();

        //If uses caching try to get value from the profiles dict
        if (useCaching)
        {
            //If no value is found make a new entry in the dict
            if (!profiles.TryGetValue(index, out pInfo))
            {
                StartCoroutine(ProfileRandomizer.Instance.GetRandomProfileAPI(pUi, index, this));
                pInfo = ProfileInfo.GetDefault();
            }
        }
        else
        {
            StartCoroutine(ProfileRandomizer.Instance.GetRandomProfileAPI(pUi, index, this));
        }

        pUi.SetInfo(pInfo);
    }