00:00
00:00
Newgrounds Background Image Theme

Someone gifted MetalSlayer69 supporter status!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Edits to post #26499146 by RaIix

Edited

At 5/26/20 09:46 PM, extrabeans wrote: I'm able to load a profile picture in Unity's editor, but once I test it live I'm being blocked by a CORS policy.


I'm not sure if I understand it correctly, but I don't think you personally can.

You can circumvent it in a way, using CORS proxy of some kind. Simply prefix the icon link like this:

https://cors-anywhere.herokuapp.com/uimg.ngfiles.com/profile/7985/7985576.png

Or in code:

string iconLink = "uimg.ngfiles.com/profile/7985/7985576.png";
const string CorsProxy = "https://cors-anywhere.herokuapp.com/";
string urlPrefix = (Application.platform == RuntimePlatform.WebGLPlayer) ? CorsProxy : "https://"; // don't use it in Editor
string iconUrl = $"{urlPrefix}{iconLink}"; // construct the final url

if (Uri.IsWellFormedUriString(iconUrl, UriKind.Absolute))
{
    StartCoroutine(DownloadIcon(iconUrl));
}
else Debug.LogWarningFormat("The icon url isn't valid: {0}", iconUrl);

iu_126137_4428769.png


It's probably not the best solution, but it works.


Oh, and keep in mind Unity doesn't natively support downloading GIFs – and user icons can be (static) gifs, too – so use something like Unigif in that case. For example, Tom Fulp's icon is a gif if you want to test it.


IEnumerator DownloadIcon(string iconUrl)
{
	using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(iconUrl))
	{
		yield return www.SendWebRequest();
		if (www.isNetworkError || www.isHttpError)
		{
			Debug.LogWarning(www.error);
		}
		else
		{
			// Debug.Log("Downloaded a texture at: " + iconUrl);
			Texture2D texture = null;
			if (iconUrl.EndsWith(".gif", StringComparison.OrdinalIgnoreCase))
			{
				//! Unity doesn't natively support GIF textures; only JPG and PNG
				byte[] gifBytes = www.downloadHandler.data;
				yield return StartCoroutine(UniGif.GetTextureListCoroutine(gifBytes, (gifTexList, loopCount, width, height) =>
				{
					texture = gifTexList[0].m_texture2d; // *Static* gif texture only
				}));
			}
			else
			{
				//! JPG and PNG
				texture = DownloadHandlerTexture.GetContent(www);
			}			
            // TODO: Do something with the texture now
		}
	}
}

At 5/26/20 09:46 PM, extrabeans wrote: I'm able to load a profile picture in Unity's editor, but once I test it live I'm being blocked by a CORS policy.


I'm not sure if I understand it correctly, but I don't think you personally can.

You can circumvent it in a way, using CORS proxy of some kind. Simply prefix the icon link like this:

https://cors-anywhere.herokuapp.com/uimg.ngfiles.com/profile/7985/7985576.png

Or in code:

string iconLink = "uimg.ngfiles.com/profile/7985/7985576.png";
const string CorsProxy = "https://cors-anywhere.herokuapp.com/";
string urlPrefix = (Application.platform == RuntimePlatform.WebGLPlayer) ? CorsProxy : "https://"; // don't use it in Editor
string iconUrl = $"{urlPrefix}{iconLink}"; // construct the final url

if (Uri.IsWellFormedUriString(iconUrl, UriKind.Absolute))
{
    StartCoroutine(DownloadIcon(iconUrl));
}
else Debug.LogWarningFormat("The icon url isn't valid: {0}", iconUrl);

iu_126137_4428769.png


It's probably not the best solution, but it works.


Oh, and keep in mind Unity doesn't natively support downloading GIFs – and user icons can be (static) gifs, too – so use something like Unigif in that case. For example, Tom Fulp's icon is a gif if you want to test it.


IEnumerator DownloadIcon(string iconUrl)
{
	using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(iconUrl))
	{
		yield return www.SendWebRequest();
		if (www.isNetworkError || www.isHttpError)
		{
			Debug.LogWarning(www.error);
		}
		else
		{
			// Debug.Log("Downloaded a texture at: " + iconUrl);
			Texture2D texture = null;
			if (iconUrl.EndsWith(".gif", StringComparison.OrdinalIgnoreCase))
			{
				//! Unity doesn't natively support GIF textures; only JPG and PNG
				byte[] gifBytes = www.downloadHandler.data;
				yield return StartCoroutine(UniGif.GetTextureListCoroutine(gifBytes, (gifTexList, loopCount, width, height) =>
				{
					texture = gifTexList[0].m_texture2d; // *Static* gif texture only
				}));
			}
			else
			{
				//! JPG and PNG
				texture = DownloadHandlerTexture.GetContent(www);
			}			
            // TODO: Do something with the texture now
		}
	}
}