Notifications
Article
Getting Started With "Unity Ads"
Published 4 months ago
43
0
Unity Ads

Overview:

"Unity Ads" is a great tool for generating revenue from your game. Currently, "Unity Ads" service provide "Video Ads" (Which is kind of like 'Interstitial Ads' on the other ad platform) & "Reward Video Ads" where you can reward user once they finished up watching the advertisement. So far, "Unity Ads" is only available for "Android" & "iOS" platform.

Show Me The Money:

"Unity Ads" share a great revenue based on two factor.
  1. Impression (Number of view by the user)
  2. Geographic Location.
As we mentioned above, your game will get 2$ - 20$ for 1000 Impression based on where the ads has been shown so far. So the revenue generated from 1k Impression is pretty untraceable on how much your are getting from 2-20$ as your game are played through different geological location. If the game it self specified base on region for example at Europe, you are pretty much getting 20$ for 1k Impression. So yes, you are a rich guy !.

Setting Your Project:

After creating your project in "Unity", there are two ways you can enable "Unity Ads" service for your current project.
  1. Enable "Unity Ads" service from "Service" window from the editor.
  2. Downloading "Unity Ads" package from the asset store.
For the sake of avoiding unwanted bugs (Which I faced couple of time from the different project when trying to enable it from the editor), you should always download the "Unity Ads Package" from the "Asset Store" as the bugs/feature fixed/added faster in the asset store rather than adding from the "Service" window as enabling the "Unity Ads" comes with the latest unity update, where the asset store is always getting updated whenever they are changing it. Plus, I find it a bug free way to add this service to our projects.

What's next?

I have created a public repository at "bitbucket" including documentation & ".unityPackage" which can be ported to any of your project. All you have to do is to use the prefab and callback for your game. Tada !!!. Life is much more easier with scalable module.
https://bitbucket.org/thefaithstudio/services-unity-ads/src/master/
if you just want to see how the module has been implemented rather than totally using the module, just check out the following script to get an idea how to start working with the "Unity Ads".

