Chartboost ads not showing in Unity 3D Game - c#

I have a simple platformer game in which I'm using chartboost and unity ads t show ads and I it was working fine during the test mode but ever since I deployed to production and disabled the test mode in both chartboost and unity ads I noticed that my interstitial ads and videos don't load or show except once in a blue moon that too of a same game and then it start failing again.
I also noticed that my ads impression are quite low on the chartboost and Unity. Can you please tell me if I code for it correctly? I used the chartboost example an built my ad controller through it, oh and I'm using caching for ads and unless ad isn't cached already I won't show it.
Here's the code:
using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;
using ChartboostSDK;
using System;
public class AdsController : MonoBehaviour
{
public static AdsController instance;
// app id for unity apps
private const string _appId = "someID";
public bool canShowChartBoostInterstitial;
public bool canShowChartBoostVideo;
private void Awake()
{
MakeSingleton();
if (!canShowChartBoostInterstitial)
{
LoadChartBoostInterstitialAds();
}
if (!canShowChartBoostVideo)
{
LoadChartBoostVideoAds();
}
LoadUnityAds();
}
private void MakeSingleton()
{
if (instance != null)
{
Destroy(gameObject);
}
else
{
instance = this;
DontDestroyOnLoad(gameObject);
}
}
private void OnLevelWasLoaded()
{
if (Application.loadedLevelName == "LevelMenu")
{
if (GameController.instance.canShowAds)
{
if (canShowChartBoostInterstitial)
{
ShowChartBoostInterstitial();
}
else
{
LoadChartBoostInterstitialAds();
}
}
}
}
private void OnEnable()
{
Chartboost.didCompleteRewardedVideo += VideoCompleted;
Chartboost.didCacheInterstitial += DidCacheInterstitial;
Chartboost.didDismissInterstitial += DidDismissInterstitial;
Chartboost.didCloseInterstitial += DidCloseInterstitial;
Chartboost.didCacheRewardedVideo += DidCacheVideo;
Chartboost.didFailToLoadInterstitial += FailedToLoadInterstitial;
Chartboost.didFailToLoadRewardedVideo += FailedToLoadVideo;
}
private void OnDisable()
{
Chartboost.didCompleteRewardedVideo -= VideoCompleted;
Chartboost.didCacheInterstitial -= DidCacheInterstitial;
Chartboost.didDismissInterstitial -= DidDismissInterstitial;
Chartboost.didCloseInterstitial -= DidCloseInterstitial;
Chartboost.didCacheRewardedVideo -= DidCacheVideo;
Chartboost.didFailToLoadInterstitial -= FailedToLoadInterstitial;
Chartboost.didFailToLoadRewardedVideo -= FailedToLoadVideo;
}
public void VideoCompleted(CBLocation location, int reward)
{
canShowChartBoostVideo = false;
if (RewardController.instance != null)
{
RewardController.instance.VideoWatchedGiveUserAReward();
}
LoadChartBoostVideoAds();
}
public void DidCacheInterstitial(CBLocation location)
{
canShowChartBoostInterstitial = true;
}
public void DidDismissInterstitial(CBLocation location)
{
canShowChartBoostInterstitial = false;
LoadChartBoostVideoAds();
LoadChartBoostInterstitialAds();
}
public void DidCloseInterstitial(CBLocation location)
{
canShowChartBoostInterstitial = false;
LoadChartBoostVideoAds();
LoadChartBoostInterstitialAds();
}
public void DidCacheVideo(CBLocation location)
{
canShowChartBoostVideo = true;
}
private void FailedToLoadInterstitial(CBLocation location, CBImpressionError error)
{
canShowChartBoostInterstitial = false;
LoadChartBoostInterstitialAds();
}
private void FailedToLoadVideo(CBLocation location, CBImpressionError error)
{
canShowChartBoostVideo = false;
if (ShopMenuController.instance != null)
{
ShopMenuController.instance.FailedToLoadTheVideo();
}
LoadChartBoostVideoAds();
}
public void LoadChartBoostVideoAds()
{
Chartboost.cacheRewardedVideo(CBLocation.Default);
}
public void LoadChartBoostInterstitialAds()
{
Chartboost.cacheInterstitial(CBLocation.Default);
}
public void ShowChartBoostInterstitial()
{
if (canShowChartBoostInterstitial)
{
Chartboost.showInterstitial(CBLocation.Default);
}
else
{
LoadChartBoostInterstitialAds();
}
}
public void ShowChartBoostVideo()
{
if (canShowChartBoostVideo)
{
Chartboost.showRewardedVideo(CBLocation.Default);
}
else
{
LoadChartBoostVideoAds();
}
}
public void LoadUnityAds()
{
if (Advertisement.isSupported)
{
Advertisement.Initialize(_appId, false);
}
}
public void ShowUnityAds()
{
if (Advertisement.IsReady())
{
Advertisement.Show(null, new ShowOptions()
{
resultCallback = result =>
{
switch (result)
{
case ShowResult.Finished:
GameController.instance.RewardPlayerWithSomething();
LoadUnityAds();
break;
case ShowResult.Failed:
GameController.instance.VideoNotLoadedOrUserSkippedTheVideo("Failed to load the video. Please try again.");
LoadUnityAds();
break;
case ShowResult.Skipped:
GameController.instance.VideoNotLoadedOrUserSkippedTheVideo("Video skipped.");
LoadUnityAds();
break;
}
}
});
}
else
{
GameController.instance.VideoNotLoadedOrUserSkippedTheVideo("Failed to load the video. Please try again.");
LoadUnityAds();
}
}
}

