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");
        });
    }

}