Posted on

Cloud Saving Service – Cross Platform Native Plugins: Essential Kit

Download

Documentation: https://assetstore.essentialkit.voxelbusters.com/

0:00 Welcome to Cloud Saving with CPNP2
0:30 Overview
1:27 Setup
2:30 iOS
2:57 Android
3:30 Usage Code
12:40 Unity Demo
15:10 Testing

In this lesson on how to use the Cross Platform Native Plugin in Unity, I will show you how to implement the Cloud Service to save player data. Having the ability to save player data to the cloud is important because it makes it so the player game quickly change devices and keep their data. The Cloud Service even allows you to save the player’s purchases whenever then buy an IAP from your store.

IG_CloudSaving.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VoxelBusters.EssentialKit;
using VoxelBusters.CoreLibrary;


public class IG_CloudSaving : MonoBehaviour
{
    public static IG_CloudSaving instance;

    public IG_CloudData myCloudData;
    public string cloudDataKey;




    private void OnEnable()
    {
        // register for events
        CloudServices.OnUserChange += OnUserChange;
        CloudServices.OnSavedDataChange += OnSavedDataChange;
        CloudServices.OnSynchronizeComplete += OnSynchronizeComplete;
    }

    private void OnDisable()
    {
        // unregister from events
        CloudServices.OnUserChange -= OnUserChange;
        CloudServices.OnSavedDataChange -= OnSavedDataChange;
        CloudServices.OnSynchronizeComplete -= OnSynchronizeComplete;
    }

    // Start is called before the first frame update
    void Start()
    {
        instance = this;
        DontDestroyOnLoad(gameObject);
        if(CloudServices.IsAvailable())
        {
            CloudServices.Synchronize(); // First call to syncronize triggers google play services login prompt if required - on Android.
        }
        
    }

    private void OnSynchronizeComplete(CloudServicesSynchronizeResult result)
    {
        Debug.Log("Received synchronize finish callback.");
        Debug.Log("Status: " + result.Success);
        // By this time, you have the latest data from cloud and you can start reading.
        myCloudData = GetCloudData(cloudDataKey);
    }


    public void SaveCloudData(string key, IG_CloudData data)
    {
        string json = JsonUtility.ToJson(data);                                                                  // Please mention the limitations of JsonUtility and they have option to use their own serializer here for complex data where JsonUtility has limitations.

        CloudServices.SetString(key, json);
    }

    public IG_CloudData GetCloudData(string key)
    {
        string json = CloudServices.GetString(key);

        return JsonUtility.FromJson<IG_CloudData>(json);
    }


    private void OnUserChange(CloudServicesUserChangeResult result, Error error)
    {
        Debug.Log(result.User);
    }

    //This gets triggered if the data changed externally on another device while playing on this device.
    private void OnSavedDataChange(CloudServicesSavedDataChangeResult result)
    {

        for (int i = 0; i < result.ChangedKeys.Length; i++)
        {
            if (result.ChangedKeys[i] == cloudDataKey)
            {
                // Here GetCloudData returns the data that is recently available on server. So, we always need to maintain a runtime memory copy incase if current device has new data.
                // Here you are already maintaining the copy in myCloudData. So let me show you how to handle this conflict...

                IG_CloudData serverCopy = GetCloudData(cloudDataKey);

                if(serverCopy.highScore > myCloudData.highScore) //This means if server has better highscore, we want to retain it.
                {
                    myCloudData.highScore = serverCopy.highScore;
                }

                if(serverCopy.hasRemoveAds || myCloudData.hasRemoveAds) // This means we are making sure if remove ads value is true any where on external device or this device
                {
                    myCloudData.hasRemoveAds = true;
                }

                //Now as we made changes, we push the conflict resolved data to the cloud again by setting the latest data.
                SaveCloudData(cloudDataKey, myCloudData);

                // Lines 127-140 can be skipped if you are fine overwriting your local device data with the external changed data. But its good to consider whats the best value to safeguard the users progress.

                // On next sync, the recent data set above will be automatically pushed to cloud.
            }
        }

    }
}

public class IG_CloudData
{
    public int highScore;
    public bool hasRemoveAds;
    //Add whatever variable you wish to save to the cloud
}
Posted on

Unity IAP and Billing – Cross Platform Native Plugins: Essential Kit

Download

Documentation: https://assetstore.essentialkit.voxelbusters.com/

0:00 Welcome to Billing and IAP
0:21 Overview
1:16 Setup
3:35 iOS
5:03 Android
6:27 Usage and Code
16:30 Setup in Unity

In this tutorial lesson on how to use the Cross Platform Native Plugin 2 in Unity, I will show you how to use the Billing service to create in-app purchases. Adding a store and in-app purchase to your project is one of the best ways to monetize your games. This allows the users to purchase actual content in your game versus monetizing your game with ads only.

IG_Billing.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using VoxelBusters.EssentialKit;
using VoxelBusters.CoreLibrary;

public class IG_Billing : MonoBehaviour
{
    [SerializeField] Button OpenStoreButton; //Holds the first button used to enable to Store UI
    [SerializeField] GameObject storePanel; // Holds the Store UI so we can toggle it on and off

    [SerializeField] Button[] purchaseButtons; //An Array of all the puchase buttons in our store so we can enable each one if purduct is available

    // Start is called before the first frame update
    void Start()
    {
        //Check to see if Billing Services in enable in Essental Kit settings if true enable OpenStoreButton and initialize store
        if (BillingServices.IsAvailable())
        {
            OpenStoreButton.interactable = true;
            BillingServices.InitializeStore();
        }
    }