Run cache after every time you show an interstitial.
like this :
Chartboost.showInterstitial(CBLocation.Default);
Chartboost.cacheInterstitial(CBLocation.Default);
That way you will replenish the cache every time you show an ad.
Remember to cache as soon as its initialized as well.

Related

Unity new Input system not being triggered

I'm having a weird issue where any input is not being triggered 'at all'.
Pulling my hairs out; can't find out where this is wrong.
Result is that nothing is being written; although all things are set.
In the input asset I have added the 'Interact' and 'Application Quit' entries.
Anyone could have a glimpse of what's wrong here ?
public class InputManager : MonoBehaviour
{
[SerializeField] private InputActionAsset _actions;
public InputActionAsset actions
{
get => _actions;
set => _actions = value;
}
private InputAction quitInputAction { get; set; }
private InputAction interactInputAction { get; set; }
private void OnEnable()
{
quitInputAction?.Enable();
interactInputAction?.Enable();
Setup();
}
private void Setup()
{
interactInputAction = actions.FindAction("Interact");
if (interactInputAction != null)
{
interactInputAction.started += OnInteract;
interactInputAction.performed += OnInteract;
interactInputAction.canceled += OnInteract;
}
else
{
Debug.LogError("Missing Interact Binding");
}
quitInputAction = actions.FindAction("Application Quit");
if (quitInputAction != null)
{
quitInputAction.started += OnAppQuit;
quitInputAction.performed += OnAppQuit;
quitInputAction.canceled += OnAppQuit;
}
else
{
Debug.LogError("Missing Application Quit Binding");
}
}
protected virtual void OnAppQuit(InputAction.CallbackContext context)
{
if (context.started || context.performed)
{
Debug.Log("Quit");
Application.Quit();
}
else if (context.canceled)
Debug.Log("Application Quit Cancelled");
}
protected virtual void OnInteract(InputAction.CallbackContext context)
{
if (context.started || context.performed)
{
Debug.Log("Interact");
}
else if (context.canceled)
Debug.Log("Application Quit Cancelled");
}
}
You need to run your setup before attempting to enable them:
private void OnEnable()
{
Setup();
quitInputAction?.Enable();
interactInputAction?.Enable();
}
Source: Disco Fever

Not working leaderboard Google Play Service

