Skip to content

Commit

Permalink
UnityWebRequest replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaina committed Jun 15, 2021
1 parent 2c2f5e1 commit 95600fe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
11 changes: 8 additions & 3 deletions Facebook.Unity/Facebook.Unity/Results/GraphResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,25 @@ namespace Facebook.Unity
{
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

internal class GraphResult : ResultBase, IGraphResult
{
internal GraphResult(WWW result) : base(new ResultContainer(result.text), result.error, false)
internal GraphResult(UnityWebRequestAsyncOperation result) :
base(new ResultContainer(result.webRequest.downloadHandler.text), result.webRequest.error, false)
{

this.Init(this.RawResult);

// The WWW object will throw an exception if accessing the texture field and
// an error has occured.
if (result.error == null)
if (string.IsNullOrEmpty(result.webRequest.error))
{
// The Graph API does not return textures directly, but a few endpoints can
// redirect to images when no 'redirect=false' parameter is specified. Ex: '/me/picture'
this.Texture = result.texture;

this.Texture = new Texture2D(2, 2);
this.Texture.LoadImage(result.webRequest.downloadHandler.data);
}
}

Expand Down
21 changes: 11 additions & 10 deletions Facebook.Unity/Facebook.Unity/Utils/AsyncRequestString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace Facebook.Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

/*
* A short lived async request that loads a FBResult from a url endpoint
Expand Down Expand Up @@ -81,7 +82,7 @@ internal class AsyncRequestString : MonoBehaviour

internal IEnumerator Start()
{
WWW www;
UnityWebRequestAsyncOperation webRequestOperation;
if (this.method == HttpMethod.GET)
{
string urlParams = this.url.AbsoluteUri.Contains("?") ? "&" : "?";
Expand All @@ -93,14 +94,13 @@ internal IEnumerator Start()
}
}

Dictionary<string, string> headers = new Dictionary<string, string>();

UnityWebRequest webRequest = UnityWebRequest.Get(url + urlParams);
if (Constants.CurrentPlatform != FacebookUnityPlatform.WebGL)
{
headers["User-Agent"] = Constants.GraphApiUserAgent;
webRequest.SetRequestHeader("User-Agent", Constants.GraphApiUserAgent);
}

www = new WWW(this.url + urlParams, null, headers);
webRequestOperation = webRequest.SendWebRequest();
}
else
{
Expand Down Expand Up @@ -128,18 +128,19 @@ internal IEnumerator Start()
this.query.headers["User-Agent"] = Constants.GraphApiUserAgent;
}

www = new WWW(this.url.AbsoluteUri, this.query);
UnityWebRequest webRequest = UnityWebRequest.Post(url.AbsoluteUri, query);
webRequestOperation = webRequest.SendWebRequest();
}

yield return www;
yield return webRequestOperation;

if (this.callback != null)
{
this.callback(new GraphResult(www));
this.callback(new GraphResult(webRequestOperation));
}

// after the callback is called, www should be able to be disposed
www.Dispose();
// after the callback is called, web request should be able to be disposed
webRequestOperation.webRequest.Dispose();
MonoBehaviour.Destroy(this);
}

Expand Down

0 comments on commit 95600fe

Please sign in to comment.