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

Threading. Where to start?

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

  1. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    I've installed the Adtapsy SDK and I'm trying to thread the scripts to prevent lag caused by caching.
    These are the three scripts included:
    Code (CSharp):
    1.  
    2. using System.Runtime.InteropServices;
    3. using System.Threading;
    4. using UnityEngine;
    5.  
    6. [ClassInterface(ClassInterfaceType.None)]
    7. [ComVisible(true)]
    8. public class Adverts : MonoBehaviour
    9. {
    10.     public static Adverts manager;
    11.  
    12.     void Awake()
    13.     {
    14.         manager = this;
    15.     }
    16.  
    17.     void Start()
    18.     {
    19.         AdTapsy.SetTestMode(true, "xxxxxxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxx");
    20.         AdTapsy.StartSessionAndroid("xxxxxxxxxxxxxxxxxxxxxxxx");
    21.     }
    22.  
    23.     public void ShowInterstitial()
    24.     {
    25.         if (AdTapsy.IsInterstitialReadyToShow())
    26.         {
    27.             AdTapsy.ShowInterstitial();
    28.         }
    29.     }
    30. }
    31.  
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class AdTapsy : MonoBehaviour {
    7.     public const int InterstitialZone = 0;
    8.     public const int RewardedVideoZone = 1;
    9.  
    10.     public delegate void ATDelegate(int zoneId);
    11.     public static ATDelegate OnAdCached = delegate {};
    12.     public static ATDelegate OnAdShown = delegate {};
    13.     public static ATDelegate OnAdSkipped = delegate {};
    14.     public static ATDelegate OnAdClicked  = delegate {};
    15.     public static ATDelegate OnAdFailedToShow  = delegate {};
    16.     public static ATDelegate OnRewardEarned = delegate{};
    17.  
    18.     public static void StartSessionAndroid(string appId){
    19. #if UNITY_ANDROID  && !UNITY_EDITOR
    20.         AdTapsyAndroid.StartSession(appId);
    21. #endif
    22.     }
    23.  
    24.     public static void StartSessionIOS(string appId) {
    25. #if UNITY_IPHONE && !UNITY_EDITOR
    26.         AdTapsyIOS.StartSession (appId);
    27. #endif  
    28.     }
    29.  
    30.     public static void SetTestMode(bool enabled, params string[] testDevices){
    31. #if UNITY_ANDROID  && !UNITY_EDITOR
    32.         AdTapsyAndroid.SetTestMode(enabled, testDevices);
    33. #endif
    34. #if UNITY_IPHONE && !UNITY_EDITOR
    35.         AdTapsyIOS.SetTestMode(enabled, testDevices);
    36. #endif
    37.     }
    38.  
    39.     public static void ShowInterstitial(){
    40. #if UNITY_ANDROID  && !UNITY_EDITOR
    41.         AdTapsyAndroid.ShowInterstitial();
    42. #endif
    43. #if UNITY_IPHONE && !UNITY_EDITOR
    44.         AdTapsyIOS.ShowInterstitial ();
    45. #endif
    46.     }
    47.    
    48.     public static bool IsInterstitialReadyToShow(){
    49. #if UNITY_ANDROID  && !UNITY_EDITOR
    50.         return AdTapsyAndroid.IsInterstitialReadyToShow();
    51. #endif
    52. #if UNITY_IPHONE && !UNITY_EDITOR
    53.         return AdTapsyIOS.IsInterstitialReadyToShow();
    54. #else
    55.         return false;
    56. #endif
    57.     }
    58.     public static void ShowRewardedVideo(){
    59. #if UNITY_ANDROID  && !UNITY_EDITOR
    60.         AdTapsyAndroid.ShowRewardedVideo();
    61. #endif
    62. #if UNITY_IPHONE && !UNITY_EDITOR
    63.         AdTapsyIOS.ShowRewardedVideo ();
    64. #endif
    65.     }
    66.    
    67.     public static bool IsRewardedVideoReadyToShow(){
    68. #if UNITY_ANDROID  && !UNITY_EDITOR
    69.         return AdTapsyAndroid.IsRewardedVideoReadyToShow();
    70. #endif
    71. #if UNITY_IPHONE && !UNITY_EDITOR
    72.         return AdTapsyIOS.IsRewardedVideoReadyToShow();
    73. #else
    74.         return false;
    75. #endif
    76.     }
    77.  
    78.    
    79.     public static bool CloseAd(){
    80. #if UNITY_ANDROID  && !UNITY_EDITOR
    81.         return AdTapsyAndroid.CloseAd();
    82. #else
    83.         return false;
    84. #endif
    85.     }
    86.     public static void SetRewardedVideoAmount(int amount){
    87. #if UNITY_ANDROID && !UNITY_EDITOR
    88.         AdTapsyAndroid.SetRewardedVideoAmount(amount);
    89. #endif
    90. #if UNITY_IPHONE && !UNITY_EDITOR
    91.     AdTapsyIOS.SetRewardedVideoAmount(amount);
    92. #endif
    93.     }
    94.     public static void SetUserIdentifier(string userId){
    95. #if UNITY_ANDROID && !UNITY_EDITOR
    96.         AdTapsyAndroid.SetUserIdentifier(userId);
    97. #endif
    98. #if UNITY_IPHONE && !UNITY_EDITOR
    99.         AdTapsyIOS.SetUserIdentifier(userId);
    100. #endif
    101.     }
    102.     public static void SetRewardedVideoPrePopupEnabled(bool toShow){
    103. #if UNITY_ANDROID && !UNITY_EDITOR
    104.         AdTapsyAndroid.SetRewardedVideoPrePopupEnabled(toShow);
    105. #endif
    106. #if UNITY_IPHONE && !UNITY_EDITOR
    107.         AdTapsyIOS.SetRewardedVideoPrePopupEnabled(toShow);
    108. #endif
    109.     }
    110.     public static void SetRewardedVideoPostPopupEnabled(bool toShow){
    111. #if UNITY_ANDROID && !UNITY_EDITOR
    112.         AdTapsyAndroid.SetRewardedVideoPostPopupEnabled(toShow);
    113. #endif
    114. #if UNITY_IPHONE && !UNITY_EDITOR
    115.         AdTapsyIOS.SetRewardedVideoPrePopupEnabled(toShow);
    116. #endif
    117.     }
    118. }
    119.  
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public static class AdTapsyAndroid {
    7.    
    8.     static AdTapsyAndroid(){
    9.         #if UNITY_ANDROID  && !UNITY_EDITOR
    10.         AndroidJNI.AttachCurrentThread();
    11.         #endif
    12.     }
    13.    
    14.     public static void StartSession(string appId){
    15.         #if UNITY_ANDROID  && !UNITY_EDITOR
    16.         getAdTapsy().CallStatic("setEngine", "unity");
    17.         getAdTapsy().CallStatic("setDelegate", new AdTapsyDelegate());
    18.         getAdTapsy().CallStatic("setRewardedDelegate", new AdTapsyRewardedDelegate());
    19.         getAdTapsy().CallStatic("startSession", getCurrentActivity(), appId);
    20.         #endif
    21.     }
    22.  
    23.     public static void SetTestMode(bool enabled, params string[] testDevices){
    24.         #if UNITY_ANDROID  && !UNITY_EDITOR
    25.         getAdTapsy().CallStatic("setTestMode", true, testDevices);
    26.         #endif
    27.     }
    28.    
    29.     public static void ShowInterstitial(){
    30.         #if UNITY_ANDROID  && !UNITY_EDITOR
    31.         getAdTapsy().CallStatic("showInterstitial", getCurrentActivity());
    32.         #endif
    33.     }
    34.     public static void ShowRewardedVideo(){
    35.         #if UNITY_ANDROID  && !UNITY_EDITOR
    36.         getAdTapsy().CallStatic("showRewardedVideo", getCurrentActivity());
    37.         #endif
    38.     }
    39.     public static bool CloseAd(){
    40.         #if UNITY_ANDROID  && !UNITY_EDITOR
    41.         return getAdTapsy().CallStatic<bool>("closeAd");
    42.         #else
    43.         return false;
    44.         #endif
    45.     }
    46.     public static bool IsInterstitialReadyToShow(){
    47.         #if UNITY_ANDROID  && !UNITY_EDITOR
    48.         return getAdTapsy().CallStatic<bool>("isInterstitialReadyToShow");
    49.         #else
    50.         return false;
    51.         #endif
    52.     }
    53.     public static bool IsRewardedVideoReadyToShow(){
    54.         #if UNITY_ANDROID  && !UNITY_EDITOR
    55.         return getAdTapsy().CallStatic<bool>("isRewardedVideoReadyToShow");
    56.         #else
    57.         return false;
    58.         #endif
    59.     }
    60.     public static void SetRewardedVideoAmount(int amount){
    61. #if UNITY_ANDROID  && !UNITY_EDITOR
    62.         getAdTapsy().CallStatic("setRewardedVideoAmount", amount);
    63. #endif
    64.     }
    65.     public static void SetUserIdentifier(string userId){
    66. #if UNITY_ANDROID  && !UNITY_EDITOR
    67.         getAdTapsy().CallStatic("setUserIdentifier", userId);
    68. #endif
    69.     }
    70.     public static void SetRewardedVideoPrePopupEnabled(bool toShow){
    71. #if UNITY_ANDROID  && !UNITY_EDITOR
    72.         getAdTapsy().CallStatic("setRewardedVideoPrePopupEnabled", toShow);
    73. #endif
    74.     }
    75.     public static void SetRewardedVideoPostPopupEnabled(bool toShow){
    76. #if UNITY_ANDROID  && !UNITY_EDITOR
    77.         getAdTapsy().CallStatic("setRewardedVideoPostPopupEnabled", toShow);
    78. #endif
    79.     }
    80.     #if UNITY_ANDROID  && !UNITY_EDITOR
    81.     private static AndroidJavaObject getCurrentActivity(){
    82.         AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    83.         AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
    84.         return jo;
    85.     }
    86.      
    87.  
    88.     private static AndroidJavaClass getAdTapsy(){
    89.         return new AndroidJavaClass("com.adtapsy.sdk.AdTapsy");
    90.     }
    91.     private class AdTapsyRewardedDelegate : AndroidJavaProxy
    92.     {
    93.         public AdTapsyRewardedDelegate() : base("com.adtapsy.sdk.AdTapsyRewardedDelegate")
    94.         {
    95.  
    96.         }
    97.         void onRewardEarned(int amount){
    98.             AdTapsy.OnRewardEarned.Invoke (amount);
    99.         }
    100.     }
    101.     private class AdTapsyDelegate : AndroidJavaProxy
    102.     {
    103.        
    104.         public AdTapsyDelegate() : base("com.adtapsy.sdk.AdTapsyDelegate")
    105.         {
    106.            
    107.         }
    108.        
    109.         void onAdShown(int zoneId)
    110.         {
    111.             AdTapsy.OnAdShown.Invoke (zoneId);
    112.         }
    113.         void onAdSkipped(int zoneId){
    114.             AdTapsy.OnAdSkipped.Invoke (zoneId);
    115.         }
    116.         void onAdClicked(int zoneId){
    117.             AdTapsy.OnAdClicked.Invoke (zoneId);
    118.         }
    119.         void onAdFailToShow(int zoneId){
    120.             AdTapsy.OnAdFailedToShow.Invoke (zoneId);
    121.         }
    122.         void onAdCached(int zoneId){
    123.             AdTapsy.OnAdCached.Invoke (zoneId);
    124.         }
    125.     }
    126.     #endif
    127.    
    128. }
    129.  
    So my question is of all these scripts which should I begin threading?
    The first, second or all ?
    I assumed that it would be the third and will begin with this.
    Just want to know if what I'm doing is correct first.
     
  2. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    Actually, I would try threading from the first script .. I think you could do it with the Task Parallel Library or Thread Ninja.

    The TPL was backported to .net 3.5 as part of the reactive extensions, which someone turned into an asset.. Maybe it will help. https://www.assetstore.unity3d.com/en/#!/content/17276

    Thread Ninja is quite a bit simpler, you simply need to change your calling code into a coroutine and use their simple syntax. https://www.assetstore.unity3d.com/en/#!/content/15717

    As an aside.. why does all your code live in static methods? It looks a little sad and procedural :(
     
    Ryiah and JoeStrout like this.
  3. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    The last two scripts came with the Adtapsy SDK.
    Using the thread Ninja I managed to change my Adverts script to this.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using CielaSpike;
    5.  
    6. public class Adverts : MonoBehaviour
    7. {
    8.     public static Adverts manager;
    9.  
    10.     void Awake()
    11.     {
    12.         manager = this;
    13.     }
    14.  
    15.     void Start()
    16.     {
    17.         this.StartCoroutineAsync(LoadAds());
    18.     }
    19.  
    20.     IEnumerator LoadAds()
    21.     {
    22.         AdTapsy.StartSessionAndroid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    23.         yield return Ninja.JumpToUnity;
    24.         AdTapsy.OnAdCached += delegate (int zoneId) {
    25.             if (zoneId == AdTapsy.InterstitialZone)
    26.             {
    27.                 this.StopCoroutine(LoadAds());
    28.             }
    29.         };
    30.     }
    31.  
    32.     public void ShowInterstitial()
    33.     {
    34.         if (AdTapsy.IsInterstitialReadyToShow())
    35.         {
    36.             AdTapsy.ShowInterstitial();
    37.         }
    38.     }
    39. }
    40.     }
    41.  
    I'm not sure why but if I run StartSession in the background thread It does not show any ads up.
     
    Last edited: Jan 21, 2016
  4. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
  5. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    Sorry Venatal, it sounds like the AdTapsy scripts can't run in a background thread -- they must have some kind of dependency on being run in the main thread.

    The other scripts you posted from AdTapsy are very odd to me. It looks as though their API is written in Java and they have provided a bridge to Unity.

    Since their code is probably written in Java, I assume you are not given source access in order to remove the dependency of being on the main thread. In that case, you may be out of luck but I would try asking on the AdTapsy forums as they would have a better chance of knowing the strengths and shortcomings of their own software.

    Good luck and sorry I wasn't more help!
     
  6. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    Is there a way to change the thread order? So I swap the background process to the main thread and the main thread to the background thread?
    Messing about with Ninja thread when I use JumpBack I can get the ads to load but they the game still lags (freezes) for a second or two however when I use main thread and the wait for task to finish it seems fine then a couple seconds later 6-10 seconds the game lags quite heavily and can crash at times
     
  7. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    I don't believe so, though I'm not totally clear what you're asking. The main thread is the first one created along with the process (in this case, Unity). There is no way to "promote" a different thread to be the main thread without shutting down the process and starting it again. Then you will be back to square one because there will only be a single thread.

    The theory behind preventing lag is to run slow operations "asynchronously". One of the methods for achieving asynchronous behavior is multiple threads. Unfortunately, multiple threads imply some kind of shared data and if the script run on the different threads is not written to cooperate in this kind of environment they will compete for the data. This could definitely cause the strange behavior you are describing -- particularly the crashing.

    My only guess is that the AdTapsy scripts are not designed to be run on different threads.

    Without access to the AdTapsy code, I don't think you'll be able to determine why it is not working on the background thread. I would suggest you ask on the AdTapsy forums if there is support for asynchronous loading and caching of the ads. Most likely this would involve calling a particular method like "LoadAds" on a background thread and another methods like "ShowAds" on the main thread once the background thread has completed.
     
  8. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    Thanks for the in-depth answer, appreciate it.
    I think I'll try looking for different Ad network mediation tool because trying to get Adtapsy to stop lagging has taken me almost a week to figure out without success.


    Actually, since StartSession is requesting and caching all the ads at once (my guess) surely if I request the ads one after the other that can fix the lag?
     
    Last edited: Jan 25, 2016