OpenAnime / capacitor-plugin-unityads

Unity ADS Integration for Capacitor & Ionic Projects.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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
npx cap sync

Table of Contents

Basic Example with Svelte

<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>

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.

About

Unity ADS Integration for Capacitor & Ionic Projects.


Languages

Language:Java 48.5%Language:Swift 15.4%Language:TypeScript 13.7%Language:Ruby 10.2%Language:Objective-C 7.1%Language:JavaScript 5.0%