Unity Banner is not showing - unity3d

My Banner Ad does show in test mode but without testmode not.
I used a code template from the unity website.
I also asked the unity ads support but they couldn't find the error.
they send me a video of my application,and in this video the banner ad worked.
Here is my code:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
public class newadsystem : MonoBehaviour
{
// For the purpose of this example, these buttons are for functionality testing:
[SerializeField] Button _loadBannerButton;
[SerializeField] Button _showBannerButton;
[SerializeField] Button _hideBannerButton;
[SerializeField] BannerPosition _bannerPosition = BannerPosition.BOTTOM_CENTER;
[SerializeField] public string _androidAdUnitId = "Banner_Android";
[SerializeField] string _iOsAdUnitId = "Banner_iOS";
public string _adUnitId = "Banner_Android";
void Start()
{
Advertisement.Initialize("4395157",false,true);
// Disable the button until an ad is ready to show:
_showBannerButton.interactable = true;
_hideBannerButton.interactable = true;
// Set the banner position:
Advertisement.Banner.SetPosition(_bannerPosition);
// Configure the Load Banner button to call the LoadBanner() method when clicked:
_loadBannerButton.onClick.AddListener(LoadBanner);
_loadBannerButton.interactable = true;
}
// Implement a method to call when the Load Banner button is clicked:
public void LoadBanner()
{
// Set up options to notify the SDK of load events:
BannerLoadOptions options = new BannerLoadOptions
{
loadCallback = OnBannerLoaded,
errorCallback = OnBannerError
};
// Load the Ad Unit with banner content:
Advertisement.Initialize("4395157", false, true);
Advertisement.Banner.Load(_adUnitId, options);
Advertisement.Initialize("4395157", false, true);
}
// Implement code to execute when the loadCallback event triggers:
void OnBannerLoaded()
{
Debug.Log("Banner loaded");
// Configure the Show Banner button to call the ShowBannerAd() method when clicked:
_showBannerButton.onClick.AddListener(ShowBannerAd);
// Configure the Hide Banner button to call the HideBannerAd() method when clicked:
_hideBannerButton.onClick.AddListener(HideBannerAd);
// Enable both buttons:
_showBannerButton.interactable = true;
_hideBannerButton.interactable = true;
}
// Implement code to execute when the load errorCallback event triggers:
void OnBannerError(string message)
{
Debug.Log($"Banner Error: {message}");
// Optionally execute additional code, such as attempting to load another ad.
}
// Implement a method to call when the Show Banner button is clicked:
void ShowBannerAd()
{
// Set up options to notify the SDK of show events:
BannerOptions options = new BannerOptions
{
clickCallback = OnBannerClicked,
hideCallback = OnBannerHidden,
showCallback = OnBannerShown
};
// Show the loaded Banner Ad Unit:
Advertisement.Initialize("4395157", false, true);
Advertisement.Banner.Show(_adUnitId, options);
}
// Implement a method to call when the Hide Banner button is clicked:
void HideBannerAd()
{
// Hide the banner:
Advertisement.Banner.Hide();
}
void OnBannerClicked()
{
Debug.Log("Banner Clicked!");
}
void OnBannerShown()
{
Debug.Log("Banner Shown!");
}
void OnBannerHidden()
{
Debug.Log("Banner Hidden");
}
void OnDestroy()
{
// Clean up the listeners:
_loadBannerButton.onClick.RemoveAllListeners();
_showBannerButton.onClick.RemoveAllListeners();
_hideBannerButton.onClick.RemoveAllListeners();
}
}
I looked into the android device log and there was: "Unity : Banner Error: UnityAds is not initialized."
but i call several times the initialize function.
PS: i am not very good at english.

Have a look at Start (), your code:
Advertisement.Initialize("4395157",false,true);
// Disable the button until an ad is ready to show:
_showBannerButton.interactable = true;
_hideBannerButton.interactable = true;
And then at OnBannerLoaded ()
// Enable both buttons:
_showBannerButton.interactable = true;
_hideBannerButton.interactable = true;
But those button are already interactable = true.
Maybe you need to change Start () like this:
Advertisement.Initialize("4395157",false,true);
// Disable the button until an ad is ready to show:
_showBannerButton.interactable = false;
_hideBannerButton.interactable = false;
Update 1
Try to change your code the following way:
void Start()
{
_showBannerButton.interactable = false;
_hideBannerButton.interactable = false;
Advertisement.Initialize("4395157",false,true);
_loadBannerButton.onClick.AddListener(LoadBanner);
_showBannerButton.onClick.AddListener(ShowBannerAd);
_hideBannerButton.onClick.AddListener(HideBannerAd);
_loadBannerButton.interactable = true;
}
public void LoadBanner()
{
BannerLoadOptions options = new BannerLoadOptions
{
loadCallback = OnBannerLoaded,
errorCallback = OnBannerError
};
if (Advertisement.isInitialized) {
Advertisement.Banner.Load(_adUnitId, options);
} else Debug.LogWarning ("Adverisement not initialized! Try again later");
}
void OnBannerLoaded()
{
Debug.Log("Banner loaded");
BannerOptions options = new BannerOptions
{
clickCallback = OnBannerClicked,
hideCallback = OnBannerHidden,
showCallback = OnBannerShown
};
Advertisement.Banner.SetPosition(_bannerPosition);
_showBannerButton.interactable = true;
_hideBannerButton.interactable = true;
}
void ShowBannerAd()
{
Advertisement.Banner.Show(_adUnitId, options);
}

Related

How to trigger a button click for deviceorientation in WebGL

