Posted on

Sharing – Cross Platform Native Plugins: Essential Kit

Download

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

0:00 Welcome to Share CPNP2
0:20 Overview
1:29 Setup
1:45 Usage and Coding

In this lesson on how to use the Cross Platform Native Plugins 2, I will show you how to use the Sharing and Rate My App services. Adding Sharing to your app is vital to the growth of your audience. User to user sharing is the best way to get the word out and get your game noticed.

IG_Sharing.cs

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

public class IG_Sharing : MonoBehaviour
{
    [SerializeField] string textMessage;
    [SerializeField] string imageName;


    public void ShareText()
    {
        ShareSheet shareSheet = ShareSheet.CreateInstance();
        shareSheet.AddText(textMessage);
        shareSheet.SetCompletionCallback((result, error) =>
        {
            Debug.Log("Share Sheet was closed. Result code: " + result.ResultCode);
        });
        shareSheet.Show();
    }

    public void ShareTextWithScreenshot()
    {
        ShareSheet shareSheet = ShareSheet.CreateInstance();
        shareSheet.AddText(textMessage);
        shareSheet.AddScreenshot();
        shareSheet.SetCompletionCallback((result, error) =>
        {
            Debug.Log("Share Sheet was closed. Result code: " + result.ResultCode);
        });
        shareSheet.Show();
    }


    public void ShareImage()
    {
        Texture2D texture = Resources.Load<Texture2D>(imageName);

        ShareSheet shareSheet = ShareSheet.CreateInstance();
        shareSheet.AddImage(texture);
        shareSheet.SetCompletionCallback((result, error) =>
        {
            Debug.Log("Share Sheet was closed. Result code: " + result.ResultCode);
        });
        shareSheet.Show();
    }
}
Posted on

Game Services – 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

For this lesson on how to use the Cross Platform Native Plugins 2 in Unity, I will show you how to set up Game Services which include Achievements and Leaderboards. Having Achievement and Leaderboards in your game are helpful tools which will increase the retention of player for your game. Achievements are good as they give the play something to work for and get rewarded. Leaderboards are good as they will introduce competition into your game. Both rewards and competition will drive returning users to your game.

IG_GameServices.cs

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

public class IG_GameServices : MonoBehaviour
{
    public static IG_GameServices instance;

    public static bool IsAthenticated;

    static ILocalPlayer localplayer;

    private void OnEnable()
    {
        // register for events
        GameServices.OnAuthStatusChange += OnAuthStatusChange;
    }

    private void OnDisable()
    {

        // unregister from events
        GameServices.OnAuthStatusChange -= OnAuthStatusChange;
    }
    // Start is called before the first frame update
    void Start()
    {
        
        
        instance = this;
        if (IsAthenticated)
            return;
        if(GameServices.IsAvailable())
        {
            
            GameServices.Authenticate();
        }
    }

    private void OnAuthStatusChange(GameServicesAuthStatusChangeResult result, Error error)
    {
        
        if (error == null)
        {
            Debug.Log("Received auth status change event");
            Debug.Log("Auth status: " + result.AuthStatus);
            Debug.Log("Local player: " + result.LocalPlayer);
            if (result.AuthStatus == LocalPlayerAuthStatus.Authenticated)
            {
                IsAthenticated = true;
                localplayer = result.LocalPlayer;
            }
        }
        else
        {
            Debug.LogError("Failed login with error : " + error);
        }
        
    }

    //Achievements
    //IG_GameServices.instance.ReportAchivement("AchivementID", 100);
    
    //IG_GameServices.instance.ReportAchivement("id", 100);
    public void ReportAchivement(string achievementId, double percentageCompleted)
    {
        GameServices.ReportAchievementProgress(achievementId, percentageCompleted, (error) =>
        {
            if (error == null)
            {
              Debug.Log("Request to submit progress finished successfully.");
            }
                else
            {
            Debug.Log("Request to submit progress failed with error. Error: " + error);
            }
        });
    }

    public void DisplayDefaultAchivements()
    {
        GameServices.ShowAchievements((result, error) =>
        {   
            Debug.Log("Achievements view closed");
        });
        
    }

    //Leaderboards
    //IG_GameServices.instance.ReportHighScore("LeaderboardID", 100);
    
   //IG_GameServices.instance.ReportLeaderboard("id", 100);
    public void ReportLeaderboard(string leaderboardId, long score)
    {
        GameServices.ReportScore(leaderboardId, score, (error) =>
        {
            if (error == null)
            {
                Debug.Log("Request to submit score finished successfully.");
            }   
            else
            {
                Debug.Log("Request to submit score failed with error: " + error.Description);
            }
        });
    }

    public void DisplayDefaultLeaderboard()
    {
        GameServices.ShowLeaderboards(callback: (result, error) =>
        {
            Debug.Log("Leaderboards UI closed");
        });
    }

}
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
}