Implementing Ad Integration in Unity WebGL Builds

Implementing Ad Integration in Unity WebGL Builds

Introduction

In modern game development, integrating advertisements is a common strategy for monetization, especially for web-based games. This script in Unity facilitates the display and management of ads across different platforms. It supports several ad networks, including GameDistribution, GameMonetize, CrazyGames, Y8, and GameArter. This article explains the script’s functionality, providing insights into how it manages ads and interacts with various ad providers.

Script Overview

The script, gamemonetizeshowadscript, extends Unity’s MonoBehaviour and includes methods to handle ad-related events and state management. It allows toggling fullscreen mode, pausing and resuming game logic, and showing different types of ads based on the specified WebGL build target.

Key Components

  1. Build Target Selection: The script includes an enum-like integer to specify the ad provider for the WebGL build. The options are:
  • 1: Game Distribution
  • 2: GameMonetize
  • 3: CrazyGames
  • 4: Y8
  • 5: GameArter
  1. Fullscreen Management: The toggleFullScreen method toggles the fullscreen mode. It is triggered by pressing the ‘F’ key.
  2. Game Pause Logic: The script includes logic to pause and resume the game, which is necessary when displaying ads. It adjusts the game’s audio and fullscreen state accordingly.

Detailed Functionality

Awake Method

The Awake method initializes the gamePauseLogic and sets up event listeners for GameDistribution if the build target is set to 1. These listeners handle various ad-related events such as pausing and resuming the game, and handling rewarded video ads.

void Awake() {
    gamePauseLogic = 0;
    if (webGLBuildTarget == 1) {
        GameDistribution.OnResumeGame += ResumeGame;
        GameDistribution.OnPauseGame += PauseGame;
        GameDistribution.OnPreloadRewardedVideo += OnPreloadRewardedVideo;
        GameDistribution.OnRewardedVideoSuccess += OnRewardedVideoSuccess;
        GameDistribution.OnRewardedVideoFailure += OnRewardedVideoFailure;
    }
}

Ad Display Methods

The script contains methods to display standard and rewarded ads. Depending on the webGLBuildTarget, it calls the appropriate methods from the respective ad provider’s SDK.

public void ShowAd() {
    if (webGLBuildTarget == 1) {
        GameDistribution.Instance.ShowAd();
    }
    // Other ad providers can be added similarly
}

public void ShowRewardedAd() {
    if (webGLBuildTarget == 1) {
        GameDistribution.Instance.ShowRewardedAd();
        PreloadRewardedAd();
    }
}

Event Handlers

The script defines handlers for ad-related events. These ensure the game resumes properly after an ad is displayed and handle the success or failure of rewarded video ads.

public void ResumeGame() {
    AudioListener.volume = 1f;
    if (gamePauseLogic == 1) {
        setGameReumeLogic();
        gamePauseLogic = 0;
    }
    Screen.fullScreen = fullscreenState_BeforeAdCall;
}

public void PauseGame() {
    AudioListener.volume = 0f;
    fullscreenState_BeforeAdCall = Screen.fullScreen;
    Screen.fullScreen = false;
}

Utility Methods

Additional methods manage game state and handle application focus and pause events to ensure the game behaves correctly during ad display.

void OnApplicationFocus(bool hasFocus) {
    Silence(!hasFocus);
}

void OnApplicationPause(bool isPaused) {
    Silence(isPaused);
}

private void Silence(bool silence) {
    AudioListener.pause = silence;
    AudioListener.volume = silence ? 0 : 1;
}

Conclusion

This Unity script provides a comprehensive solution for integrating ads into WebGL builds, supporting multiple ad networks. It manages game state transitions smoothly, ensuring a seamless user experience during ad displays. By following the structure and logic outlined in this script, developers can efficiently implement ad monetization in their Unity games.

 

Leave a Reply

Your email address will not be published. Required fields are marked *