# Unity Integration

## 🧩Prerequisites

* Unity version 2022.3.38f1 or later
* Unity Android Build Support installed
* Basic familiarity with Unity C# scripts
* Your application key from Playtime SDK
* Targeting Android platform only

### Navigate to: `Assets → Plugins → Android → mainTemplate.gradle`

Add the following line inside the dependencies block:

```csharp
implementation 'com.playtimeads:offerwall:2.0.6'
```

***

### Create a C# Wrapper Class With a Listener Proxy Class

This wrapper bridges Unity and the native Android library.

```csharp
using System;
using UnityEngine;

public class PlaytimeAds
{
    private AndroidJavaObject objPlaytimeAds;
    private AndroidJavaObject unityActivity;
    public static Action OnInitSuccess;
    public static Action OnInitFailed;
    public PlaytimeAds()
    {
        using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
        {
            unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        }
        using (AndroidJavaClass javaClass = new AndroidJavaClass("com.playtimeads.PlaytimeAds"))
        {
            objPlaytimeAds = javaClass.CallStatic<AndroidJavaObject>("getInstance");
        }
    }
    public void DestroyPlaytimeAds()
    {
         if (objPlaytimeAds != null)
         {
             // Call the method in the Java class without expecting a return value
             Debug.LogError("destroyPlaytimeAds method is getting called");
             objPlaytimeAds.Call("destroy");
         }
         else
         {
             Debug.LogError("Failed to create Android class instance destroyPlaytimeAds.");
         }
    }
    public void Init(string applicationId, string userId)
    {
        if (objPlaytimeAds != null)
        {
            var listenerProxy = new Proxy.Listener();
            Debug.Log("init method is getting called" + userId);
            objPlaytimeAds.Call("init",unityActivity, applicationId, userId,listenerProxy);
        }
        else
        {
            Debug.LogError("Failed to create Android class instance. isInitPlaytimeAds1");
        }
    }

    public void OpenOfferWall()
    {
        if (objPlaytimeAds != null)
        {
            // Call the method in the Java class without expecting a return value
            Debug.LogError("open Offerwall method is getting called");
            // objPlaytimeAds.Call("isInitialized");
            objPlaytimeAds.Call("open",unityActivity);
        }
        else
        {
            Debug.LogError("Failed to create Android class instance. isInitPlaytimeAds1");
        }
    }
}
namespace Proxy
{
    public class Listener : AndroidJavaProxy
    {
        public Listener() : base("com.playtimeads.listeners.OfferWallInitListener") { }

        // Implement the onSuccess method
        public void onInitSuccess()
        {
            PlaytimeAds.OnInitSuccess?.Invoke();
            Debug.Log("onInitSuccess is called");
        }

        public void onAlreadyInitializing()
        {
            Debug.Log("onAlreadyInitializing is called");
        }

        // Implement the onFailure method
        public void onInitFailed(string error)
        {
            PlaytimeAds.OnInitFailed?.Invoke();
            Debug.LogError("onInitFailed is called: " + error);
        }
    }
}

```

***

### Create a Manager Class

This manager centralizes all interactions with the wrapper class.

```csharp

public class PlaytimeAdsManager : Manager<PlaytimeAdsManager>
{
    private PlaytimeAds _playtimeAds;
    public PlaytimeAds PlaytimeAds => _playtimeAds ??= new PlaytimeAds();

    public void InitializeWithUserId(string userId)
    {
#if !UNITY_ANDROID || UNITY_EDITOR
        Debug.Log("PlaytimeAds Initialization Skipped in Editor");
        return;
#endif
        Debug.Log("Initializing PlaytimeAds...");
        PlaytimeAds.OnInitSuccess += OnInitSuccess;
        PlaytimeAds.OnInitFailed += OnInitFailed;
        PlaytimeAds.Init(userId);
    }

    private void OnInitSuccess()
    {
        Debug.Log("PlaytimeAds Initialized Successfully");
    }

    private void OnInitFailed()
    {
        Debug.Log("PlaytimeAds Initialization Failed");
    }

    public void DestroyPlaytimeAds()
    {
#if !UNITY_ANDROID || UNITY_EDITOR
        Debug.Log("Destroy Skipped in Editor");
        return;
#endif
        PlaytimeAds.DestroyPlaytimeAds();
    }

    public void OpenOfferWall()
    {
#if !UNITY_ANDROID || UNITY_EDITOR
        Debug.Log("Open Offerwall Skipped in Editor");
        return;
#endif
        PlaytimeAds.OpenOfferWall();
    }
}
```


---

# 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://docs.playtimeads.com/sdk-integration/unity-integration.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.
