Kemoth Ads
Utility library to show ads in game Install https://github.com/kemoth/EventSystem_UPM.git and https://github.com/kemoth/Utilities_UPM.git
com.kemothstudios.kemothads Install via UPM
Add to Unity Package Manager using this URL
https://www.pkglnk.dev/kemothads.git README Markdown
Copy this to your project's README.md
## Installation
Add **Kemoth Ads** to your Unity project via Package Manager:
1. Open **Window > Package Manager**
2. Click **+** > **Add package from git URL**
3. Enter:
```
https://www.pkglnk.dev/kemothads.git
```
[](https://www.pkglnk.dev/pkg/kemothads)README
Usage
Install dependencies
- Install Event System package by adding
https://github.com/kemoth/EventSystem_UPMin package manager - Install Utilities package by adding
https://github.com/kemoth/Utilities_UPMin package manager - Install Mobile Ads SDK
Setup
- Create an empty gameobject in scene and add KemothAdsManager to it
- Drag-Drop MessageBoxCanvas prfab from Packages > KemothAds folder inside this empty gameobject we created above
- Create ads configuration by right-clicking project window and select
Create > Kemoth Studios > KemothAds > Configuration- Enable Show Test Ads while testing game or ads
- Enabel the the type of ads you want to show
- If and ad is enabled then assign an AdUnitID for it, if test ads are enabled then this is not necessary
- Drag-Drop this configuration file in the Kemoth Ads Configuration field of the KemothAdsManager
NOTE:
KemothAdsManager is not a singleton object
Using package
This package can show Interstitial, Banner and Reward ads and we are using Event Bus pattern to show ads and receive callbacks from them. Below are the examples for all the ads:
- Interstitial Ads
- To show an ad call
EventBus<ShowInterstitialAdEvent>.RaiseEvent(new ShowInterstitialAdEvent()); - In any case like either user closed the ad or ad could not be shown, we will receive same event, to use that event follow the below code:
// Create a binding to the event private EventBinding<InterstitialAdCompletedEvent> _interstitialAdCompleted; private void Start() { // Register binding to the event bus _interstitialAdCompleted = new EventBinding<InterstitialAdCompletedEvent>(OnInterstialAdCompleted); EventBus<InterstitialAdCompletedEvent>.RegisterBinding(_interstitialAdCompleted); } private void OnDestroy() { // Unregister binding from event bus EventBus<InterstitialAdCompletedEvent>.UnregisterBinding(_interstitialAdCompleted); } private void OnInterstialAdCompleted() { // code to run after ad is closed ... }
- To show an ad call
- Banner Ads
- To show an ad call
EventBus<ShowBannerAdEvent>.RaiseEvent(new ShowBannerAdEvent); - To hide an ad call
EventBus<HideBannerAdEvent>.RaiseEvent(new HideBannerAdEvent);
- To show an ad call
- Rewarded Ads
- To Show an ad call
EventBus<ShowRewardedAdEvent>.RaiseEvent(new ShowRewardedAdEvent()); - If user watched the ad and we need to award them then code below is an example for that:
// Create a binding to the event private EventBinding<RewardedAdCompletedEvent> _rewardedAdCompleted; private void Start() { // Register binding to the event bus _rewardedAdCompleted = new EventBinding<RewardedAdCompletedEvent>(OnRewardUser); EventBus<RewardedAdCompletedEvent>.RegisterBinding(_rewardedAdCompleted); } private void OnDestroy() { // Unregister binding from event bus EventBus<RewardedAdCompletedEvent>.UnregisterBinding(_rewardedAdCompleted); } // This method can used without the argument also private void OnRewardUser(RewardedAdCompletedEvent rewardData) { // rewardData holds a RewardAmount integer which holds the amount we need to reward user with // For eg. we can use it to reward coins to user. // reward user here... } - If user cannot be rewarded in any case like they closed the rewarded ad or the ad not loaded itself then code below is an example for that:
// Create a binding to the event private EventBinding<RewardedAdFailedEvent> _rewardFailedEvent; private void Start() { // Register binding to the event bus _rewardFailedEvent = new EventBinding<RewardedAdFailedEvent>(OnRewardFailed); EventBus<RewardedAdCompletedEvent>.RegisterBinding(_rewardFailedEvent); } private void OnDestroy() { // Unregister binding from event bus EventBus<RewardedAdFailedEvent>.UnregisterBinding(_rewardFailedEvent); } private void OnRewardFailed() { // handle failed reward here ... }
- To Show an ad call
Conditional Ads
We can define conditions for the ads, for example, we only want to show ads after 3rd show request.
NOTE:
Currently only Interstitial Ads supports this.
Below is a simple example to demonstrate conditional ads usage.
Define condition data
- Conditions require some data to validate and here's how we define it.
public struct TestingAdConditionData : IKemothAdsConditionData { public bool show3Ads; }
- Conditions require some data to validate and here's how we define it.
Define condition
- Now that we have data, let's make a condition that will use it.
public class TestingAdCondition : MonoBehaviour, IKemothAdsCondition<TestingAdConditionData> { private int _adsCountMax3; public bool ValidateCondition(TestingAdConditionData conditionData) { if (conditionData.show3Ads) { if (_adsCountMax3++ < 3) return true; _adsCountMax3 = 0; } return false; } public bool ValidateCondition(IKemothAdsConditionData conditionData) { if(conditionData is TestingAdConditionData testingAdConditionData) return ValidateCondition(testingAdConditionData); return false; } }
- Now that we have data, let's make a condition that will use it.
Assign Condition
- Conditions should be an Unity object bacause it needs to be referenced in KemothAdsManager, if you failed to provide a condition, KemothAdsManager will use a DefaultKemothAdsCondition
- Conditions should be an Unity object bacause it needs to be referenced in KemothAdsManager, if you failed to provide a condition, KemothAdsManager will use a DefaultKemothAdsCondition
Show Ad
- Now that we have our condition ready, let's show ad which will use this condition
public void ShowCountedInterstitial() { TestingAdConditionData data = new TestingAdConditionData(); data.show3Ads = true; EventBus<ShowInterstitialAdEvent>.RaiseEvent(new ShowInterstitialAdEvent { ConditionData = data }); }
- Now that we have our condition ready, let's show ad which will use this condition
Comments
No comments yet. Be the first!
Sign in to join the conversation
Sign In