Skip to content

Instantly share code, notes, and snippets.

@sapphire-al2o3
Last active January 23, 2020 02:14
Show Gist options
  • Save sapphire-al2o3/5598828408034f79dffbf81ed889533c to your computer and use it in GitHub Desktop.
Save sapphire-al2o3/5598828408034f79dffbf81ed889533c to your computer and use it in GitHub Desktop.
UnityWebRequestTexture.GetTexture使ったときにテクスチャがリークするパターン
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class DownloadTextureTest : MonoBehaviour
{
[SerializeField]
string url1 = null;
[SerializeField]
string url2 = null;
// テクスチャがリークする
IEnumerator DownloadTest1(string url)
{
using (var uwr = UnityWebRequestTexture.GetTexture(url))
{
yield return uwr.SendWebRequest();
if (!uwr.isNetworkError && !uwr.isHttpError)
{
Debug.Log("test1 complete.");
}
else
{
Debug.LogError(uwr.error);
}
}
System.GC.Collect();
Resources.UnloadUnusedAssets();
}
// リークしない
IEnumerator DownloadTest2(string url)
{
using (var uwr = UnityWebRequestTexture.GetTexture(url))
{
yield return uwr.SendWebRequest();
if (!uwr.isNetworkError && !uwr.isHttpError)
{
var tex = DownloadHandlerTexture.GetContent(uwr);
Debug.Log("test2 complete.");
}
else
{
Debug.LogError(uwr.error);
}
}
System.GC.Collect();
Resources.UnloadUnusedAssets();
}
void Start()
{
StartCoroutine(DownloadTest1(url1));
StartCoroutine(DownloadTest2(url2));
}
private void OnGUI()
{
if (GUILayout.Button("Download Test1", GUILayout.Width(200), GUILayout.Height(100)))
{
StartCoroutine(DownloadTest1(url1));
}
if (GUILayout.Button("Download Test2", GUILayout.Width(200), GUILayout.Height(100)))
{
StartCoroutine(DownloadTest2(url2));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment