capacitor-plugin-unityads
Unity ADS Integration for Capacitor & Ionic Projects.
⚠ NOTE
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.
Install
npm install @openanime/capacitor-plugin-unityads # (or yarn, pnpm)
npx cap sync
Table of Contents
Basic Example with Svelte
<script>
import { onMount } from "svelte";
import { UnityAds } from "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>
API
initAds(...)
initAds(options: { unityGameId: string; testMode: boolean; }) => void
loadAds(...)
loadAds(options: { adUnitId: string; }) => void
displayAd()
displayAd() => void
Events
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. |