🎮 Unity Integration

Unity WebView Integration Guide

This guide provides comprehensive step-by-step instructions to integrate a WebView component into any Unity game project. The WebView allows you to display web content directly within your Unity application.

Prerequisites

  • Unity Editor (2019.4 or later recommended)
  • WebView custom package (.unitypackage file)
  • WebView script folder (provided separately)
  • Game icon image for Website button (PNG/Sprite)
  • Basic knowledge of Unity Editor interface

1Open Your Unity Project

  1. Launch Unity Hub from your desktop.
  2. From the Projects list, select your target Unity game project.
  3. Click Open to load the project in Unity Editor.
  4. Wait for the project to fully load and compile.

2Load the Main Scene

  1. In the Project window (bottom panel), navigate to your game's Scenes folder.
  2. Locate your main scene file (commonly named MainScene, GameScene, or Level1).
  3. Double-click the scene to open it in the Hierarchy.
  4. Verify that the scene contains a Canvas GameObject (required for UI elements).
💡

Note:If your scene doesn't have a Canvas, create one by right-clicking in the Hierarchy → UI → Canvas.

3Create the Website Button

  1. In the Hierarchy window, locate and select your main Canvas GameObject.
  2. Right-click on the Canvas → Create Empty.
  3. Rename the newly created GameObject to [WebsiteButton].
  4. With [WebsiteButton] selected in the Inspector:
    • Click Add Component in the Inspector.
    • Search for and add Image component.
    • In the Image component, assign your game icon to the Source Image field.
    • Click Add Component again.
    • Search for and add Button component.

Tip:You can adjust the button's position and size using the Rect Transform component in the Inspector.

4Import WebView Custom Package

  1. In the Unity menu bar, go to Assets → Import Package → Custom Package.
  2. Navigate to the location of your WebView custom package (.unitypackage file).
  3. Select the package file and click Open.
  4. In the Import Unity Package dialog, ensure all items are checked.
  5. Click Import button.
  6. Wait for Unity to import and compile all package files.
⚠️

Important: Do not modify the package folder structure after import to maintain proper dependencies.

5Setup WebView Script and Game Controller

Import WebView Script Folder

  • Locate the WebView script folder (provided separately).
  • Drag and drop the entire WebView script folder into the Assets folder.
  • Wait for Unity to import and compile all scripts.

Create GameControllerWindow GameObject

  • In the Hierarchy window, right-click on the Canvas.
  • Select Create Empty.
  • Rename this new GameObject to [GameControllerWindow].

Attach Script to GameControllerWindow

  • Select [GameControllerWindow] in the Hierarchy.
  • From the Project window, locate the WebView script file.
  • Drag and drop the script onto the Inspector panel of [GameControllerWindow].

Configure Banner Button Reference

  • Look at the WebView script component in the Inspector.
  • Locate the Banner Button field.
  • Drag and drop the [WebsiteButton] into this field.

Implementation Script

You can use the following C# snippet as a reference for your GameController orWebViewManager script. This handles the click event and launches the Unique Link.

SnappGamesHandler.cscsharp
using UnityEngine;
using UnityEngine.UI;

public class SnappGamesHandler : MonoBehaviour
{
    public Button snappgamesButton;
    private string gameUrl = "https://www.snappgames.com/?id=44";

    void Start()
    {
        if (snappgamesButton != null)
        {
            snappgamesButton.onClick.AddListener(OpenWebView);
        }
    }

    void OpenWebView()
    {
        // Trigger your WebView component here
        // Example: WebView.Open(gameUrl);
        Debug.Log("Launching SnappGames: " + gameUrl);
        Application.OpenURL(gameUrl); // Fallback to browser if needed
    }
}
🛑

Important: The [WebsiteButton] must be properly assigned to the Banner Button field for the WebView to function correctly.