Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to prevent lag from Ad requests (Downloading and caching)

Discussion in 'Scripting' started by Venatal, Jan 23, 2016.

  1. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    I've tried using the background thread and the main thread, when I use the background thread the game lags for about a second or two however if I use the main thread it lags roughly 5-6 seconds after calling the code and can crash.
    What other methods are there for loading ad requests that don't seem to cause lag?
     
  2. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Can I see your background thread code?
     
  3. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    Sure, I'm using the thread ninja plugin.
    Code (CSharp):
    1.  
    2. using CielaSpike;
    3. using System.Collections;
    4. using UnityEngine;
    5.  
    6. public class LoadAdverts : MonoBehaviour
    7. {
    8.     Task task;
    9.  
    10.     void Awake()
    11.     {
    12.         StartCoroutine(LoadAds());
    13.     }
    14.  
    15.  
    16.     IEnumerator LoadAds()
    17.     {
    18.         yield return Ninja.JumpBack;
    19.         AdTapsy.StartSessionAndroid("xxxxxxxxxxxxxxxxxxxxxx");
    20.         AdTapsy.OnAdCached += delegate (int zoneId) {
    21.             if (zoneId == AdTapsy.InterstitialZone)
    22.             {
    23.                 StopCoroutine(LoadAds());
    24.             }
    25.         };
    26.     }
    27. }
    28.  
     
  4. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Hi, currently, as I'm unfamiliar with the "Ninja plugin" I cannot see a thread. Also, your task is never used.

    Here is an example of Multi-threading - this is what you need.