- Home /
Question by
sakurahime796 · May 05, 2021 at 05:11 PM ·
c#loading during runtimeperfomance
Game Freeze 2 second when use Sprite.Create
When i use the method Sprite.Create Unity Freeze 2 / 4 seconds if the image is large, i try to transform the Texture into a Sprite in a Thread, but can't use the method in it.
¿How can I perform these operations in the background if I can't use c# threads?
¿Someone can help me?
private UnityEngine.UI.Image image;
private IEnumerator coroutine;
private void OnTriggerExit(Collider other)
{
if (other.GetComponent<CustomClass>())
{
if (coroutine != null) StopCoroutine(coroutine);
coroutine = GetText(path);
StartCoroutine(coroutine);
}
}
public IEnumerator GetText(string path)
{
using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(path)) //
{
yield return uwr.SendWebRequest();
if (uwr.result != UnityWebRequest.Result.Success)
{
Debug.Log(uwr.error);
}
else
{
Texture2D texture = DownloadHandlerTexture.GetContent(uwr);
image.sprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100.0f);
}
}
}
Comment