C# ADS
Add mid-roll and rewarded ads to a Unity WebGL game on VIVERSE with the VIVERSE Ad SDK Unity package. Covers setup, cooldowns, pausing, and rewards.
Before you start
The VIVERSE Ad SDK runs only on the VIVERSE platform. In the Unity Editor, the package simulates every ad so you can test your game logic, but real ads appear only in a WebGL build published to VIVERSE.
You need:
A Unity project with the WebGL build target.
A VIVERSE App ID for your game.
The VIVERSE Ad SDK Unity package,
VADK_v0.0.6.unitypackage.
Ad formats on this page
The SDK uses its own names for each format. Mid-roll ads are the Overlay and Video formats, and rewarded ads are the Reward Overlay and Reward Video formats.
Overlay
Mid-roll, full-screen interstitial
Your game, between levels
ShowOverlayAsync()
Video
Mid-roll, video interstitial
Your game, between levels
ShowVideoAsync()
Reward Overlay
Rewarded, full-screen, 15s video, image, or text
The player, from a button
ShowRewardOverlayAsync()
Reward Video
Rewarded, 60s video
The player, from a button
ShowRewardVideoAsync()
The package also includes ViverseAdsBannerClient for banner ads, which this page doesn't cover.
Set up the package
Import the package, add the two client components to your first scene, and initialize the SDK once at startup.
Import the package
In Unity, open Assets > Import Package > Custom Package and select VADK_v0.0.6.unitypackage. The scripts use the ViverseAdSDK namespace.
The package includes a sample scene, ViverseAdFullscreenButtonTestScene, with a button for each ad format. Its test UI uses the Input System package. If your project doesn't use the Input System, clear the Assets/Scenes folder and ViverseAdFullscreenTestUIBuilder.cs in the import dialog.
Add the client components
In your first scene, create two empty GameObjects named ViverseAdsClient and ViverseAdsFullscreenClient. Add the component with the same name to each one.
Both components persist across scene loads. Give each GameObject a name that is unique in your scenes, because the WebGL bridge sends callbacks to it by name.
Pause and resume your game during an ad
Every format on this page covers the screen, so your game must stop while the ad plays. ViverseAdsFullscreenClient raises events that tell you when to pause and when to resume.
OnBreakStart
Pause gameplay, animations, input, background music, and sound effects.
OnBreakEnd
Resume everything you paused.
OnError
Resume everything you paused, and let the game continue.
Resume in both OnBreakEnd and OnError. When no ad is available, OnError fires instead of OnBreakStart. The task returned by each Show...Async method completes when the ad is ready or starts, not when it ends, so use the events to resume your game.
The AdBreakManager component below handles pausing, cooldowns, and rewards for the rest of this page. It pauses with Time.timeScale and AudioListener.pause; replace those with your game's own pause logic if you have it.
Show mid-roll ads between levels
Show Overlay or Video ads at natural breaks, such as after a level ends and before the next one loads. Don't show one when the game first starts, before the player has played anything.
Overlay and Video share one cooldown. Wait at least 30 seconds after either one before you call either one again. Add this method to AdBreakManager. It uses Time.realtimeSinceStartup so the cooldown keeps counting while Time.timeScale is 0.
Call it from your level flow, for example adBreakManager.ShowMidrollAd(); when a level is complete. OnBreakEnd passes a skipped value for Video if you want to log whether the player skipped.
Show rewarded ads
A rewarded ad gives the player something in exchange for watching, such as an extra life or in-game currency. The player must start it. Call it only from a button or similar control the player chooses, such as Watch an ad to continue, and never autoplay it.
Grant the reward only in OnRewardGranted. Don't grant it in OnBreakEnd. Add these methods to AdBreakManager.
Connect it to a UI button. For example, on a Game Over screen:
Decide what the reward is in your own game code. Don't rely on the amount and type arguments of OnRewardGranted to set its value.
Test your integration
Test in two places. The Editor confirms your game logic, and a published WebGL build confirms real ads.
In the Unity Editor and other non-WebGL targets, InitAsync always succeeds and each Show...Async call simulates a complete ad: ready, break start, a reward for rewarded formats, then break end. No ad appears on screen. Use Play mode to check that your game pauses, resumes, and grants rewards in the right places.
To see real ads, build for WebGL and publish your game to VIVERSE. Then check each item below.
Mid-roll ads appear between levels and never at game start.
A second mid-roll ad within 30 seconds of the first one is skipped.
Gameplay, input, music, and sound effects stop during every ad and resume afterward.
The game keeps running when an ad fails. To test this, turn on an ad blocker.
Rewarded ads start only from a player action, and each ad grants its reward once.
API reference
These are the members this page uses. All of them are in the ViverseAdSDK namespace. Only one ad can show at a time.
ViverseAdsClient
Instance
The singleton component in your scene.
InitAsync(string appId)
Initializes the SDK. Returns Task<bool>: true on success.
IsInitialized
true after InitAsync succeeds.
OnInitialized
Raised when initialization succeeds.
OnInitializeFailed
Raised with an error message when initialization fails.
ViverseAdsFullscreenClient
Instance
The singleton component in your scene.
ShowOverlayAsync()
Requests an Overlay ad. Returns Task<FullscreenAdHandle>.
ShowVideoAsync()
Requests a Video ad. Returns Task<FullscreenAdHandle>.
ShowRewardOverlayAsync()
Requests a Reward Overlay ad. Returns Task<FullscreenAdHandle>.
ShowRewardVideoAsync()
Requests a Reward Video ad. Returns Task<FullscreenAdHandle>.
OnReady(FullscreenAdHandle)
Raised when the ad has loaded.
OnBreakStart(FullscreenAdHandle)
Raised when the ad starts. Pause your game.
OnBreakEnd(FullscreenAdHandle, bool skipped)
Raised when the ad ends. Resume your game.
OnRewardGranted(FullscreenAdHandle, float amount, string type)
Raised when the player earns the reward. Grant it here.
OnError(FullscreenAdHandle, string message)
Raised when the ad fails or isn't available. Resume your game.
Each Show...Async task completes when the ad is ready or starts, and faults with an exception when the request fails. FullscreenAdHandle has a RequestId and a Format (Overlay, Video, RewardOverlay, or RewardVideo). Events fire for every request, so check handle.Format or handle.RequestId when you need to tell ads apart.
Last updated
Was this helpful?