Publishers · Reference

React, Vue & Unity

Copy-paste hooks and components for React, Vue 3 and Unity WebGL.

Keep the <script> tag in your index.html, then use one of these helpers.

React

import { useCallback, useEffect, useRef } from "react";

export function useAdsun({ blockId, onReward, onError }: {
  blockId: string; onReward?: () => void; onError?: (r: unknown) => void;
}) {
  const ctrl = useRef<any>();
  useEffect(() => { ctrl.current = window.Adsun?.init({ blockId }); }, [blockId]);

  return useCallback(async () => {
    if (!ctrl.current) return onError?.({ done: false, description: "Adsun script not loaded", state: "load", error: true });
    try { await ctrl.current.show(); onReward?.(); }
    catch (r) { onError?.(r); }
  }, [onReward, onError]);
}

Vue 3

import { onMounted, shallowRef } from "vue";

export function useAdsun(blockId: string) {
  const ctrl = shallowRef<any>(null);
  onMounted(() => { ctrl.value = window.Adsun?.init({ blockId }); });
  const show = () => ctrl.value?.show();
  return { show };
}

Unity WebGL

Add Adsun.jslib to Assets/Plugins/WebGL and the script tag to your WebGL template.

mergeInto(LibraryManager.library, {
  AdsunShow: function (blockIdPtr) {
    var blockId = UTF8ToString(blockIdPtr);
    window.Adsun.init({ blockId: blockId }).show()
      .then(function () { SendMessage("Ads", "OnReward"); })
      .catch(function () { SendMessage("Ads", "OnAdFailed"); });
  }
});

INFO

The GameObject must be named exactly Ads so SendMessage can find it.

Still stuck? Write to support@adsun.io or our Telegram support — we answer fast.