UnityAdsReward.cs

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using UnityEngine.Advertisements; public class UnityAdsReward : MonoBehaviour { #region PUBLIC VARIABLES public static UnityAdsReward instance; [Header("RewardVideoAds Property")] public bool enableRewardVideoAds; public bool rewardedVideoAdsSkipCallback; public bool rewardedVideoAdsFailedCallBack; public UnityEvent OnRewardVideoAdsFinished; public UnityEvent OnRewardVideoAdsSkipped; public UnityEvent OnRewardVideoAdsFailed; [Header("VideoAds Property")] public bool enableVideoAds; public bool videoAdsSkipCallback; public bool videoAdsFailedCallback; public UnityEvent OnVideoAdsFinished; public UnityEvent OnVideoAdsSkipped; public UnityEvent OnVideoAdsFailed; //-------------------------------- public bool unityTestMode; public bool testModeGUI; public string gameID; public string gameID_Android; public string gameID_iOS; #endregion #region PRIVATE VARIABLES private bool mCustomCall; private UnityAction OnAdsFinished; private UnityAction OnAdsSkipped; private UnityAction OnAdsFailed; #endregion void Awake(){ mPreProcess (); } #region Pre-Process/Process/Post-Process void OnGUI(){ if (testModeGUI) { float width = 0; float height = 0; if (Screen.width > Screen.height) { //Landscape width = Screen.width/3; height = Screen.height / 16; } else { //Portrait width = Screen.width / 2; height = Screen.height / 12; } if (Advertisement.isSupported) { GUI.TextField (new Rect (0, 0, width, height), "Unity Ads Supported"); } if (GUI.Button (new Rect (0,height, width, height), "Show Ads")) { ShowAds (); } if (Advertisement.IsReady ("rewardedVideo")) { GUI.TextField (new Rect (width, 0, width, height), "rewardVideo (Ready)"); } else { GUI.TextField (new Rect (width, 0, width, height), "rewardVideo\n(NOT! Ready)"); } if (Advertisement.IsReady ("video")) { GUI.TextField (new Rect (width, height, width, height), "video (Ready)"); } else { GUI.TextField (new Rect (width, height, width, height), "video (NOT! Ready)"); } if (GUI.Button (new Rect (0,height*2, width, height), "Show Ads\n(RwVAds)")) { ShowRewardVideoAds (); } if (GUI.Button (new Rect (width,height*2, width, height), "Show Ads\n(VAds)")) { ShowVideoAds (); } } } private void mPreProcess(){ mCustomCall = false;; mResetCustomAdsCallBack (); if (instance == null) { instance = this; DontDestroyOnLoad (instance.gameObject); } else if(instance != this){ Destroy (this.gameObject); } #if UNITY_ANDROID gameID = gameID_Android; #elif UNITY_IOS gameID = gameID_iOS; #endif StartCoroutine (InitializedAds ()); } private IEnumerator InitializedAds(){ if (Advertisement.isSupported) { Advertisement.Initialize (gameID, unityTestMode); } while (!Advertisement.isInitialized || !Advertisement.IsReady ()) { yield return new WaitForSeconds (0.5f); } } private void mResetCustomAdsCallBack(){ OnAdsFinished = null; OnAdsSkipped = null; OnAdsFailed = null; } #endregion //----------------------------------------------------------------------------- #region RewardedVide Ads public bool IsRewardedVideoAdsReady(){ if (Advertisement.IsReady ("rewardedVideo")) return true; return false; } public void ShowRewardVideoAds(){ if (Advertisement.IsReady ("rewardedVideo")) { Advertisement.Show ("rewardedVideo", new ShowOptions () { resultCallback = HandleRewardAdResult }); } } public void ShowRewardedVideoAdsWithEvent(UnityAction OnAdsFinished){ mResetCustomAdsCallBack (); this.OnAdsFinished = OnAdsFinished; mCustomCall = true; ShowRewardVideoAds (); } public void ShowRewardedVideoAdsWithEvent(UnityAction OnAdsFinished,UnityAction OnAdsFailed){ mResetCustomAdsCallBack (); this.OnAdsFinished = OnAdsFinished; this.OnAdsFailed = OnAdsFailed; mCustomCall = true; ShowRewardVideoAds (); } public void ShowRewardedVideoAdsWithEvent(UnityAction OnAdsFinished,UnityAction OnAdsSkipped,UnityAction OnAdsFailed){ mResetCustomAdsCallBack (); this.OnAdsFinished = OnAdsFinished; this.OnAdsSkipped = OnAdsSkipped; this.OnAdsFailed = OnAdsFailed; mCustomCall = true; ShowRewardVideoAds (); } private void HandleRewardAdResult(ShowResult result){ switch (result) { case ShowResult.Failed: if (mCustomCall) { OnAdsFailed.Invoke (); mCustomCall = false; } else { OnRewardVideoAdsFailed.Invoke (); } //Debug.Log ("Failed To Load The RewardAds"); break; case ShowResult.Skipped: if (mCustomCall) { OnAdsSkipped.Invoke (); mCustomCall = false; } else { OnRewardVideoAdsSkipped.Invoke (); } //Debug.Log ("Skip RewardAds"); break; case ShowResult.Finished: if (mCustomCall) { OnAdsFinished.Invoke (); mCustomCall = false; } else { OnRewardVideoAdsFinished.Invoke (); } //Debug.Log ("Finished RewardAds"); break; } } #endregion //----------------------------------------------------------------------------- #region VideoAds public bool IsVideoAdsReady(){ if (Advertisement.IsReady ("rewardedVideo")) return true; return false; } public void ShowVideoAds(){ if(Advertisement.IsReady("video")){ Advertisement.Show ("video", new ShowOptions () { resultCallback = HandleVideoAdResult }); } } public void ShowVideoAdsWithEvent(UnityAction OnAdsFinished){ mResetCustomAdsCallBack (); this.OnAdsFinished = OnAdsFinished; mCustomCall = true; ShowVideoAds (); } public void ShowVideoAdsWithEvent(UnityAction OnAdsFinished,UnityAction OnAdsFailed){ mResetCustomAdsCallBack (); this.OnAdsFinished = OnAdsFinished; this.OnAdsFailed = OnAdsFailed; mCustomCall = true; ShowVideoAds (); } public void ShowVideoAdsWithEvent(UnityAction OnAdsFinished,UnityAction OnAdsSkipped,UnityAction OnAdsFailed){ mResetCustomAdsCallBack (); this.OnAdsFinished = OnAdsFinished; this.OnAdsSkipped = OnAdsSkipped; this.OnAdsFailed = OnAdsFailed; mCustomCall = true; ShowVideoAds (); } private void HandleVideoAdResult(ShowResult result){ switch (result) { case ShowResult.Failed: if (mCustomCall) { OnAdsFailed.Invoke (); } else { OnVideoAdsFailed.Invoke (); mCustomCall = false; } //Debug.Log ("Failed To Load The VideoAds"); break; case ShowResult.Skipped: if (mCustomCall) { OnAdsSkipped.Invoke (); } else { OnVideoAdsSkipped.Invoke (); mCustomCall = false; } //Debug.Log ("Skip VideoAds"); break; case ShowResult.Finished: if (mCustomCall) { OnAdsFinished.Invoke (); } else { OnVideoAdsFinished.Invoke (); mCustomCall = false; } //Debug.Log ("Finished VideoAds"); break; } } #endregion //----------------------------------------------------------------------------- #region ShowAds (With Priority of Rewarded Video Ads -> Video Ads) public bool IsAdsReady(){ if (Advertisement.IsReady ()) return true; return false; } public void ShowAds(){ if (Advertisement.IsReady ("rewardedVideo")) { Advertisement.Show ("rewardedVideo", new ShowOptions () { resultCallback = HandleRewardAdResult }); } else if (Advertisement.IsReady ("video")) { Advertisement.Show ("video", new ShowOptions () { resultCallback = HandleVideoAdResult }); } } public void ShowAdsWithEvent(UnityAction OnAdsFinished){ mResetCustomAdsCallBack (); this.OnAdsFinished = OnAdsFinished; ShowAds (); } public void ShowAdsWithEvent(UnityAction OnAdsFinished,UnityAction OnAdsFailed){ mResetCustomAdsCallBack (); this.OnAdsFinished = OnAdsFinished; this.OnAdsFailed = OnAdsFailed; ShowAds (); } public void ShowAdsWithEvent(UnityAction OnAdsFinished,UnityAction OnAdsSkipped,UnityAction OnAdsFailed){ mResetCustomAdsCallBack (); this.OnAdsFinished = OnAdsFinished; this.OnAdsSkipped = OnAdsSkipped; this.OnAdsFailed = OnAdsFailed; ShowAds (); } #endregion }

Editor/UnityAdsRewardEditor.cs

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; [CustomEditor(typeof(UnityAdsReward))] public class UnityAdsRewardEditor : Editor { private UnityAdsReward mUARReference; private string Is_Android_Platform_Enabled = "Is_Android_Platform_Enabled"; private string Is_iOS_Platform_Enabled = "Is_iOS_Platform_Enabled"; private string gameID_AndroidLocal; private void mPreProcess(){ mUARReference = (UnityAdsReward)target; } private void OnEnabled(){ mPreProcess (); } public override void OnInspectorGUI(){ mPreProcess (); serializedObject.Update (); TestModeGUI (); PlatformSelectionGUI (); AdsTypeSelectionGUI (); serializedObject.ApplyModifiedProperties (); } private void TestModeGUI(){ #region Test Mode EditorGUILayout.Space (); EditorGUILayout.BeginHorizontal(); EditorGUILayout.HelpBox( "Allow you to test the unity ads without seeing actual ads." + "But you need to configure this on unity dashboard and puting " + "'gameID' under platform selection even if you want to test it", MessageType.Warning); EditorGUILayout.PropertyField( serializedObject.FindProperty("unityTestMode")); EditorGUILayout.EndHorizontal(); GUILayout.Box("",GUILayout.ExpandWidth(true), GUILayout.Height(2.5f)); EditorGUILayout.Space (); EditorGUILayout.BeginHorizontal(); EditorGUILayout.HelpBox( "Allow you to test unity ads services with GUI buttons to test on" + "physical device with callback", MessageType.Info); EditorGUILayout.PropertyField( serializedObject.FindProperty("testModeGUI")); EditorGUILayout.EndHorizontal(); #endregion } private void PlatformSelectionGUI(){ #region Platform Selection GUILayout.Box("",GUILayout.ExpandWidth(true), GUILayout.Height(2.5f)); EditorGUILayout.Space (); EditorGUILayout.LabelField ("Select Platforms"); EditorGUI.indentLevel++; EditorGUILayout.BeginVertical(); // Android EditorGUILayout.BeginHorizontal(); if(PlayerPrefs.GetInt(Is_Android_Platform_Enabled) == 1){ if(GUILayout.Button("Disable : Android")){ PlayerPrefs.SetInt(Is_Android_Platform_Enabled,0); } EditorGUILayout.PropertyField( serializedObject.FindProperty("gameID_Android")); }else{ if(GUILayout.Button("Enable Android")){ PlayerPrefs.SetInt(Is_Android_Platform_Enabled,1); } serializedObject.FindProperty("gameID_Android").stringValue = ""; } EditorGUILayout.EndHorizontal(); //------------- // iOS EditorGUILayout.BeginHorizontal(); if(PlayerPrefs.GetInt(Is_iOS_Platform_Enabled) == 1){ if(GUILayout.Button("Disable : iOS")){ PlayerPrefs.SetInt(Is_iOS_Platform_Enabled,0); } EditorGUILayout.PropertyField( serializedObject.FindProperty("gameID_iOS")); }else{ if(GUILayout.Button("Enable iOS")){ PlayerPrefs.SetInt(Is_iOS_Platform_Enabled,1); } serializedObject.FindProperty("gameID_iOS").stringValue = ""; } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); EditorGUI.indentLevel--; #endregion } private void AdsTypeSelectionGUI(){ #region Ads Type Selection EditorGUILayout.Space (); EditorGUILayout.Space (); GUILayout.Box("",GUILayout.ExpandWidth(true), GUILayout.Height(2.5f)); OnRewardedVideoAdsGUI(); OnVideoAdsGUI(); #endregion } private void OnRewardedVideoAdsGUI(){ EditorGUILayout.BeginVertical(); EditorGUILayout.BeginHorizontal(); mUARReference.enableRewardVideoAds = EditorGUILayout.Foldout( mUARReference.enableRewardVideoAds, mUARReference.enableRewardVideoAds ? "RewardVideoAds(Enabled)" : "RewardVideoAds(Disabled)"); if (mUARReference.enableRewardVideoAds) { EditorGUI.indentLevel += 2; EditorGUILayout.PropertyField ( serializedObject.FindProperty ( "OnRewardVideoAdsFinished"), true ); EditorGUILayout.EndHorizontal (); EditorGUILayout.Space (); EditorGUILayout.Space (); EditorGUILayout.BeginHorizontal (); mUARReference.rewardedVideoAdsSkipCallback = EditorGUILayout.Toggle ( "Skip Callback", mUARReference.rewardedVideoAdsSkipCallback); if (mUARReference.rewardedVideoAdsSkipCallback) { EditorGUILayout.PropertyField ( serializedObject.FindProperty ("OnRewardVideoAdsSkipped") ); } EditorGUILayout.EndHorizontal (); EditorGUILayout.Space (); EditorGUILayout.Space (); EditorGUILayout.BeginHorizontal (); mUARReference.rewardedVideoAdsFailedCallBack = EditorGUILayout.Toggle ( "Failed Callback", mUARReference.rewardedVideoAdsFailedCallBack); if (mUARReference.rewardedVideoAdsFailedCallBack) { EditorGUILayout.PropertyField ( serializedObject.FindProperty ("OnRewardVideoAdsFailed") ); } EditorGUILayout.EndHorizontal (); EditorGUI.indentLevel -= 2; } EditorGUILayout.EndVertical(); } private void OnVideoAdsGUI(){ EditorGUILayout.Space (); EditorGUILayout.Space (); EditorGUILayout.BeginVertical(); EditorGUILayout.BeginHorizontal(); mUARReference.enableVideoAds = EditorGUILayout.Foldout( mUARReference.enableVideoAds, mUARReference.enableVideoAds ? "VideoAds(Enable)" : "VideoAds(Disabled)"); if (mUARReference.enableVideoAds) { EditorGUI.indentLevel += 2; EditorGUILayout.PropertyField ( serializedObject.FindProperty ("OnVideoAdsFinished") ); EditorGUILayout.EndHorizontal (); EditorGUILayout.Space (); EditorGUILayout.Space (); EditorGUILayout.BeginHorizontal (); mUARReference.videoAdsSkipCallback = EditorGUILayout.Toggle ( "Skip Callback", mUARReference.videoAdsSkipCallback); if (mUARReference.videoAdsSkipCallback) { EditorGUILayout.PropertyField ( serializedObject.FindProperty ("OnVideoAdsSkipped") ); } EditorGUILayout.EndHorizontal (); EditorGUILayout.Space (); EditorGUILayout.Space (); EditorGUILayout.BeginHorizontal (); mUARReference.videoAdsFailedCallback = EditorGUILayout.Toggle ( "Failed Callback", mUARReference.videoAdsFailedCallback); if (mUARReference.videoAdsFailedCallback) { EditorGUILayout.PropertyField ( serializedObject.FindProperty ("OnVideoAdsFailed") ); } EditorGUILayout.EndHorizontal (); EditorGUI.indentLevel -= 2; } EditorGUILayout.EndVertical(); } }

Thank You !!

For having amazing patience to read the article. I do believe it will help you on some extend on the journey of the game development. Please don't forget to give your valuable feedback on how I can improve the module. Cheers !!

Muhammad Tashfiqul Bari
FAITH - Student
11
Comments