[Unity] I tried to make a native plug-in UniNWPathMonitor using NWPathMonitor

Unity has a network connection check function ʻApplication.internetReachability`, but I dared to make a native Swift plugin using ** NWPathMonitor **.

Use NWPath Monitor, which is simpler than Reachability, to detect network connection status on iOS. --Qiita For the processing around Swift, I referred to the article here.

Swift side

UniNWPathMonitor.swift


monitor.pathUpdateHandler = { path in
    if path.status == .satisfied {
        // connected
        self._onCallback("1")
    } else {
        // No connected
        self._onCallback("0")
    }
}

If you are connected using the NWPathMonitor instance (monitor), " 1 " will be returned, and if you are disconnected, " 0 " will be returned with a preset callback.

Objective-C ++ side

API_AVAILABLE(ios(12.0))
UniNWPathMonitor *_monitor;

Since iOS12 or later is required to use NWPathMonitor this time, declare that iOS12 or later is required with ʻAPI_AVAILABLE`.

UniNWPathMonitorBridge.mm


[_monitor setCallbackOnCallback:^(NSString * value) {
    UnitySendMessage("UniNWPathMonitor", "Noti", [value UTF8String]);
}];

Set a callback for UniNWPathMonitor. When the callback is returned, use ʻUnitySendMessage` to return the connection information to the Unity side.

For the description of Objective-C ++, please refer to the article here that I wrote earlier. [Unity] Shortest implementation to run Swift from C # in recent Unity-Qiita

Unity side

This is how it is used on the Unity side. Just run StartMonitor on UniNWPathMonitor and set the callback.

Sample.cs


void Start()
{
    UniNWPathMonitor.StartMonitor();
    UniNWPathMonitor.onChangeNetworkStatus += status =>
    {
        if (status == UniNWPathMonitor.NetworkStatus.satisfied)
            //connecting
        else
            //Cutting
    };
}

anim.gif Click here for what I actually made. I turn Wifi on and off and receive callbacks on the Unity side. There seems to be a slight lag until the callback is returned to Unity after switching.

Finally

As I said at the beginning, this plugin is not required for Buchake Unity because ** Application.internetReachability ** exists. * I made it because I wanted to make it.

For me, I learned a lot by actually making it. I'm glad I had the opportunity to learn about Swift, which I'm currently studying, such as the Swift callback grammar that came out for the first time and how to make iOS 12 or later mandatory.


All sources are uploaded below, so please refer to them. https://github.com/baobao/UniNWPathMonitor

Development environment

Recommended Posts

[Unity] I tried to make a native plug-in UniNWPathMonitor using NWPathMonitor
I tried using Hotwire to make Rails 6.1 scaffold a SPA
How to make Unity Native Plugin (Android version)
I tried to implement a server using Netty
I tried to make a talk application in Java using AI "A3RT"
I tried to make a login function in Java
I tried to make a client of RESAS-API in Java
How to make a Jenkins plugin
I tried to build a simple application using Dockder + Rails Scaffold
I tried to make it an arbitrary URL using routing nesting
[Java] I tried to make a maze by the digging method ♪
I tried to make a group function (bulletin board) with Rails
[Rails] Implementation of multi-layer category function using ancestry "I tried to make a window with Bootstrap 3"
I tried to make a parent class of a value object in Ruby
[iOS] I tried to make a processing application like Instagram with Swift
I tried to make a Web API that connects to DB with Quarkus
I tried to make my own transfer guide using OpenTripPlanner and GTFS
Java beginner tried to make a simple web application using Spring Boot
I made a virtual currency arbitrage bot and tried to make money
I tried to make a sample program using the problem of database specialist in Domain Driven Design
I tried to make Basic authentication with Java
java I tried to break a simple block
I tried to develop a man-hour management tool
I did Java to make (a == 1 && a == 2 && a == 3) always true
I tried to develop a DUO3.0 study website.
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I tried to create a LINE clone app
Try to make a music player using Basic Player
I tried to develop a website to record expenses.
I tried to break a block with java (1)
[LINE @] I tried to make a Japanese calendar Western calendar conversion BOT [Messaging API]
I tried to make a machine learning application with Dash (+ Docker) part3 ~ Practice ~
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished)
[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
[Android] I tried to make a material list screen with ListView + Bottom Sheet
[Small story] I tried to make the java ArrayList a little more convenient
I tried using a database connection in Android development
I tried to make a new sorting algorithm, but I don't know if it's really new
I stumbled when I tried using neo4j in the jenv environment, so make a note
I tried to develop a ramen shop sharing website.
I tried using Gson
I tried to decorate the simple calendar a little
I tried to operate SQS using AWS Java SDK
When making a personal app, I was wondering whether to make it using haml
I tried using Wercker to create and publish a Docker image that launches GlassFish 5.
I tried to create a Clova skill in Java
I tried to make a message function of Rails Tutorial extension (Part 1): Create a model
I tried using TestNG
I tried using Log4j2 on a Java EE server
I tried using Galasa
I tried to make FizzBuzz that is uselessly flexible
I tried scraping a stock chart using Java (Jsoup)
I tried to build an environment using Docker (beginner)
I tried to make a reply function of Rails Tutorial extension (Part 3): Corrected a misunderstanding of specifications
I tried to make the sample application into a microservice according to the idea of the book "Microservice Architecture".
[Java] I tried to make a rock-paper-scissors game that beginners can run on the console.
I tried to make a message function of Rails Tutorial extension (Part 2): Create a screen to display
I tried to make an application in 3 months from inexperienced
I tried to make an introduction to PHP + MySQL with Docker
I tried to create a java8 development environment with Chocolatey
I tried using the GitHub repository as a library server