I have a button to open the browser to fullscreen mode. This is the code:
private void ResizeButton_OnClick (UberButton obj)
{
if (!Screen.fullScreen)
Screen.fullScreen = true;
else
Screen.fullScreen = false;
}
How can I implement automatic button pressing when the screen orientation changes
private void Update()
{
if (Input.deviceOrientation == DeviceOrientation.LandscapeLeft)
{
// ...
}
}

How to define gravity of a Dialog Fragment?

I want my dialog to stick to the start but it always comes in the center.
i will give you example==>
Following is the code -->Onclick image -->Dialog Appears==>
Dialog dialog; //your dialog declaration
//
//
//
oncreate(){
imageView = (ImageView) findViewById(R.id.customProfileGridImg);
dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.activity);
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.START |Gravity.TOP; //change direction as per your requirement
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.show();
}
});
}

Unity Crashes when Using this UI

using UnityEngine.UI;
using UnityEngine;
public class Tutorial : MonoBehaviour
{
public Text tutorialText;
void Update()
{
tutorialText.text = "Press [SPACE] to Jump... (obviously...)";
if (Input.GetKeyDown(KeyCode.Space))
{
while (true)
{
tutorialText.text = "Now Shoot";
if (Input.GetKeyDown(KeyCode.Mouse0))
{
while (true)
{
tutorialText.text = "Nice";
}
}
}
}
}
}
Change your code to that, because right now you are in a infinite loop which sets your tutorialtext to "Now Shoot" again and again until Unity crashes.
if (Input.GetKeyDown(KeyCode.Space))
{
tutorialText.text = "Now Shoot";
}
else if (Input.GetKeyDown(KeyCode.Mouse0))
{
tutorialText.text = "Nice";
}
If you want the text to disappear after the first time you done the actions you should add bool variables.
bool jumped = false;
bool hasShot = false;
And then implement thoose bools into your code like this:
if (Input.GetKeyDown(KeyCode.Space))
{
if (jumped)
return;
tutorialText.text = "Now Shoot";
jumped = true;
}
else if (Input.GetKeyDown(KeyCode.Mouse0))
{
if (hasShot || !jumped)
return;
tutorialText.text = "Nice";
hasShot = true;
}
You also need to move the first text definition out of Update, because Update is called each frame. Put it into void Start() instead
void Start() {
tutorialText.text = "Press [SPACE] to Jump... (obviously...)";
}

Unity3D GUI.Label is not displayed

I am currently trying to make "Welcome!" string and a button appear on screen by using the GUI Label and GUILayout Button, but it does not appear and there are not any errors that I know of. Unity emulator is ok, but the "Welcome!" string is not displayed in android.
Here is my code, can someone please tell me my problem and if I might have forgotten to do something.
public class MainMenu : MonoBehaviour
{
public GUISkin Skin;
public Texture btnTexture;
public void Awake() { }
public void OnGUI()
{
if (this.Skin != null)
{
GUI.skin = this.Skin;
}
GUI.skin.button.fontSize = 30;
Rect content = new Rect(30, 100, Screen.width-60, Screen.height-200);
GUILayout.BeginArea(content);
GUILayout.BeginHorizontal();
GUIStyle TextStyle = new GUIStyle(GUI.skin.label);
TextStyle.fontSize = 50;
// Load and set Font
Font myFont = (Font)Resources.Load("font/stingray", typeof(Font));
GUI.skin.font = myFont;
// Set color for selected and unselected
TextStyle.normal.textColor = Color.yellow;
TextStyle.hover.textColor = Color.yellow;
// Display and set the style in string
GUI.Label(new Rect(50,50,300,70), "Welcome!", TextStyle);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (GUILayout.Button("Join", GUILayout.Width(300), GUILayout.Height(80)))
{
SceneManager.LoadSceneAsync("SingleMenu");
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
void Start () { }
void Update () { }
}

MovieTexture Never Plays In Build

I have written a script to play OGG files as a MovieTexture, i've tried both embedded asset files and also from the web. The problem I'm having is that the videos play in debug move (When I hit the play button and test in the "Game" tab) but they never work when I build to an executable..
using UnityEngine;
namespace Assets.Scripts
{
[RequireComponent(typeof(AudioSource))]
public class VideoScreen : MonoBehaviour
{
public string videoUrl;
public bool autoPlay = true;
public bool loop = true;
public bool playAudio = true;
public float opacity = 1.0f;
private bool hasLoaded;
private MovieTexture movieTexture;
public void Start ()
{
if (string.IsNullOrEmpty(videoUrl))
{
return;
}
var data = new WWW(videoUrl);
movieTexture = data.movie as MovieTexture;
}
public void Update()
{
if (movieTexture.isReadyToPlay && !hasLoaded)
{
renderer.material = new Material(Shader.Find("Custom/Unlit Transparent Color")) { mainTexture = movieTexture };
audio.clip = movieTexture.audioClip;
SetLoop(loop);
if (autoPlay)
{
Play();
}
hasLoaded = true;
}
var textureColor = renderer.material.color;
textureColor.a = opacity;
renderer.material.color = textureColor;
}
private void Play()
{
movieTexture.Stop();
movieTexture.Play();
if (playAudio)
{
audio.Stop();
audio.Play();
}
}
private void Pause()
{
movieTexture.Pause();
if (playAudio)
{
audio.Pause();
}
}
private void Stop()
{
movieTexture.Stop();
if (playAudio)
{
audio.Stop();
}
}
private void SetLoop(bool loopStatus)
{
movieTexture.loop = loopStatus;
if (playAudio)
{
audio.loop = loopStatus;
}
}
}
}
Can anyone shed any light on this behaviour?
Thanks
Turns out the custom Transparent Diffuse shader I was using was causing the issue! Doh!!
renderer.material = new Material(Shader.Find("Custom/Unlit Transparent Color")) { mainTexture = movieTexture };
now becomes
renderer.material.mainTexture = movieTexture;