# Unity

After an instant or a tournament game is published, it can be integrated into a mobile application or game built with Unity.

### Overview

The WindayWrapper class provides functionality to display and interact with web content within a **Unity application using a WebView**.&#x20;

It allows for loading web pages, handling events, and communicating with web content through JavaScript.

### Setup

1. Download the [<mark style="color:blue;">**Unity Plugin**</mark>](https://drive.google.com/file/d/1a1e56cIPCsw9m6pG55TSM-Cku5be1N0f/view?usp=sharing).
2. Install the plugin and toggle the dropdown menu Internet Access: Required in Project settings.

### Constructor

#### WindayWrapper()

Description: Initializes the WebView and sets up event handlers.

Example:

{% code overflow="wrap" %}

```csharp
WindayWrapper webView = new WindayWrapper();
```

{% endcode %}

### Methods

#### 1. Subscribe(string eventName, Action callback)

Description: Subscribes to an event that does not pass any data. The callback will be executed when the event is triggered.

Parameters:

* eventName: The name of the event to listen to.
* callback: The action to execute when the event is triggered.

Example:

{% code overflow="wrap" %}

```csharp
webView.Subscribe("exit-mobile-plugin", () => { Debug.Log("Exiting the mobile plugin."); });
```

{% endcode %}

#### 2. Subscribe\<T>(string eventName, Action\<T> callback) where T : class

Description: Subscribes to an event that passes data. The callback will receive data associated with the event.

Parameters:

* eventName: The name of the event.
* callback: The action to execute when the event is triggered, receiving data of type T.

Example:

{% code overflow="wrap" %}

```csharp
webView.Subscribe<GameColors>("winday-game-colors", (colors) => { Debug.Log($"Colors received: Yellow - {colors.yellow}, Blue - {colors.blue}, Red - {colors.red}, Green - {colors.green}, Purple - {colors.purple}"); });
```

{% endcode %}

#### 3. Unsubscribe(string eventName)

Description: Unsubscribes from a previously subscribed event, removing all callbacks associated with the event.

Parameters:

* eventName: The name of the event to unsubscribe from.

Example:

{% code overflow="wrap" %}

```csharp
webView.Unsubscribe("exit-mobile-plugin");
```

{% endcode %}

#### 4. SetVisible(bool visible)

Description: Sets the visibility of the WebView. When visible, the WebView is rendered and interactive.

Parameters:

* visible: true to make the WebView visible, false to hide it.

Example:

```csharp
webView.SetVisible(true);  // Shows the WebView
```

#### 5. LoadPage(string url)

Description: Loads a web page in the WebView using the specified URL.

Parameters:

* url: The URL of the web page to load.

Example:

{% code overflow="wrap" %}

```csharp
webView.LoadPage("https://winday.club/games/VMPWRL3Y?isMobile=true");
```

{% endcode %}

#### 6. SetMargins(int left, int top, int right, int bottom, bool relative = false)

Description: Sets the margins of the WebView, defining how it is positioned on the screen.

Parameters:

* left: Left margin in pixels.
* top: Top margin in pixels.
* right: Right margin in pixels.
* bottom: Bottom margin in pixels.
* relative: If true, the margins are defined as a percentage of the screen size; otherwise, they are in pixels.

Example:

{% code overflow="wrap" %}

```csharp
webView.SetMargins(10, 10, 10, 10);  // Set 10px margins on all sides
```

{% endcode %}

#### 7. OnPageStartLoading

Description: This event is triggered when a new web page begins loading in the WebView. It provides the URL of the page that is starting to load.

Example:

{% code overflow="wrap" lineNumbers="true" %}

```csharp
// Instantiate the WindayWrapper
WindayWrapper webViewWrapper = new WindayWrapper();

// Subscribe to the OnPageStartLoading event
webViewWrapper.OnPageStartLoading += (url) =>
{
    Debug.Log($"Page is starting to load: {url}");
};

// Load a new page
webViewWrapper.LoadPage("https://example.com");
```

{% endcode %}

#### 8. OnPageLoaded

Description: This event is triggered when the web page has finished loading successfully. It also provides the URL of the loaded page.

Example:

{% code overflow="wrap" lineNumbers="true" %}

```csharp
// Subscribe to the OnPageLoaded event
webViewWrapper.OnPageLoaded += (url) =>
{
    Debug.Log($"Page has finished loading: {url}");
    webViewWrapper.Show();
};

// Load a new page
webViewWrapper.LoadPage("https://example.com");
```

{% endcode %}

#### 9. OnClosed

Description: This event is triggered when the WebView is destroyed or closed. It signals that the WebView is no longer active and has been removed from the scene.

Example:

{% code overflow="wrap" lineNumbers="true" %}

```csharp
// Subscribe to the OnClosed event
webViewWrapper.OnСlosed += () =>
{
    Debug.Log("WebView has been closed.");
};

// Destroy the WebView
webViewWrapper.Destroy();
```

{% endcode %}

### Event Examples

#### exit-mobile-plugin

Description: Triggered when the user wants to exit the mobile plugin.

Example:

{% code overflow="wrap" %}

```csharp
webView.Subscribe("exit-mobile-plugin", () => { Debug.Log("Exiting mobile plugin."); });
```

{% endcode %}

#### winday-game-win

Description: Triggered when the user wins the game.

Example:

{% code overflow="wrap" %}

```csharp
webView.Subscribe("winday-game-win", () => { Debug.Log("Player has won the game!"); });
```

{% endcode %}

#### winday-game-lose

Description: Triggered when the user loses the game.

Example:

{% code overflow="wrap" %}

```csharp
webView.Subscribe("winday-game-lose", () => { Debug.Log("Player has lost the game."); });
```

{% endcode %}

#### winday-game-tutorial-start

Description: Triggered when the tutorial starts.

Example:

{% code overflow="wrap" %}

```csharp
webView.Subscribe("winday-game-tutorial-start", () => { Debug.Log("Tutorial has started."); });
```

{% endcode %}

#### winday-game-tutorial-complete

Description: Triggered when the tutorial is completed.

Example:

{% code overflow="wrap" %}

```csharp
webView.Subscribe("winday-game-tutorial-complete", () => { Debug.Log("Tutorial is complete."); });
```

{% endcode %}

#### winday-game-colors

Description: Triggered when game color data is sent.

Example:

{% code overflow="wrap" %}

```csharp
webView.Subscribe<GameColors>("winday-game-colors", (colors) => { Debug.Log($"Colors: Yellow - {colors.yellow}, Blue - {colors.blue}, Red - {colors.red}, Green - {colors.green}, Purple - {colors.purple}"); });
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.winday.co/publish-campaigns/publish-in-mobile-app/unity.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
