Unity ADS Integration for Capacitor & Ionic Projects.
This plugin is in beta. Currently it only supports interstitial ads but rewarded ads and banner ads are planned.
Also, IOS support is still under development.
npm install @openanime/capacitor-plugin-unityads
npx cap sync
<script>
import { onMount } from "svelte";
import { UnityAds } from "@openanime/unity-ads-capacitor";
let ready = false;
let message = "";
let isSkipped;
const unityGameId = "YOUR_GAME_ID";
const testMode = true; //change this to false in production!
const adUnitId = "YOUR_AD_UNIT_AD";
onMount(() => {
UnityAds.addListener("initialized", () => {
message = "initialized";
UnityAds.loadAds({
adUnitId,
});
});
UnityAds.addListener("initializationError", ({ error }) => {
message = "initializationError: " + error;
});
UnityAds.addListener("adShown", ({ state }) => {
message = "ad completed state: " + state; // SKIPPED or COMPLETED
UnityAds.loadAds({
adUnitId,
});
});
UnityAds.addListener("adShowError", ({ error }) => {
message = "adShowError: " + error;
});
UnityAds.addListener("adLoaded", () => {
message = "ad loaded";
ready = true;
});
UnityAds.addListener("adLoadError", ({ error }) => {
message = "adLoadError: " + error;
});
UnityAds.initAds({
unityGameId,
testMode,
});
});
const handleClick = () => {
UnityAds.loadAds({
adUnitId,
});
UnityAds.displayAd();
};
</script>
<div>
{#if ready}
<button on:click="{handleClick}">Show ad</button>
{:else}
<p>not ready!</p>
{/if}
<p>{message}</p>
</div>
initAds(options: { unityGameId: string; testMode: boolean; }) => void
loadAds(options: { adUnitId: string; }) => void
displayAd() => void
Event | Returns | Description |
---|---|---|
initialized |
null |
Fires when Unity SDK is initialized. |
adLoaded |
null |
Fires when AD is loaded. |
adShowStart |
null |
Fires when AD starts showing. |
adShowClick |
null |
Fires when user clicks the AD. |
adShown |
{ state: "SKIPPED" | "COMPLETED"; } |
Fires when AD is completed or skipped. |
initializationError |
{ error: string; } |
Fires when error occurs in initialization. |
adLoadError |
{ error: string; } |
Fires when error occurs while loading an AD. |
adShowError |
{ error: string; } |
Fires when error occurs while showing an AD. |