Unity 2D Game Development, Add Advertising and In-App Purchases Add advertising and in-app purchase features using Unity Ads and IAP.

Recently, 2D games have become very popular, and many developers are using Unity to develop their games. Unity provides a powerful engine and various tools to help developers implement their creative ideas into actual games. However, just developing a game is not enough to generate revenue. Therefore, it is important to add advertising and in-app purchase features. This article will detail how to add advertising and in-app purchase features using Unity Ads and Unity IAP (In-App Purchase).

1. Overview of Unity Ads

Unity Ads is an advertising platform provided by the Unity engine, allowing developers to easily integrate ads into their games. Through Unity Ads, developers can display ads to generate revenue without interrupting the user’s gaming experience.

2. How to Set Up Unity Ads

To set up Unity Ads, the following steps are required.

2.1 Setting Up a Unity Project

To use the Ads feature in the Unity project, you first need to enable the Ads service in your Unity project. Select Window > General > Services from the top menu of the Editor. In the Unity Services window, select “Ads” and enable it. You will need to log in to your Unity developer account to do this.

2.2 Setting Up Advertising

Once the service is activated, an Ads ID will be created for the project. This information can be used to integrate Unity Ads.

2.3 Importing the Unity Ads SDK

Open the Unity Package Manager and find the Ads package in the Unity Registry to install it. This will add the packages related to Unity Ads to the project.

3. Unity Ads Code Sample

The basic code to integrate Unity Ads into a game is as follows.

using UnityEngine;
using UnityEngine.Advertisements;

public class AdsManager : MonoBehaviour, IUnityAdsListener
{
    private string gameId = "YOUR_GAME_ID";
    private bool testMode = true;
    
    void Start()
    {
        if (Advertisement.isSupported)
        {
            Advertisement.Initialize(gameId, testMode);
            Advertisement.AddListener(this);
        }
    }

    public void ShowAd()
    {
        if (Advertisement.IsReady("video"))
        {
            Advertisement.Show("video");
        }
    }

    public void OnUnityAdsReady(string placementId) { }

    public void OnUnityAdsDidFinish(string placementId, ShowResult showResult) 
    {
        if (showResult == ShowResult.Finished)
        {
            // Action when ad is fully viewed
            Debug.Log("Ad viewing completed");
        }
        else if (showResult == ShowResult.Skipped)
        {
            // In case the ad was skipped
            Debug.Log("Ad skipped");
        }
        else if (showResult == ShowResult.Failed)
        {
            // Ad load failed
            Debug.Log("Ad load failed");
        }
    }

    public void OnUnityAdsDidError(string message) 
    {
        Debug.Log("Ad error occurred: " + message);
    }
}
        

4. Overview of In-App Purchases (IAP)

In-app purchases allow users to buy items or features within the game. Using IAP in Unity makes it easy to implement such features.

5. How to Set Up Unity IAP

The steps for installing and setting up Unity IAP are as follows.

5.1 Importing the Unity IAP Package

In the Unity editor, select Window > Package Manager and find the Unity IAP package to install it.

5.2 Activating Services

To use IAP, you must activate the IAP service in Unity Services. Select “IAP” in the Services window and enable it.

5.3 Registering Products

You need to register products (in-app purchase items) in the Unity dashboard. You must set a unique ID and specify a price for each product. This information can be used in your code.

6. Unity IAP Code Sample

The basic code for integrating Unity IAP and handling in-app purchases is as follows.

using UnityEngine;
using UnityEngine.Purchasing;

public class IAPManager : MonoBehaviour, IStoreListener
{
    private static IStoreController m_StoreController;          
    private static IExtensionProvider m_ExtensionProvider;
    
    public string productId = "YOUR_PRODUCT_ID";

    void Start()
    {
        if (m_StoreController == null)
        {
            InitializePurchasing();
        }
    }

    public void InitializePurchasing()
    {
        if (IsInitialized())
            return;

        var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
        builder.AddProduct(productId, ProductType.Consumable);
        UnityPurchasing.Initialize(this, builder);
    }

    private bool IsInitialized()
    {
        return m_StoreController != null && m_ExtensionProvider != null;
    }

    public void BuyProductID()
    {
        BuyProductID(productId);
    }

    void BuyProductID(string productId)
    {
        BuyProduct(productId);
    }

    public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    {
        m_StoreController = controller;
        m_ExtensionProvider = extensions;
        Debug.Log("Purchase initialization completed");
    }

    public void OnInitializeFailed(InitializationFailureReason error)
    {
        Debug.Log("Purchase initialization failed: " + error.ToString());
    }

    public void OnPurchaseFailed(Product product, PurchaseFailureReason reason)
    {
        Debug.Log($"Purchase failed: {product.definition.id}, Reason: {reason}");
    }

    public void OnPurchased(Product product)
    {
        Debug.Log($"Purchase completed: {product.definition.id}");
    }

    public void OnPurchasingFailed(Product product, PurchaseFailureReason reason)
    {
        Debug.Log($"Error occurred during purchase: {product.definition.id}, Reason: {reason}");
    }
}
        

7. Final Check for Integrating Ads and In-App Purchases into the Game

Now that you have integrated Unity Ads and IAP, you can display ads within the game and perform in-app purchases. It is important to provide appropriate feedback when users view ads or make purchases.

8. Conclusion

Through this article, we have learned how to add advertising and in-app purchase features to Unity 2D games. Proper use of Unity Ads and IAP can enhance the profitability of the game. However, excessive advertising can harm user experience, so it is important to maintain a proper balance. Create great 2D games with Unity!