    private void OnEnable()
    {
        // register for events
        BillingServices.OnInitializeStoreComplete += OnInitializeStoreComplete;
        BillingServices.OnTransactionStateChange += OnTransactionStateChange;
        BillingServices.OnRestorePurchasesComplete += OnRestorePurchasesComplete;
    }

    private void OnDisable()
    {
        // unregister from events
        BillingServices.OnInitializeStoreComplete -= OnInitializeStoreComplete;
        BillingServices.OnTransactionStateChange -= OnTransactionStateChange;
        BillingServices.OnRestorePurchasesComplete -= OnRestorePurchasesComplete;
    }

    // Register for the BillingServices.OnInitializeStoreComplete event

    private void OnInitializeStoreComplete(BillingServicesInitializeStoreResult result, Error error)
    {
        if (error == null)
        {
            // update UI
            // show console messages
            var products = result.Products;
            Debug.Log("Store initialized successfully.");
            Debug.Log("Total products fetched: " + products.Length);
            Debug.Log("Below are the available products:");
            for (int iter = 0; iter < products.Length; iter++)
            {
                //Enable each purchase button in the store for each product that is available and not already purchased
                purchaseButtons[iter].interactable = !BillingServices.IsProductPurchased(products[iter]);
                var product = products[iter];
                Debug.Log(string.Format("[{0}]: {1}", iter, product));
            }
        }
        else
        {
            Debug.Log("Store initialization failed with error. Error: " + error);
        }

        var invalidIds = result.InvalidProductIds;
        Debug.Log("Total invalid products: " + invalidIds.Length);
        if (invalidIds.Length > 0)
        {
            Debug.Log("Here are the invalid product ids:");
            for (int iter = 0; iter < invalidIds.Length; iter++)
            {
                Debug.Log(string.Format("[{0}]: {1}", iter, invalidIds[iter]));
            }
        }
    }

    //This function is set to the on click of each purchase button with the static paramenter set to the products index in Essental Kits settings
    public void Purchase(int productIndex)
    {
        if (BillingServices.CanMakePayments())
        {
            BillingServices.BuyProduct(BillingServices.Products[productIndex]);
        }
    }

    private void OnTransactionStateChange(BillingServicesTransactionStateChangeResult result)
    {
        var transactions = result.Transactions;
        for (int iter = 0; iter < transactions.Length; iter++)
        {
            var transaction = transactions[iter];
            switch (transaction.TransactionState)
            {
                case BillingTransactionState.Purchased:
                    //Check for Receipt. if valid send product to RewardPurchase function
                    if (transaction.ReceiptVerificationState == BillingReceiptVerificationState.Success)
                    {
                        RewardPurchase(transaction.Payment.ProductId);
                    }
                    Debug.Log(string.Format("Buy product with id:{0} finished successfully.", transaction.Payment.ProductId));
                    break;

                case BillingTransactionState.Failed:
                    Debug.Log(string.Format("Buy product with id:{0} failed with error. Error: {1}", transaction.Payment.ProductId, transaction.Error));
                    break;
            }
        }
    }

    //This function is set to the on click of the Restore Purchase button.
    public void Restore()
    {
        BillingServices.RestorePurchases();
    }

    private void OnRestorePurchasesComplete(BillingServicesRestorePurchasesResult result, Error error)
    {
        if (error == null)
        {
            var transactions = result.Transactions;
            Debug.Log("Request to restore purchases finished successfully.");
            Debug.Log("Total restored products: " + transactions.Length);
            for (int iter = 0; iter < transactions.Length; iter++)
            {

                var transaction = transactions[iter];
                //Check for Receipt. If valid send product again to the RewardPurchase function
                if (transaction.ReceiptVerificationState == BillingReceiptVerificationState.Success)
                {
                    RewardPurchase(transaction.Payment.ProductId);
                }
                Debug.Log(string.Format("[{0}]: {1}", iter, transaction.Payment.ProductId));
            }
        }
        else
        {
            Debug.Log("Request to restore purchases failed with error. Error: " + error);
        }
    }
    
    //This function is set to the on click of open and close buttons for toggling the Store UI on and off

    public void ToggleStore()
    {
        storePanel.SetActive(!storePanel.activeInHierarchy);
    }

    //This function is used to reward to player with everything they have purcahse.
    void RewardPurchase(string productID)
    {
        //Add switch cases for each product you have in your  products array
        switch(productID)
        {
            case "remove_ads":
                Debug.Log("Reward product and save to cloud");
                IG_CloudSaving.instance.myCloudData.hasRemoveAds = true;
                IG_CloudSaving.instance.SaveCloudData(IG_CloudSaving.instance.cloudDataKey, IG_CloudSaving.instance.myCloudData);
                purchaseButtons[0].interactable = false;
                break;
        }
    }
    
    void PlayAd()
    {
        if (IG_CloudSaving.instance.myCloudData.hasRemoveAds)
            return;
        //Display an ad here!
    }
    
}
Posted on

Cross Platform Native Plugins: Essential Kit – Setup in Unity

Download

Documentation: https://assetstore.essentialkit.voxelbusters.com/

0:00 Welcome to the Setup for CPNP2
0:39 Purchase and Plugin Setup
2:30 Documentation
4:24 Introduction and Application Setup

In this tutorial series, we will show you how to use all the features of the Cross Platform Native Plugins 2 in Unity. This Unity Package Developed by Voxel Busters can help you implement many standard features that can be found in almost every popular mobile game out there.