-1
\$\begingroup\$

I'm new to Unity and I'm a beginner. Unity ads have completed, but instead of the letter "E" and "T" I want the ad to come out by clicking the button. how can I write a code for it?

using System.Collections.Generic; using UnityEngine; using UnityEngine.Monetization;

public class AdController : MonoBehaviour { private string store_id = "3049359";

private string video_ad = "video";
private string banner_ad = "bannerAd";

void Start()
{
    Monetization.Initialize (store_id, true);

}

void Update()
{
    if (Input.GetKeyDown (KeyCode.E)) {
        if (Monetization.IsReady (video_ad)) {

            ShowAdPlacementContent ad = null;
            ad = Monetization.GetPlacementContent (video_ad) as ShowAdPlacementContent;
            if (ad != null) {
                ad.Show ();
            }


        }
    }
        if (Input.GetKeyDown (KeyCode.T)) {
        if (Monetization.IsReady (banner_ad)) {

            ShowAdPlacementContent ad = null;
            ad = Monetization.GetPlacementContent (video_ad) as ShowAdPlacementContent;
            if (ad != null) {
                ad.Show ();
}

} } } }

\$\endgroup\$
5
  • \$\begingroup\$ Have you tried checking out the documentation on using Button controls in Unity, in particular adding a listener to an OnClick event? \$\endgroup\$ Commented Feb 19, 2019 at 0:33
  • \$\begingroup\$ worked, but this time it does not build. it was working before placing ads. gives me this error: "job failed, see logs for details see the console for details." \$\endgroup\$ Commented Feb 19, 2019 at 0:58
  • \$\begingroup\$ Build completed with a result of 'Failed' UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr) \$\endgroup\$ Commented Feb 19, 2019 at 0:59
  • \$\begingroup\$ UnityEditor.BuildPlayerWindow+BuildMethodException: 2 errors at UnityEditor.BuildPlayerWindow+DefaultBuildMethods.BuildPlayer (BuildPlayerOptions options) [0x00234] in C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:190 at UnityEditor.BuildPlayerWindow.CallBuildMethods (Boolean askForBuildLocation, BuildOptions defaultBuildOptions) [0x0007f] in C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:96 UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr) \$\endgroup\$ Commented Feb 19, 2019 at 0:59
  • \$\begingroup\$ In regards to the error. If the project built on a android device before you included the ads then make sure the services are correctly enabled. If not then you should make sure to use a up to date sdk, jdk and ndk to point to in the Edit/Preferences/ tab. This sometimes can be troublesome with the stand alone downloads. I like to update those using the built in manager within android studio and point to those in the preferences then run the build in Unity and it should not throw the build errors anymore. \$\endgroup\$ Commented Feb 20, 2019 at 16:26

2 Answers 2

1
\$\begingroup\$

Place the code in the if statements in a new void and call that void from the button

public void ShowAd(){
 [Your code here]
}

Then go to the button, by "On Click" click on the +.

enter image description here

Select the GameObject the ads-script is on. Then select [your script name] and the ShowAd function.

enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ It'd be great to see your ShowAd function in the list. :) \$\endgroup\$ Commented Mar 22, 2019 at 13:35
  • \$\begingroup\$ @DMGregory Yes but I wrote that out of my head. I don't have such a function in my unity project \$\endgroup\$ Commented Mar 22, 2019 at 14:01
  • \$\begingroup\$ Right, so, take thirty seconds to write one for the sake of the example? There's no rule that says all answers must use existing production code — often writing up a one-off example for the sake of an answer is a good way to demonstrate what you mean. \$\endgroup\$ Commented Mar 22, 2019 at 14:02
  • \$\begingroup\$ @DMGregory I don't really want to wait 4 minutes for unity to open just to write one function for this answer. \$\endgroup\$ Commented Mar 22, 2019 at 14:05
0
\$\begingroup\$

Go to unity ads page. Copy the c# script . Paste that. In project services enable unity ads. It is that much simple . Only 2 mins

\$\endgroup\$
1
  • \$\begingroup\$ Assume that at some time in the future, the Unity Ads page might change its content. Please make sure all the information needed to follow the steps you're recommending is present inside your answer itself. \$\endgroup\$ Commented Feb 20, 2019 at 17:55

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.