Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can't use Unity Ads using Javascript

Discussion in 'Unity Ads & User Acquisition' started by elmar1028, Oct 22, 2014.

  1. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,355
    Hi guys:

    So I am trying to implement UnityAds into Android game.

    Since I am a JS fan I decided it would be nice to show ads using JS.

    Here is my code:

    Code (JavaScript):
    1. import UnityEngine.Advertisements;
    2.  
    3. function Start () {
    4.     Advertisement.Initialize(1234);
    5. }
    This gives me a following error:

    So I checked out the UnityAds folder and found the main script (C#) which contained an Advertisements along with a declared namespace. How come Unity cannot find this statement? o_O

    Any ideas how to fix this? I know that C# uses "using". I found out that Javascript uses "import".
    Is UnityAds can be used using C# only?


    Answers are greatly appreciated! :)
     
    HeikkiTunkelo likes this.
  2. unity-nikkolai

    unity-nikkolai

    Joined:
    Sep 18, 2014
    Posts:
    540
    You're declaring the namespace properly. Trouble is, since Unity Ads is written in C#, you need to make sure it's compiled first before you can use it with UnityScript. See Script Compilation in Unity Docs for more details.

    To fix this issue, move the UnityAds directory into the Standard Assets directory. Then, open up the UnityAdsEditorPlaceholder.cs script in MonoDevelop and update the path references pointing to placeholder textures:
    Code (CSharp):
    1. placeHolderLandscape = textureFromFile(Application.dataPath + "/Standard Assets/UnityAds/Advertisements/VideoAds/test_unit_800.png");
    2. placeHolderPortrait = textureFromFile(Application.dataPath + "/Standard Assets/UnityAds/Advertisements/VideoAds/test_unit_600.png");
    We'll have to address this issue more directly in future Asset Store releases in order to better support integrations using UnityScript. For now, this is how you setup your project with Unity Ads.
     

    Attached Files:

    Last edited: Oct 27, 2014
    HeikkiTunkelo likes this.
  3. unity-nikkolai

    unity-nikkolai

    Joined:
    Sep 18, 2014
    Posts:
    540
    Just a quick update: we'll have a solution for this in the next Unity Ads update, which should be on the Asset Store in the next week or so.
     
    EliasMasche likes this.
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Has this been fixed yet? I can't get it to work properly with these steps here:

     
  5. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Hmm fixed it by moving my script from the root asset dir to a "Script" dir. Seems to conflict with what the docs say.

    Next problem shows up:

    The documentation on this is really unhelpful and I really don't want to use C# and Unityscript in one project.
     
    Last edited: Nov 11, 2014
  6. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,355
    You might've possibly done this:

    Code (JavaScript):
    1. UnityEngine.Advertisements.Advertisement.Initialize("some id")
    Instead of this:

    Code (JavaScript):
    1. UnityEngine.Advertisements.Advertisement.Initialize("some id", true)
     
    unity-nikkolai likes this.
  7. unity-nikkolai

    unity-nikkolai

    Joined:
    Sep 18, 2014
    Posts:
    540
    One of the benefits of using C# is that you can set default values for parameters so you don't have to explicitly write them when calling a method that uses them. Unfortunately, UnityScript doesn't support this. You will need to pass values for all parameters, and do so in the right order.

    The latest version of the Unity Ads asset package (1.0.3) released last week, and is more UnityScript friendly than previous versions. Unity Ads script assets are placed within the Standard Assets directory so that they compile before your UnityScript assets, allowing them to reference from the C# scripts.

    I've written a UnityScript version of the UnityAdsHelper script called UnityAdsHelperJS, which handles much of the integration for Unity Ads. Feel free to use it in your projects and modify it for your needs. I've also written a UnityScript version of the button example called UnityAdsButtonJS to show how you might use it within your own game code.
     
    EliasMasche and Salazar like this.
  8. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Thank you for the reply. I already managed to get it to work using C#, but it was a real puzzle implementing it correctly. It would have been helpful if there were more examples, especially concerning the video on demand.
     
  9. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    I'd like to see this but the link is broken. Is the example still available? Thanks
     
  10. unity-nikkolai

    unity-nikkolai

    Joined:
    Sep 18, 2014
    Posts:
    540
    Even better. In this demo project, I've written analogous JavaScript examples for all C# examples. You can find the JavaScript examples—as well as scenes that implement them—in Assets/UnityAdsDemo/JavaScript.

    Download the demo project and try them out for yourself. Feel free to use any part of the demo in your own project and modify the assets to fit your own needs.
     
    Salazar and EliasMasche like this.
  11. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Hey, Nikkolai.

    Your example links are all 404'd now. Any chance you could resurrect them? In particular, the JS implementation demo with your UnityAdsHelperJS and UnityAdsButtonJS scripts.

    Thanks!
     
  12. unity-nikkolai

    unity-nikkolai

    Joined:
    Sep 18, 2014
    Posts:
    540
    The new Unity Ads Helper project now has examples for UnityScript. The UnityAdsHelper script itself is written in C#.

    To use it with UnityScript, you'll need to first move the UnityAdsHelper directory to the Standard Assets directory. Then you can call the methods from UnityAdsHelper within your own scripts using UnityScript.

    For the time being, I recommend using the UnityAdsHelper since the methods in the Advertisement class are still being defined with default parameters (not supported by UnityScript) rather than method overrides. This version of the UnityAdsHelper script is compatible with the Unity Ads asset package and the version of Unity Ads available with Unity 5.2 through the Services window.

    See the README for code examples in both UnityScript and C#.
     
    ThinkAside likes this.
  13. ThinkAside

    ThinkAside

    Joined:
    May 2, 2014
    Posts:
    1
    Thanks a ton for this, If I didn't have a near complete game, I would have just switched to C# haha. Glad I found this, you rock.