added a leaderboard to the Google Play Service. Resources are added, authorization is working, adding a record is working. But when use ShowLeaderboardUI() only shows the name of the highscore table, but there are no highscores. Help pls ;(
CODE Ads Init:
public class AdsInitialize : MonoBehaviour
{
public static AdsInitialize instance = null;
private string LEADER_BOARD = "******";
public Text _text;
private void Awake()
{
if (instance == null)
{
instance = this;
MobileAds.Initialize(initStatus => { });
PlayGamesPlatform.DebugLogEnabled = true;
PlayGamesPlatform.Activate();
Social.localUser.Authenticate(success =>
{
if (success)
{
_text.text = "Auth working";
}
else
{
_text.text = "Auth not working";
}
});
}
else if (instance != this)
{
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
}
public void ShowLeaderBoard()
{
//Social.ShowLeaderboardUI();
PlayGamesPlatform.Instance.ShowLeaderboardUI(LEADER_BOARD);
}
}
Write highscores:
Social.ReportScore(_time, AdsInitialize.instance.LEADER_BOARD, (bool success) => {
if (success)
{
_text.text = "ReportScore working";
}
else
{
_text.text = "ReportScore not working";
}
});
Show leaderboard in menu (this method is hanging on button):
public void ShowLeaderBoard()
{
AdsInitialize.instance.ShowLeaderBoard();
}
You need update your Google Play Games at phone

Can someone explain how to fix the error?

Ok, so I found an open source script for Unity and it kinda don't want to be friends with the new input system. I have no idea how to replace InputActionAssetReference
If somebody helps - thank you. The errors are: Assets\Input\InputMaster.cs(7,28): error CS0246: The type or namespace name 'InputActionAssetReference' could not be found (are you missing a using directive or an assembly reference?) and Assets\Input\InputMaster.cs(50,26): error CS0115: 'InputMaster.MakePrivateCopyOfActions()': no suitable method found to override
I'd really like the help because I want to use this 2D platformer controller. The script:
using System;
using UnityEngine;
using UnityEngine.InputSystem;
[Serializable]
public class InputMaster : InputActionAssetReference
{
public InputMaster()
{
}
public InputMaster(InputActionAsset asset)
: base(asset)
{
}
[NonSerialized] private bool m_Initialized;
private void Initialize()
{
// Player
m_Player = asset.GetActionMap("Player");
m_Player_Movement = m_Player.GetAction("Movement");
m_Player_Jump = m_Player.GetAction("Jump");
m_Player_Dash = m_Player.GetAction("Dash");
m_Player_Interact = m_Player.GetAction("Interact");
m_Player_AttackA = m_Player.GetAction("Attack A");
m_Initialized = true;
}
private void Uninitialize()
{
if (m_PlayerActionsCallbackInterface != null)
{
Player.SetCallbacks(null);
}
m_Player = null;
m_Player_Movement = null;
m_Player_Jump = null;
m_Player_Dash = null;
m_Player_Interact = null;
m_Player_AttackA = null;
m_Initialized = false;
}
public void SetAsset(InputActionAsset newAsset)
{
if (newAsset == asset) return;
var PlayerCallbacks = m_PlayerActionsCallbackInterface;
if (m_Initialized) Uninitialize();
asset = newAsset;
Player.SetCallbacks(PlayerCallbacks);
}
public override void MakePrivateCopyOfActions()
{
SetAsset(ScriptableObject.Instantiate(asset));
}
// Player
private InputActionMap m_Player;
private IPlayerActions m_PlayerActionsCallbackInterface;
private InputAction m_Player_Movement;
private InputAction m_Player_Jump;
private InputAction m_Player_Dash;
private InputAction m_Player_Interact;
private InputAction m_Player_AttackA;
public struct PlayerActions
{
private InputMaster m_Wrapper;
public PlayerActions(InputMaster wrapper) { m_Wrapper = wrapper; }
public InputAction #Movement { get { return m_Wrapper.m_Player_Movement; } }
public InputAction #Jump { get { return m_Wrapper.m_Player_Jump; } }
public InputAction #Dash { get { return m_Wrapper.m_Player_Dash; } }
public InputAction #Interact { get { return m_Wrapper.m_Player_Interact; } }
public InputAction #AttackA { get { return m_Wrapper.m_Player_AttackA; } }
public InputActionMap Get() { return m_Wrapper.m_Player; }
public void Enable() { Get().Enable(); }
public void Disable() { Get().Disable(); }
public bool enabled { get { return Get().enabled; } }
public InputActionMap Clone() { return Get().Clone(); }
public static implicit operator InputActionMap(PlayerActions set) { return set.Get(); }
public void SetCallbacks(IPlayerActions instance)
{
if (m_Wrapper.m_PlayerActionsCallbackInterface != null)
{
Movement.started -= m_Wrapper.m_PlayerActionsCallbackInterface.OnMovement;
Movement.performed -= m_Wrapper.m_PlayerActionsCallbackInterface.OnMovement;
Movement.cancelled -= m_Wrapper.m_PlayerActionsCallbackInterface.OnMovement;
Jump.started -= m_Wrapper.m_PlayerActionsCallbackInterface.OnJump;
Jump.performed -= m_Wrapper.m_PlayerActionsCallbackInterface.OnJump;
Jump.cancelled -= m_Wrapper.m_PlayerActionsCallbackInterface.OnJump;
Dash.started -= m_Wrapper.m_PlayerActionsCallbackInterface.OnDash;
Dash.performed -= m_Wrapper.m_PlayerActionsCallbackInterface.OnDash;
Dash.cancelled -= m_Wrapper.m_PlayerActionsCallbackInterface.OnDash;
Interact.started -= m_Wrapper.m_PlayerActionsCallbackInterface.OnInteract;
Interact.performed -= m_Wrapper.m_PlayerActionsCallbackInterface.OnInteract;
Interact.cancelled -= m_Wrapper.m_PlayerActionsCallbackInterface.OnInteract;
AttackA.started -= m_Wrapper.m_PlayerActionsCallbackInterface.OnAttackA;
AttackA.performed -= m_Wrapper.m_PlayerActionsCallbackInterface.OnAttackA;
AttackA.cancelled -= m_Wrapper.m_PlayerActionsCallbackInterface.OnAttackA;
}
m_Wrapper.m_PlayerActionsCallbackInterface = instance;
if (instance != null)
{
Movement.started += instance.OnMovement;
Movement.performed += instance.OnMovement;
Movement.cancelled += instance.OnMovement;
Jump.started += instance.OnJump;
Jump.performed += instance.OnJump;
Jump.cancelled += instance.OnJump;
Dash.started += instance.OnDash;
Dash.performed += instance.OnDash;
Dash.cancelled += instance.OnDash;
Interact.started += instance.OnInteract;
Interact.performed += instance.OnInteract;
Interact.cancelled += instance.OnInteract;
AttackA.started += instance.OnAttackA;
AttackA.performed += instance.OnAttackA;
AttackA.cancelled += instance.OnAttackA;
}
}
}
public PlayerActions #Player
{
get
{
if (!m_Initialized) Initialize();
return new PlayerActions(this);
}
}
private int m_KeyboardSchemeIndex = -1;
public InputControlScheme KeyboardScheme
{
get
{
if (m_KeyboardSchemeIndex == -1) m_KeyboardSchemeIndex = asset.GetControlSchemeIndex("Keyboard");
return asset.controlSchemes[m_KeyboardSchemeIndex];
}
}
private int m_GamepadSchemeIndex = -1;
public InputControlScheme GamepadScheme
{
get
{
if (m_GamepadSchemeIndex == -1) m_GamepadSchemeIndex = asset.GetControlSchemeIndex("Gamepad");
return asset.controlSchemes[m_GamepadSchemeIndex];
}
}
}
public interface IPlayerActions
{
void OnMovement(InputAction.CallbackContext context);
void OnJump(InputAction.CallbackContext context);
void OnDash(InputAction.CallbackContext context);
void OnInteract(InputAction.CallbackContext context);
void OnAttackA(InputAction.CallbackContext context);
}
If there's too much to edit then can someone say what's the last supported version that has InputActionAssetReference?
Edited message --
I am replacing my answer without links as I have been told to. Basically instead of using InputActionAssetReference, another class like IInputActionCollection should be used. The first class must have been deprecated which is why it wasn't working properly.
Note: I have never used this new input system myself, but I am glad I was able to help! I get to learn something too which is a bonus.
InputActionAssetReference was removed at release [0.2.8-preview] - 2019-4-23
Generated wrapper code for Input Action Assets are now self-contained,
generating all the data from code and not needing a reference to the
asset; InputActionAssetReference has been removed.
link to changelog

Rewarded video ads events not firing in Unity and Admob

I am using Unity 2019.2.8f1 and I am preparing to publish my first game on Google Play Store. Unfortunately the reward is not being given to the user after watching a rewarded ad. The user should get an extra life whenever he/she watches a rewarded ad.
Here is my Ad Manager script. I am using dummy ids for testing.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
using System;
public class AdManager : MonoBehaviour
{
public static AdManager instance;
private string AppId = "ca-app-pub-3940256099942544~3347511713";
private BannerView banner;
private string bannerID = "ca-app-pub-3940256099942544/6300978111";
private InterstitialAd videoAD;
private string videoID = "ca-app-pub-3940256099942544/1033173712";
private int showing, video;
private RewardBasedVideoAd rewardedAds;
private string rewardedID = "ca-app-pub-3940256099942544/5224354917";
public bool RewardTaken;
public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
Debug.Log("HandleRewardBasedVideoClosed event received");
RequestRewardedAD();
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
UIManager.variable.RewardPanel.SetActive(true);
UIManager.variable.gameOver.SetActive(false);
}
private void Awake()
{
rewardedAds = RewardBasedVideoAd.Instance;
showing = PlayerPrefs.GetInt("show");
video = PlayerPrefs.GetInt("video");
MobileAds.Initialize(AppId);
RequestvideoAD();
if (instance == null)
{
instance = this;
}
else
{
Destroy(this);
}
}
private void Start()
{
RewardTaken = false;
if (video == 1)
{
ShowvideoAD();
}
if (showing == 1)
{
requestBanner();
}
RequestRewardedAD();
// Called when the user should be rewarded for watching a video.
rewardedAds.OnAdRewarded += HandleRewardBasedVideoRewarded;
// Called when the ad is closed.
rewardedAds.OnAdClosed += HandleRewardBasedVideoClosed;
}
public void requestBanner()
{
banner = new BannerView(bannerID, AdSize.Banner, AdPosition.Bottom);
AdRequest request = new AdRequest.Builder().Build();
banner.LoadAd(request);
banner.Show();
}
public void RequestvideoAD()
{
videoAD = new InterstitialAd(videoID);
AdRequest request = new AdRequest.Builder().Build();
videoAD.LoadAd(request);
}
public void ShowvideoAD()
{
if (videoAD.IsLoaded())
{
videoAD.Show();
}
else
{
Debug.Log("FullscreenADNotLoaded");
}
}
public void RequestRewardedAD()
{
AdRequest request = new AdRequest.Builder().Build();
rewardedAds.LoadAd(request,rewardedID);
}
public void ShowRewardedAD()
{
if(rewardedAds.IsLoaded())
{
rewardedAds.Show();
Debug.Log("Ads Are Working");
}
else
{
Debug.Log("Rewarded Ads not laded properly");
}
}
public void HideAD()
{
banner.Hide();
}
}
Even though the console was not showing any message that I wanted to show as a part of the test. After trying several times, I decided to remove the debug text.
It looks like you are attaching Callbacks to a different ad object and you are loading ads from a different object.
Try using rewardedAds
to Load Ad
public void RequestvideoAD()
{
AdRequest request = new AdRequest.Builder().Build();
rewardedAds.LoadAd(request);
}

Xamarin IOS: unable to play mp3 from internet

I read a lot of documentation regarding this problem, but I don't seen any answer.
I have a Xamarin form app that play mp3 sample music from the Groove music service.
On Android, everyhting workd fine.
On Ios, I'm not able to play the sound. I get the URL and it's not possible to ear anything (I was able to play a local mp3)
I see this problem occured also for people using SWIFT : How to play MP3 From URL in iOS
I also found several exemple on the Xamarin forum but no sound neither: https://forums.xamarin.com/discussion/19883/how-do-i-get-the-avplayer-to-play-an-mp3-from-a-remote-url
Here is the code I use:
public class AudioService : IAudio
{
protected string FileName = string.Empty;
protected AVPlayer Player;
public bool IsPlaying { get; private set; }
public void Init(string fileName)
{
//FileName = fileName;
string second = "http://progdownload.zune.net/145/119/034/170/audio.mp3?rid=0b80911a-ba3b-42ec-b17f-c242ba087024-s4-nl-BE-music-asset-location";
FileName = second;
QueueFile();
}
public async void PlayStream(string uri)
{
Init(uri);
System.Diagnostics.Debug.WriteLine("Enter in function");
Player.Play();
System.Diagnostics.Debug.WriteLine("Play sound");
System.Diagnostics.Debug.WriteLine(FileName);
IsPlaying = true;
System.Diagnostics.Debug.WriteLine(IsPlaying);
}
public void Pause()
{
IsPlaying = false;
Player.Pause();
}
public void Stop()
{
IsPlaying = false;
if (Player == null) return;
Player.Dispose();
Player = null;
}
public bool HasFile
{
get { return Player != null; }
}
private void QueueFile()
{
if (string.IsNullOrWhiteSpace(FileName))
{
throw new Exception("No file specified to play");
}
using (var url = NSUrl.FromString(FileName))
{
var test = AVAsset.FromUrl(url);
var playerItem = AVPlayerItem.FromAsset(test);
// if Player is null, we're creating a new instance and seeking to the spot required
// otherwise we simply resume from where we left off.
if (Player == null)
{
Player = AVPlayer.FromPlayerItem(playerItem);
if (Player == null)
{
// todo: what should we do if the file doesn't exist?
return;
}
}
}
}}
(IAudio just implements playstream and stop)
If you click on the url, your browser will be able to play music
http://progdownload.zune.net/145/119/034/170/audio.mp3?rid=0b80911a-ba3b-42ec-b17f-c242ba087024-s4-nl-BE-music-asset-location
Does any one was able to play mp3 from internet
The answer in the info.list file. You need to add this to your info.list:
<key>NSAppTransportSecurity</key><dict>
<key>NSAllowsArbitraryLoads</key>
<true/></dict>
I had the same issue and resolved it.
I've made some cleaning for your code :)
This is what we got, including the detecting the end of playing
public class AudioService
{
#region Members
protected AVPlayer Player;
#endregion
#region Properties
public bool IsPlaying { get; private set; }
public bool HasFile => Player != null;
public Action MediaEnded { get; set; }
#endregion
#region Public Methods
public void PlayStream(string uri)
{
try
{
if (Player != null)
Player.Dispose();
Player = null;
QueueFile(uri);
Player.Play();
IsPlaying = true;
}
catch (Exception ex)
{
IsPlaying = false;
Crashes.TrackError(ex);
}
}
public void Pause()
{
IsPlaying = false;
Player.Pause();
}
public void Stop()
{
IsPlaying = false;
if (Player == null)
return;
Player.Dispose();
Player = null;
}
public void QueueFile(string fileName)
{
if (string.IsNullOrWhiteSpace(fileName))
{
return;
}
using (var url = NSUrl.FromString(fileName))
{
var asset = AVAsset.FromUrl(url);
var playerItem = AVPlayerItem.FromAsset(asset);
if (Player == null)
{
Player = AVPlayer.FromPlayerItem(playerItem);
if (Player == null)
{
return;
}
else
{
NSNotificationCenter.DefaultCenter.AddObserver(AVPlayerItem.DidPlayToEndTimeNotification, OnPlayerDidFinishPlaying, NSObject.FromObject(playerItem));
}
}
}
}
#endregion
#region Private Methods
private void OnPlayerDidFinishPlaying(NSNotification notification)
{
IsPlaying = false;
MediaEnded?.Invoke();
}
#endregion

Categories

Resources