Search Unity

Unity 2019 freezes on parallel UnityWebRequest's

Discussion in '2D' started by undefined666, Sep 18, 2019.

  1. undefined666

    undefined666

    Joined:
    Apr 3, 2018
    Posts:
    44
    This all started from the release of 2019 version
    I have webgl project.
    It contains scroll view(ScrollRect) with lots of images inside.After image enters the viewport it sends request via UnityWebRequest to fetch its content.There are maximum 15 images in the viewport simultaneously.My measurements show the first image receives its data after ~0.5sec and all the rest got it after ~2.5sec.During loading all ui noticeable lags.The problem occurs only in the browser.
    But if I compile same project with Unity 2018 all images load content in ~0.5sec as expected and there are no ui freezes at all!.Here is loader class:
    Code (CSharp):
    1. public class AlphaImage : MonoBehaviour
    2. {
    3.     public RawImage Image;
    4.     private string loadedUrl;
    5.     private IEnumerator deferredRoutine;
    6.     private UnityWebRequest www;
    7.     public void Fetch(string url) {
    8.         if (url == null) {
    9.             Image.texture = null;
    10.             return;
    11.         }
    12.         if (loadedUrl != null && loadedUrl.Equals(url))
    13.             return;
    14.         www?.Abort();
    15.         StopAllCoroutines();
    16.         var routine = load(url);
    17.         if (!gameObject.activeInHierarchy || !gameObject.activeSelf) {
    18.             deferredRoutine = routine;
    19.         } else
    20.             StartCoroutine(routine);
    21.     }
    22.  
    23.     private void OnEnable() {
    24.         CanvasGroup cg = GetComponent<CanvasGroup>();
    25.         if (cg != null)
    26.             cg.alpha = 1;
    27.         if (deferredRoutine != null) {
    28.             StartCoroutine(deferredRoutine);
    29.             deferredRoutine = null;
    30.         }
    31.     }
    32.     private IEnumerator load(string url) {
    33.         float dur = ViewConfig.GeneralAnimDuration;
    34.         Image.texture = null;
    35.         loadedUrl = null;
    36.         if (www != null) {
    37.             www.Abort();
    38.             www = null;
    39.         }
    40.         var imgRt = Image.GetComponent<RectTransform>();
    41.         www = UnityWebRequest.Get(url);
    42.         var ts = Time.realtimeSinceStartup;
    43.         yield return www.SendWebRequest();
    44.         print("ts:" + (Time.realtimeSinceStartup - ts));
    45.         loadedUrl = url;
    46.         Texture2D texture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
    47.         texture.LoadImage(www.downloadHandler.data);
    48.         www.Dispose();
    49.         www = null;
    50.         Image.texture = texture;
    51.         //
    52.         var cg = GetComponent<CanvasGroup>();
    53.         if (cg == null)
    54.             cg = gameObject.AddComponent<CanvasGroup>();
    55.         cg.alpha = 0;
    56.         float time = 0;
    57.         while (time < dur) {
    58.             time += Time.deltaTime;
    59.             cg.alpha = time / dur;
    60.             yield return null;
    61.         }
    62.         cg.alpha = 1;
    63.     }
    64.     private void OnDestroy() {
    65.         print("destroyed");
    66.         StopAllCoroutines();
    67.         if (www != null) {
    68.             www.Abort();
    69.             www = null;
    70.         }
    71.     }
    72.     public void Clear() {
    73.         StopAllCoroutines();
    74.         if (www != null) {
    75.             www.Abort();
    76.             www = null;
    77.         }
    78.         Image.texture = null;
    79.         loadedUrl = null;
    80.     }
    81. }
    82.  
    Therefore I'm kinda stuck on unity 2018
    Does anyone observe the same behaviour?
     
    Last edited: Sep 18, 2019