using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
using UnityEngine.UI;
public class AdManager : MonoBehaviour {
RewardBasedVideoAd rewardBasedVideo;
static InterstitialAd interstitial;
string VideoID = "ca-app-pub-3888784411212422/2850896103";
string adUnitId = "ca-app-pub-3888784411212422/4158795262";
public static AdManager Instance;
void Start ()
{
Instance = this;
DontDestroyOnLoad(gameObject);
RequestRewardBasedVideo();
RequestInterstitial();
}
public void RequestRewardBasedVideo()
{
rewardBasedVideo = RewardBasedVideoAd.Instance;
rewardBasedVideo.LoadAd(new AdRequest.Builder().Build(), adUnitId);
}
public void RequestInterstitial()
{
interstitial = new InterstitialAd(VideoID);
interstitial.LoadAd(new AdRequest.Builder().Build());
}
public void ShowAd()
{
if(rewardBasedVideo.IsLoaded())
{
rewardBasedVideo.Show();
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}
}
public static void ShowInter()
{
showInterstitial(interstitial);
}
private void showAdd(RewardBasedVideoAd r)
{
if (r.IsLoaded())
{
//Subscribe to Ad event
r.Show();
r.OnAdRewarded += HandleRewardBasedVideoRewarded;
}
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
PlayerPrefs.SetInt("coin", PlayerPrefs.GetInt("coin") + 200);
GameObject.FindGameObjectWithTag("Coin").GetComponent<Text>().text = PlayerPrefs.GetInt("coin").ToString();
GameObject.FindGameObjectWithTag("Double").GetComponent<Button>().interactable = false;
Debug.Log("Pref: " + PlayerPrefs.GetInt("coin"));
}
static void showInterstitial(InterstitialAd i)
{
if (i.IsLoaded())
{
i.Show();
}
}
}
I am rewarding players with 200 coins, but somehow every time i get reward increased by 200. First time when player get rewarded he gets 200, next time he gets 400, next time 600 etc. I have tried to change code in many ways but no positive result.
Method that is called upon button click is ShowAd(). Every time when panel with button thats calls the method is shown i call RequestRewardBasedVideo().
adding and creating new objects is the answer,
for example the Void would have something like that
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
int CoinOld = PlayerPrefs.GetInt("coin");
CoinOld += 200;
PlayerPrefs.SetInt("coin", CoinOld);
GameObject.FindGameObjectWithTag("Coin").GetComponent<Text>().text = PlayerPrefs.GetInt("coin").ToString();
GameObject.FindGameObjectWithTag("Double").GetComponent<Button>().interactable = false;
Debug.Log("Pref: " + PlayerPrefs.GetInt("coin"));
}
Related
I have a gameObject that needs to call lots of other functions. I was hoping to do something like this
Where I can select the gameobject and script for it to run. I can't figure out how to do this.
I'm calling a function on button click for a gui.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class AvatarSystem : MonoBehaviour
{
public Button general;
public Button face;
public Button hair;
public Button eyebrows;
public Button clothing;
// Start is called before the first frame update
void Start()
{
var root = GetComponent<UIDocument>().rootVisualElement;
general = root.Q<Button>("general");
face = root.Q<Button>("face");
hair = root.Q<Button>("hair");
eyebrows = root.Q<Button>("eyebrows");
clothing = root.Q<Button>("clothing");
general.clicked += generalClicked;
face.clicked += faceClicked;
hair.clicked += hairClicked;
eyebrows.clicked += eyebrowsClicked;
clothing.clicked += clothingClicked;
}
void generalClicked()
{
}
void faceClicked()
{
}
void hairClicked()
{
}
void eyebrowsClicked()
{
}
void clothingClicked()
{
}
}
The 5 bottom functions should do something like the top image. Thanks
You can this script which creates buttons in the inspector.
This should be outside of your AvatarSystem class.
using Unity.Editor;
[CustomEditor(typeof(AvatarSystem))]
public class AvatarSystemGUI : Editor
{
public override void OnInspectorGUI()
{
AvatarSystem t = AvatarSystem(target);
if (GUILayout.Button("Test"))
{
t.ExampleMethod();
}
}
}
Let me know of any problems! :)
You want to use a Unity Event
You can use UnityEvent<T> to pass a dynamic type in your function.
Then you can use Unity IPointerClickHandler to capture a click on the object.
You can use AddListener of UnityEvent;
you can write the code like this;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class AvatarSystem : MonoBehaviour
{
public Button general;
public Button face;
public Button hair;
public Button eyebrows;
public Button clothing;
// Start is called before the first frame update
void Start()
{
var root = GetComponent<UIDocument>().rootVisualElement;
general = root.Q<Button>("general");
face = root.Q<Button>("face");
hair = root.Q<Button>("hair");
eyebrows = root.Q<Button>("eyebrows");
clothing = root.Q<Button>("clothing");
general.onClick.AddListener(() => {
this.generalClicked();
this.faceClicked();
this.hairClicked();
this.eyebrowsClicked();
this.clothingClicked();
}) ;
}
void generalClicked()
{
}
void faceClicked()
{
}
void hairClicked()
{
}
void eyebrowsClicked()
{
}
void clothingClicked()
{
}
}
In my project, the banner opens after the scene is reloaded, everything seems to be in order in the editor.
Everything works fine in the editor, I checked it, but it’s only worth building the project on Android, it doesn’t work there normally. It will appear only after the scene is reloaded, and then not immediately with a delay of 2-3 seconds.
My code:
using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;
public class BannerAd : MonoBehaviour
{
[SerializeField] BannerPosition _bannerPosition;
[SerializeField] private string _androidAdUnitId = "Banner_Android";
[SerializeField] private string _iOSAdUnitId = "Banner_iOS";
private string _adUnitId;
private void Awake()
{
_adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
? _iOSAdUnitId
: _androidAdUnitId;
}
private void Start()
{
Advertisement.Banner.SetPosition(_bannerPosition);
LoadBanner();
}
private IEnumerator LoadAdBanner()
{
yield return new WaitForSeconds(1f);
LoadBanner();
}
public void LoadBanner()
{
BannerLoadOptions options = new BannerLoadOptions
{
loadCallback = OnBannerLoaded,
errorCallback = OnBannerError
};
Advertisement.Banner.Load(_adUnitId, options);
}
private void OnBannerLoaded()
{
Debug.Log("Banner loaded");
ShowBannerAd();
}
private void OnBannerError(string message)
{
Debug.Log($"Banner Error: {message}");
}
public void ShowBannerAd()
{
BannerOptions options = new BannerOptions
{
clickCallback = OnBannerClicked,
hideCallback = OnBannerHidden,
showCallback = OnBannerShown
};
Advertisement.Banner.Show(_adUnitId, options);
}
public void HideBannerAd()
{
Advertisement.Banner.Hide();
}
private void OnBannerClicked() { }
private void OnBannerShown() { }
private void OnBannerHidden() { }
}
It might be the ad is still not initialized when you call LoadBanner so it doesn't work at first. The best is to call LoadBanner when Initialization is complete. Set _testMode to true or false based on your need.
public class BannerAd : MonoBehaviour, IUnityAdsInitializationListener
{
[SerializeField] bool _testMode = true;
private string _adUnitId;
void Awake()
{
InitializeAds();
}
public void InitializeAds()
{
_adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
? _iOSGameId
: _androidGameId;
Advertisement.Initialize(_adUnitId, _testMode, this);
}
public void OnInitializationComplete()
{
LoadBanner();
}
//rest of the code
}
}
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);
}
I checked many times and I can’t find where my mistake is, someone please help, I don’t understand what is happening
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LobbyManager : MonoBehaviourPunCallbacks
{
public Text LogText;
private void Start()
{
PhotonNetwork.NickName = "Player " + Random.Range(1000, 9999);
Log("Player's name is set to " + PhotonNetwork.NickName);
PhotonNetwork.AutomaticallySyncScene = true;
PhotonNetwork.GameVersion = "1";
PhotonNetwork.ConnectUsingSettings();
}
public override void OnConnectedToMaster()
{
Log("Connected to Master");
}
public void CreateRoom()
{
PhotonNetwork.CreateRoom(null, new Photon.Realtime.RoomOptions { MaxPlayers = 2 });
}
public void JoinRoom()
{
PhotonNetwork.JoinRandomRoom();
}
public override void onJoinedRoom()
{
Log("Joined the room");
PhotonNetwork.LoadLevel("Game");
}
private void Log(string message)
{
Debug.Log(message);
LogText.text += "\n";
LogText.text += message;
}
}
Error: Assets\LobbyManager.cs(40,26): error CS0115: 'LobbyManager.onJoinedRoom()': no suitable method found to override
I just can't understand where my mistake or what?? Help plssss guys
they have changed this in new updates, even if this is an old question, I would like to add the answer I found, as I just came across this question now !
public class NetworkManager: MonoBehaviourPunCallbacks
{
// Start is called before the first frame update
void Start()
{
ConnectToServer();
}
I've been trying to implement the Admob services on a game I developed. But the reward video system is giving me headaches.
When I launch the app, the video ad shows and gives the player the rewards I programed for.
But the second time I press the button to watch the video, nothing happens. The video does not load not show.
My code:
private RewardBasedVideoAd rewardBasedVideo;
public GameObject GameOverUI;
public GameObject RewardPanel;
public int count=0;
public static string OutputMessage
{
set { outputMessage = value; }
}
public static GoogleMobileAdsDemoScript instance = null;
public static GoogleMobileAdsDemoScript Instance
{
get{return instance;}
}
void Awake()
{
if (instance != null && instance != this)
{
Destroy(this.gameObject);
return;
}
else
{
instance = this;
}
DontDestroyOnLoad(this.gameObject);
}
public void Start()
{
// Get singleton reward based video ad reference.
this.rewardBasedVideo = RewardBasedVideoAd.Instance;
// RewardBasedVideoAd is a singleton, so handlers should only be registered once.
this.rewardBasedVideo.OnAdLoaded += this.HandleRewardBasedVideoLoaded;
this.rewardBasedVideo.OnAdFailedToLoad += this.HandleRewardBasedVideoFailedToLoad;
this.rewardBasedVideo.OnAdOpening += this.HandleRewardBasedVideoOpened;
this.rewardBasedVideo.OnAdStarted += this.HandleRewardBasedVideoStarted;
this.rewardBasedVideo.OnAdRewarded += this.HandleRewardBasedVideoRewarded;
this.rewardBasedVideo.OnAdClosed += this.HandleRewardBasedVideoClosed;
this.rewardBasedVideo.OnAdLeavingApplication += this.HandleRewardBasedVideoLeftApplication;
this.RequestRewardBasedVideo ();
}
//
// Returns an ad request with custom ad targeting.
private AdRequest CreateAdRequest()
{
return new AdRequest.Builder().Build();
}
private void RequestRewardBasedVideo()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-3940256099942544/5224354917";
#else
string adUnitId = "unexpected_platform";
#endif
this.rewardBasedVideo.LoadAd(this.CreateAdRequest(), adUnitId);
}
public void LoadAd()
{
RequestRewardBasedVideo();
}
public void ShowRewardBasedVideo()
{
if (this.rewardBasedVideo.IsLoaded())
{
count++;
PlayerPrefs.SetInt ("adstype", 1);
this.rewardBasedVideo.Show();
}
else
{
MonoBehaviour.print("Reward based video ad is not ready yet");
}
}
#region RewardBasedVideo callback handlers
public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
}
public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
RequestRewardBasedVideo ();
MonoBehaviour.print(
"HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
}
public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
}
public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
}
public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
RequestRewardBasedVideo();
MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
MonoBehaviour.print(
"HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " + type);
GameOverUI.SetActive(false);
RewardPanel.SetActive(true);
}
public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
}
public void ReceiveReward()
{
StartCoroutine(Restarttime());
}
IEnumerator Restarttime()
{
Time.timeScale = 1f;
PlayerPrefs.SetInt("ScoreReward", 1);
yield return new WaitForSeconds(0.2f);
RewardPanel.SetActive(false);
SceneManager.LoadScene("Game");
}
#endregion
}
Any help is welcome.
Thank you very much!