Search Unity

Run C# function from JS - Addmob Google Play

Discussion in 'Scripting' started by przem997, Oct 14, 2013.

  1. przem997

    przem997

    Joined:
    Dec 10, 2012
    Posts:
    80
    How run AdvertisementHandler.EnableAds(); in CSharp from my JScript ?

    My JScipt not work
    Code (csharp):
    1. var cs = GameObject.Find("AdvertisementManager");
    2. var script = cs.GetComponent("AdvertisementManager");
    3. script.EnableAds();

    AdvertisementManager.cs on the same GameObject name "AdvertisementManager"
    Code (csharp):
    1.  
    2.         if (GUI.Button(rect, "Enable"))   {
    3.             AdvertisementHandler.EnableAds();
    4.         }
    5.  
    6.         rect.y = rect.y + rect.height;
    7.         if (GUI.Button(rect, "Disable")) {          
    8.             AdvertisementHandler.DisableAds();
    9.         }
    10.  
    11.         rect.y = rect.y + rect.height;
    12.         // Make the Hide Button
    13.         if (GUI.Button(rect, "Hide"))   {
    14.             AdvertisementHandler.HideAds();
    15.         }
    16.  
    17.         rect.y = rect.y + rect.height;
    18.         // Make the Show button.
    19.         if (GUI.Button(rect, "Show"))   {
    20.             AdvertisementHandler.ShowAds();
    21.         }

    AdvertisementHandler.cs on GameObject name "AdvertisementManager"
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AdvertisementHandler : MonoBehaviour
    5. {
    6.     public enum AdvSize
    7.     {
    8.         BANNER,
    9.         IAB_MRECT,
    10.         IAB_BANNER,
    11.         IAB_LEADERBOARD,
    12.         SMART_BANNER,
    13.         DEVICE_WILL_DECIDE
    14.     };
    15.  
    16.     public enum AdvOrientation
    17.     {
    18.         VERTICAL,
    19.         HORIZONTAL
    20.     };
    21.  
    22.     public enum Position
    23.     {
    24.         NO_GRAVITY = 0,
    25.         CENTER_HORIZONTAL = 1,
    26.         LEFT = 3,
    27.         RIGHT = 5,
    28.         FILL_HORIZONTAL = 7,
    29.         CENTER_VERTICAL = 16,
    30.         CENTER = 17,
    31.         TOP = 48,
    32.         BOTTOM = 80,
    33.         FILL_VERTICAL = 112
    34.     };
    35.  
    36.     public enum AnimationInType
    37.     {
    38.         SLIDE_IN_LEFT,
    39.         FADE_IN,
    40.         NO_ANIMATION
    41.     };
    42.  
    43.     public enum AnimationOutType
    44.     {
    45.         SLIDE_OUT_RIGHT,
    46.         FADE_OUT,
    47.         NO_ANIMATION
    48.     };
    49.  
    50.     public enum Activity
    51.     {
    52.         INSTANTIATE,
    53.         DISABLE,
    54.         ENABLE,
    55.         HIDE,
    56.         SHOW,
    57.         REPOSITION
    58.     }
    59.     public enum LevelOfDebug
    60.     {
    61.         NONE,
    62.         LOW,
    63.         HIGH,
    64.         FLOOD
    65.     }
    66.  
    67.     static AndroidJavaClass admobPluginClass;
    68.     static AndroidJavaClass unityPlayer;
    69.     static AndroidJavaObject currActivity;
    70.  
    71.     /// <summary>
    72.     /// Initializing Plugin with values
    73.     /// </summary>
    74.     /// <param name="pubID">Admob App ID</param>
    75.     /// <param name="advSize">Advertisement Size</param>
    76.     /// <param name="advOrient">Advertisement Orientation</param>
    77.     /// <param name="position_1">Advertisement First Position</param>
    78.     /// <param name="position_2">Advertisement Second Position</param>
    79.     /// <param name="isTesting">Flag for testing game/app</param>
    80.     /// <param name="testDeviceId">Test Device Id</param>
    81.     /// <param name="animIn">Animation IN Type</param>
    82.     /// <param name="animOut">Animation OUT Type</param>
    83.     /// <param name="levelOfDebug">Debug Level</param>
    84.     public static void Instantiate(string pubID, AdvSize advSize, AdvOrientation advOrient, Position position_1, Position position_2, bool isTesting, string testDeviceId, AnimationInType animIn, AnimationOutType animOut, LevelOfDebug levelOfDebug)
    85.     {
    86.         Debug.Log("Instantiate Called");
    87.         admobPluginClass = new AndroidJavaClass("com.microeyes.admob.AdmobActivity");
    88.         unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    89.         currActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    90.         admobPluginClass.CallStatic("AdvHandler", (int)Activity.INSTANTIATE, currActivity, pubID, (int)advSize, (int)advOrient, (int)position_1, (int)position_2, isTesting, testDeviceId, (int)animIn, (int)animOut, (int)levelOfDebug);
    91.         Debug.Log("Instantiate FINISHED");
    92.     }
    93.  
    94.     /// <summary>
    95.     /// Enable Advertisements, work if not yet called / after calling DisableAdvs();
    96.     /// </summary>
    97.     public static void EnableAds()
    98.     {
    99.         Debug.Log("ENABLED Called");
    100.         admobPluginClass.CallStatic("AdvHandler", (int)Activity.ENABLE, currActivity, "", -1, -1, -1, -1, false, "", -1, -1, -1);
    101.         Debug.Log("ENABLED FINISHED");        
    102.     }
    103.  
    104.  
    105.     /// <summary>
    106.     /// Disable Advertisements, Call EnableAds() to start again
    107.     /// ShowAds() won't work here.
    108.     /// </summary>
    109.     public static void DisableAds()
    110.     {
    111.         Debug.Log("DISABLED Called");
    112.         admobPluginClass.CallStatic("AdvHandler", (int)Activity.DISABLE, currActivity, "", -1, -1, -1, -1, false, "", -1, -1, -1);
    113.         Debug.Log("DISABLED FINISHED");
    114.     }
    115.  
    116.     /// <summary>
    117.     /// Temp Hide Advertisements, Call Show() to show again
    118.     /// EnableAds() won't work here
    119.     /// </summary>
    120.     public static void HideAds()
    121.     {
    122.         Debug.Log("HIDE ADV Called");
    123.         admobPluginClass.CallStatic("AdvHandler", (int)Activity.HIDE, currActivity, "", -1, -1, -1, -1, false, "", -1, -1, -1);
    124.         Debug.Log("HIDE ADV FINISHED");
    125.     }
    126.  
    127.     /// <summary>
    128.     /// Show Advertisements, work after EnableAds() already called
    129.     /// </summary>
    130.     public static void ShowAds()
    131.     {
    132.         Debug.Log("SHOW ADV Called");
    133.         admobPluginClass.CallStatic("AdvHandler", (int)Activity.SHOW, currActivity, "", -1, -1, -1, -1, false, "", -1, -1, -1);
    134.         Debug.Log("SHOW ADV FINISHED");
    135.     }
    136. }
    137.  
    Scripts is from:
    http://forum.unity3d.com/threads/173292-Admob-Unity-Package-that-work-with-Unity-3-2-amp-above
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    The script is called AdvertisementHandler, not manager.
    Also, the function is static so you should be able to call it with:
    AdvertisementHandler.EnableAds();

    Don't need to use find.
     
  3. przem997

    przem997

    Joined:
    Dec 10, 2012
    Posts:
    80
    Yes, i know but this public class not work in my project.

    test.js
    Code (csharp):
    1. AdvertisementHandler.EnableAds();
    Result:
    BCE0005: Unknown identifier: 'AdvertisementHandler'.
     
  4. OceanBlue

    OceanBlue

    Joined:
    May 2, 2013
    Posts:
    251
    Try putting the C# script into your plugins folder or something
     
  5. przem997

    przem997

    Joined:
    Dec 10, 2012
    Posts:
    80
    Now it's woks very vell. Thanks!
     
  6. Muhammad Akmal

    Muhammad Akmal

    Joined:
    Mar 31, 2015
    Posts:
    1
    what did you do przem997
    I am facing same problem AdmobAdds.cs is present in plugins folder but its giving error BCE0005: Unknown identifier: 'AdmobAdds'. when I access it in js script