Search Unity

Tajoy plugin added but not showing banner ads

Discussion in 'Android' started by createplayshare, Jul 15, 2012.

  1. createplayshare

    createplayshare

    Joined:
    Oct 15, 2009
    Posts:
    80
    I am unity 3.5 to add tapjoy unity plugin in my ANDROID game. We have successfully added the plugin following various posts and knowledge page at Tapjoy. We can see impressions on tapjoy reporting page for ads showing up in our game but we cant see any ads in the game. It is only initializing. Its a development unity build. Virtual currency is set on dashboard.

    We have added TapjoyUnityPlugin_Android_v8.2.1

    can anyone please guide us where we are going wrong.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Runtime.InteropServices;
    4. using System;
    5.  
    6.  
    7. public struct TapjoyVirtualGood
    8. {
    9.     public string name;
    10.     public string type;
    11.     public string storeItemID;
    12.     public string description;
    13.     public int price;
    14.    
    15.     // Android
    16.     public string dataURL;
    17.     public string imageURL;
    18.     public int owned;
    19.     public string thumbURL;
    20.     public string productID;
    21.    
    22.     public ArrayList attributes;
    23. }
    24.  
    25. public struct TapjoyVirtualGoodAttribute
    26. {
    27.     public string type;
    28.     public string value;
    29. }
    30.  
    31. // Use with SetDisplayAdContentSize()
    32. public enum TapjoyBannerAdSize : int
    33. {
    34.     TJC_AD_BANNERSIZE_320X50 = 0,
    35.     TJC_AD_BANNERSIZE_640X100,
    36.     TJC_AD_BANNERSIZE_768X90,
    37. }
    38.  
    39.  
    40.  
    41.  
    42. public class TapjoyPlugin : MonoBehaviour
    43. {
    44.     public static AndroidJavaClass tapjoyConnect;
    45.     public static AndroidJavaObject tapjoyConnectInstance;
    46.     public static AndroidJavaObject currentActivity;
    47.    
    48.     public static void init()
    49.     {
    50.         if (tapjoyConnect == null)
    51.         {
    52.             print("Loading TapjoyPlugin");
    53.            
    54.             // Get the activity context for Android.
    55.             AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    56.             currentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
    57.            
    58.             // Create java class object.
    59.             tapjoyConnect = new AndroidJavaClass("com.tapjoy.TapjoyConnect");
    60.         }
    61.     }
    62.    
    63.     /// <summary>
    64.     /// Sets the GameObject handler class name.  MUST BE CALLED BEFORE ANY TAPJOY LIBRARY METHODS.
    65.     /// </summary>
    66.     /// <param name="handlerName">
    67.     /// A <see cref="System.String"/>
    68.     /// </param>
    69.     public static void SetCallbackHandler(string handlerName)
    70.     {
    71.         init();
    72.         tapjoyConnect.CallStatic("setHandlerClass", handlerName);
    73.     }
    74.    
    75.     /// <summary>
    76.     /// This method is called to initialize the TapjoyConnect system. This method should be called upon app initialization and must not have logic to prevent it from being called in any case.
    77.     /// </summary>
    78.     /// <param name="appID">
    79.     /// A <see cref="System.String"/>
    80.     /// </param>
    81.     /// <param name="secretKey">
    82.     /// A <see cref="System.String"/>
    83.     /// </param>
    84.     public static void RequestTapjoyConnect(string appID, string secretKey)
    85.     {
    86.         init();
    87.         tapjoyConnect.CallStatic("requestTapjoyConnect", currentActivity, appID, secretKey);
    88.         tapjoyConnectInstance = tapjoyConnect.CallStatic<AndroidJavaObject>("getTapjoyConnectInstance");
    89.     }
    90.    
    91.     /// <summary>
    92.     /// Enables Tapjoy library logging to the console.
    93.     /// </summary>
    94.     /// <param name="enable">
    95.     /// A <see cref="System.Boolean"/>
    96.     /// </param>
    97.     public static void EnableLogging(bool enable)
    98.     {
    99.         // Enable logging for debugging.
    100.         AndroidJavaClass tapjoyLog = new AndroidJavaClass("com.tapjoy.TapjoyLog");
    101.         tapjoyLog.CallStatic("enableLogging", enable); 
    102.     }
    103.    
    104.     /// <summary>
    105.     /// This is called when an action is completed. Actions are much like connects, except that this method is only called when a user completes an in-game action.
    106.     /// </summary>
    107.     /// <param name="actionID">
    108.     /// A <see cref="System.String"/>
    109.     /// </param>
    110.     public static void ActionComplete(string actionID)
    111.     {
    112.         tapjoyConnectInstance.Call("actionComplete", actionID);
    113.     }
    114.    
    115.     /// <summary>
    116.     /// Sets the user ID. The user ID defaults to the UDID. This is only changed when NOT using Tapjoy Managed Currency.
    117.     /// </summary>
    118.     /// <param name="userID">
    119.     /// A <see cref="System.String"/>
    120.     /// </param>
    121.     public static void SetUserID(string userID)
    122.     {
    123.         tapjoyConnectInstance.Call("setUserID", userID);
    124.     }
    125.    
    126.     /// <summary>
    127.     /// Displays the offer wall.
    128.     /// </summary>
    129.     public static void ShowOffers()
    130.     {
    131.         tapjoyConnectInstance.Call("showOffers");
    132.     }
    133.    
    134.     /// <summary>
    135.     /// Initiates a request to get a user's Tap Points (Tapjoy Managed currency).
    136.     /// </summary>
    137.     public static void GetTapPoints()
    138.     {
    139.         tapjoyConnectInstance.Call("getTapPoints");
    140.     }
    141.    
    142.     /// <summary>
    143.     /// Updates the Tap Points for the user with the given spent amount of currency. If the spent amount exceeds the current amount of currency the user has, nothing will happen.
    144.     /// </summary>
    145.     /// <param name="points">
    146.     /// A <see cref="System.Int32"/>
    147.     /// </param>
    148.     public static void SpendTapPoints(int points)
    149.     {
    150.         tapjoyConnectInstance.Call("spendTapPoints", points);
    151.     }
    152.    
    153.     /// <summary>
    154.     /// Updates the Tap Points for the user with the given awarded amount of currency.
    155.     /// </summary>
    156.     /// <param name="points">
    157.     /// A <see cref="System.Int32"/>
    158.     /// </param>
    159.     public static void AwardTapPoints(int points)
    160.     {
    161.         tapjoyConnectInstance.Call("awardTapPoints", points);
    162.     }
    163.    
    164.     /// <summary>
    165.     /// Returns the current amount of Tap Points that a user has. When using Tapjoy Managed currency, the user is determined by the UDID of the device. This can safely be called after Tap Points data is retrieved from the server.
    166.     /// </summary>
    167.     /// <returns>
    168.     /// A <see cref="System.Int32"/>
    169.     /// </returns>
    170.     public static int QueryTapPoints()
    171.     {
    172.         return tapjoyConnectInstance.Call<int>("getTapPointsTotal");
    173.     }
    174.    
    175.     /// <summary>
    176.     /// Enables a notifier which triggers whenever the user earns currency through Tapjoy.  It will be called in CurrencyEarned(...).
    177.     /// </summary>
    178.     public static void SetEarnedPointsNotifier()
    179.     {
    180.         tapjoyConnectInstance.Call("setEarnedPointsNotifier");
    181.     }
    182.  
    183.     //========================================================================================================================
    184.     // BANNER ADS
    185.     //========================================================================================================================
    186.     /// <summary>
    187.     /// Initiates a request to get a Tapjoy Banner Ad.
    188.     /// </summary>
    189.     public static void GetDisplayAd()
    190.     {
    191.         tapjoyConnectInstance.Call("getDisplayAd");
    192.     }
    193.    
    194.     /// <summary>
    195.     /// Shows the banner ad.  Call after receiving the DisplayAdLoaded(...) notifier.
    196.     /// </summary>
    197.     public static void ShowDisplayAd()
    198.     {
    199.         tapjoyConnectInstance.Call("showBannerAd");
    200.     }
    201.    
    202.     /// <summary>
    203.     /// Hides the banner ad.
    204.     /// </summary>
    205.     public static void HideDisplayAd()
    206.     {
    207.         tapjoyConnectInstance.Call("hideBannerAd");
    208.     }
    209.    
    210.     /// <summary>
    211.     /// Sets the banner ad size.  Use TapjoyBannerAdSize enum for supported sizes.
    212.     /// </summary>
    213.     /// <param name="size">
    214.     /// A <see cref="System.Int32"/>
    215.     /// </param>
    216.     public static void SetDisplayAdContentSize(int size)
    217.     {
    218.         string bannerSize = "320x50";
    219.        
    220.         switch ((TapjoyBannerAdSize)size)
    221.         {
    222.             case TapjoyBannerAdSize.TJC_AD_BANNERSIZE_320X50:
    223.                 bannerSize = "320x50";
    224.                 break;
    225.             case TapjoyBannerAdSize.TJC_AD_BANNERSIZE_640X100:
    226.                 bannerSize = "640x100";
    227.                 break;
    228.             case TapjoyBannerAdSize.TJC_AD_BANNERSIZE_768X90:
    229.                 bannerSize = "768x90";
    230.                 break;
    231.             default:
    232.                 bannerSize = "320x50";
    233.                 print("*** Invalid banner ad size, using 320x50 as default ***");
    234.                 break;
    235.         }
    236.        
    237.         tapjoyConnectInstance.Call("setBannerAdSize", bannerSize);
    238.     }
    239.    
    240.     /// <summary>
    241.     /// Updates the banner ad with a new one or disables auto-refresh.
    242.     /// </summary>
    243.     /// <param name="enable">
    244.     /// A <see cref="System.Boolean"/>
    245.     /// </param>
    246.     public static void RefreshDisplayAd(bool enable)
    247.     {
    248.         tapjoyConnectInstance.Call("enableBannerAdAutoRefresh", enable);
    249.     }
    250.    
    251.     /// <summary>
    252.     /// Moves the location of the Display Banner Ad.
    253.     /// </summary>
    254.     /// <param name="x">
    255.     /// A <see cref="System.Int32"/>
    256.     /// </param>
    257.     /// <param name="y">
    258.     /// A <see cref="System.Int32"/>
    259.     /// </param>
    260.     public static void MoveDisplayAd(int x, int y)
    261.     {
    262.         tapjoyConnectInstance.Call("setBannerAdPosition", x, y);
    263.     }
    264.    
    265.     //========================================================================================================================
    266.     // FULL SCREEN ADS
    267.     //========================================================================================================================
    268.     /// <summary>
    269.     /// Initiates a request to get the full screen ad.
    270.     /// </summary>
    271.     public static void GetFeaturedApp()
    272.     {
    273.         tapjoyConnectInstance.Call("getFeaturedApp");
    274.     }
    275.    
    276.     /// <summary>
    277.     /// Sets the display count for full screen ad. The display count is separate for each unique full screen ad.
    278.     /// </summary>
    279.     /// <param name="displayCount">
    280.     /// A <see cref="System.Int32"/>
    281.     /// </param>
    282.     public static void SetFeaturedAppDisplayCount(int displayCount)
    283.     {
    284.         tapjoyConnectInstance.Call("setFeaturedAppDisplayCount", displayCount);
    285.     }
    286.    
    287.     /// <summary>
    288.     /// Shows a full screen full screen ad ad. This should be called after full screen ad data is retrieved from the server.
    289.     /// </summary>
    290.     public static void ShowFeaturedAppFullScreenAd()
    291.     {
    292.         tapjoyConnectInstance.Call("showFeaturedAppFullScreenAd");
    293.     }
    294.    
    295.     //========================================================================================================================
    296.     // VIDEOS
    297.     //========================================================================================================================
    298.     /// <summary>
    299.     /// Initializes video ads. It is recommended that this be called upon app start. No additional steps are needed to integrate video ads.
    300.     /// Caching automatically begins after this method is called and will automatically be displayed in the offer wall when it has finished caching onto the device.
    301.     /// </summary>
    302.     public static void InitVideoAd()
    303.     {
    304.         tapjoyConnectInstance.Call("initVideoAd");
    305.     }
    306.    
    307.     /// <summary>
    308.     /// Sets the number of videos to cache on the device. This value is 5 by default. Lower this value if there is heavy network dependent code running during the initialization of the application.
    309.     /// </summary>
    310.     /// <param name="cacheCount">
    311.     /// A <see cref="System.Int32"/>
    312.     /// </param>
    313.     public static void SetVideoCacheCount(int cacheCount)
    314.     {
    315.         tapjoyConnectInstance.Call("setVideoCacheCount", cacheCount);
    316.     }
    317.    
    318.     /// <summary>
    319.     /// Sets whether to enable caching for videos.  By default this is enabled.
    320.     /// </summary>
    321.     /// <param name="enable">
    322.     /// A <see cref="System.Boolean"/>
    323.     /// </param>
    324.     public static void EnableVideoCache(bool enable)
    325.     {
    326.         tapjoyConnectInstance.Call("enableVideoCache", enable);
    327.     }
    328.    
    329.     //========================================================================================================================
    330.     // MULTIPLE CURRENCY (currency cannot be managed by Tapjoy)
    331.     //========================================================================================================================
    332.     /// <summary>
    333.     /// Shows the offer wall with a specified currencyID.  The selector is whether the offer wall should allow toggling between the app's currencies.
    334.     /// </summary>
    335.     /// <param name="currencyID">
    336.     /// A <see cref="System.String"/>
    337.     /// </param>
    338.     /// <param name="selector">
    339.     /// A <see cref="System.Boolean"/>
    340.     /// </param>
    341.     public static void ShowOffersWithCurrencyID(string currencyID, bool selector)
    342.     {
    343.         tapjoyConnectInstance.Call("showOffersWithCurrencyID", currencyID, selector);
    344.     }
    345.    
    346.     /// <summary>
    347.     /// Initiates a request to get a Tapjoy banner ad with a specified currencyID.
    348.     /// </summary>
    349.     /// <param name="currencyID">
    350.     /// A <see cref="System.String"/>
    351.     /// </param>
    352.     public static void GetDisplayAdWithCurrencyID(string currencyID)
    353.     {
    354.         tapjoyConnectInstance.Call("getDisplayAdWithCurrencyID", currencyID);
    355.     }
    356.    
    357.     /// <summary>
    358.     /// Initiates a request to get the full screen ad with a specified currencyID.
    359.     /// </summary>
    360.     /// <param name="currencyID">
    361.     /// A <see cref="System.String"/>
    362.     /// </param>
    363.     public static void GetFeaturedAppWithCurrencyID(string currencyID)
    364.     {
    365.         tapjoyConnectInstance.Call("getFeaturedAppWithCurrencyID", currencyID);
    366.     }
    367.    
    368.     /// <summary>
    369.     /// Set a multiplier for virtual currency appearance on the offer wall, banner ads, etc.
    370.     /// </summary>
    371.     /// <param name="multiplier">
    372.     /// A <see cref="System.Single"/>
    373.     /// </param>
    374.     public static void SetCurrencyMultiplier(float multiplier)
    375.     {
    376.         tapjoyConnectInstance.Call("setCurrencyMultiplier", multiplier);
    377.     }
    378.    
    379.     //========================================================================================================================
    380.     // VIRTUAL GOODS
    381.     //========================================================================================================================
    382.     /// <summary>
    383.     /// Sets the color scheme of Tapjoy views.
    384.     /// </summary>
    385.     /// <param name="color">
    386.     /// A <see cref="System.Int32"/>
    387.     /// </param>
    388.     public static void SetUserDefinedColorWithIntValue(int color)
    389.     {
    390.         tapjoyConnectInstance.Call("setUserDefinedColor", color);
    391.     }
    392.    
    393.     /// <summary>
    394.     /// Initializes virtual goods by requesting purchased virtual goods from the server.
    395.     /// </summary>
    396.     public static void InitVirtualGoods()
    397.     {
    398.         tapjoyConnectInstance.Call("checkForVirtualGoods");
    399.     }
    400.    
    401.     /// <summary>
    402.     /// Shows the virtual goods UI view.
    403.     /// </summary>
    404.     public static void ShowVirtualGoodsView()
    405.     {
    406.         tapjoyConnectInstance.Call("showVirtualGoods");
    407.     }
    408.    
    409.     /// <summary>
    410.     /// Returns all virtual goods that have been purchased by a user in an ArrayList form. Each object is a TapjoyVirtualGood struct, defined within TapjoyPlugin.cs
    411.     /// </summary>
    412.     /// <returns>
    413.     /// A <see cref="ArrayList"/>
    414.     /// </returns>
    415.     public static ArrayList GetPurchasedVirtualGoodsArray()
    416.     {
    417.         ArrayList vgArray = new ArrayList();
    418.         int count = GetPurchasedVirtualGoodCount();
    419.        
    420.         for (int i = 0; i < count; ++i)
    421.         {
    422.             vgArray.Add(GetVirtualGood(i));
    423.         }
    424.        
    425.         return vgArray;
    426.     }
    427.    
    428.     //========================================================================================================================
    429.     // HELPER methods
    430.     //========================================================================================================================
    431.     private static TapjoyVirtualGood GetVirtualGood(int index)
    432.     {
    433.         // Allocate for JNI reference table to ensure we don't get an overflow.
    434.         AndroidJNI.PushLocalFrame(16);
    435.        
    436.         TapjoyVirtualGood item = new TapjoyVirtualGood();
    437.        
    438.         AndroidJavaObject javaVirtualGoodItem = tapjoyConnectInstance.Call<AndroidJavaObject>("getPurchasedItemAtIndex", index);
    439.        
    440.         if (javaVirtualGoodItem != null)
    441.         {
    442.             item.name =  javaVirtualGoodItem.Call<string>("getName");
    443.             item.description = javaVirtualGoodItem.Call<string>("getDescription");
    444.             item.dataURL = javaVirtualGoodItem.Call<string>("getDatafileUrl");
    445.             item.imageURL = javaVirtualGoodItem.Call<string>("getFullImageUrl");
    446.             item.owned = javaVirtualGoodItem.Call<int>("getNumberOwned");
    447.             item.price = javaVirtualGoodItem.Call<int>("getPrice");
    448.             item.productID = javaVirtualGoodItem.Call<string>("getProductID");
    449.             item.thumbURL = javaVirtualGoodItem.Call<string>("getThumbImageUrl");
    450.             //print("thumb img: " + vgItem.Call<string>("getThumbImage"));          // returns a Drawable Android object
    451.             item.storeItemID = javaVirtualGoodItem.Call<string>("getVgStoreItemID");
    452.             item.type =  javaVirtualGoodItem.Call<string>("getVgStoreItemTypeName");
    453.            
    454.             // Set Virtual Good attributes
    455.             item.attributes = new ArrayList();
    456.             AndroidJavaObject javaAttributeList = javaVirtualGoodItem.Call<AndroidJavaObject>("getVgStoreItemsAttributeValueList");
    457.             int size = javaAttributeList.Call<int>("size");
    458.            
    459.             for (int i = 0; i < size; i++)
    460.             {
    461.                 AndroidJavaObject javaAttributeData = javaAttributeList.Call<AndroidJavaObject>("get", i);             
    462.                 item.attributes.Add(GetAttributes(javaAttributeData));
    463.             }
    464.         }
    465.        
    466.         // Cleanup.
    467.         AndroidJNI.PopLocalFrame(IntPtr.Zero);
    468.        
    469.         return item;
    470.     }
    471.    
    472.     private static TapjoyVirtualGoodAttribute GetAttributes(AndroidJavaObject attributeData)
    473.     {
    474.         // Allocate for JNI reference table to ensure we don't get an overflow.
    475.         AndroidJNI.PushLocalFrame(2);
    476.        
    477.         TapjoyVirtualGoodAttribute vgAttribute = new TapjoyVirtualGoodAttribute();     
    478.         vgAttribute.type = attributeData.Call<string>("getAttributeType");
    479.         vgAttribute.value = attributeData.Call<string>("getAttributeValue");
    480.        
    481.         // Cleanup.
    482.         AndroidJNI.PopLocalFrame(IntPtr.Zero);
    483.        
    484.         return vgAttribute;
    485.     }
    486.    
    487.     private static int GetPurchasedVirtualGoodCount()  
    488.     {
    489.         return tapjoyConnectInstance.Call<int>("getPurchasedItemsCount");
    490.     }
    491. }
     
    Last edited: Jul 15, 2012