Posted on

Network – Cross Platform Native Plugins: Essential Kit

Download

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

0:00 Welcome to Network and Web View CPNP2
0:16 Overview
0:36 Setup
1:08 Usage

For this lesson on how to use the Cross Platform Native Plugins in Unity, I will show you how to use the Network and Web View services. The Networking service makes it so you can check to see if the user’s device has a connection to the internet. You are then able to block the user from accessing parts of your game if they don’t have a network connection.

IG_NetworkServices.cs

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

public class IG_NetworkServices : MonoBehaviour
{
    public static IG_NetworkServices instance;

    public bool isInternetActive;
    public bool isHostReachable;
    public bool isNotifierActive;

    private void OnEnable()
    {
        // register for events
        NetworkServices.OnHostReachabilityChange += OnHostReachabilityChange;
        NetworkServices.OnInternetConnectivityChange += OnInternetConnectivityChange;
        NetworkServices.StopNotifier();
    }

    private void OnDisable()
    {
        // unregister from events
        NetworkServices.OnHostReachabilityChange -= OnHostReachabilityChange;
        NetworkServices.OnInternetConnectivityChange -= OnInternetConnectivityChange;
    }

    // Start is called before the first frame update
    void Start()
    {
        instance = this;
    }

    private void OnInternetConnectivityChange(NetworkServicesInternetConnectivityStatusChangeResult result)
    {
        Debug.Log("Received internet connectivity changed event.");
        Debug.Log("Internet connectivity status: " + result.IsConnected);
        isInternetActive = NetworkServices.IsInternetActive;
    }

    private void OnHostReachabilityChange(NetworkServicesHostReachabilityStatusChangeResult result)
    {
        Debug.Log("Received host reachability changed event.");
        Debug.Log("Host reachability status: " + result.IsReachable);
        isHostReachable = NetworkServices.IsHostReachable;
    }

    //IG_NetworkServices.instance.CheckNetwork();
    public void CheckNetwork()
    {
        NetworkServices.StartNotifier();
    }

    //IG_NetworkServices.instance.StopStatus();
    public void StopStatus()
    {
        NetworkServices.StopNotifier();
    }
}