Unity具有网络连接确认功能
使用NWPath Monitor(比"可达性"更简单)来检测iOS的网络连接状态。 --Qiita
对于Swift的处理,我在这里引用了这篇文章。
斯威夫特方面
UniNWPathMonitor.swift
1 2 3 4 5 6 7 8 9 | monitor.pathUpdateHandler = { path in if path.status == .satisfied { // connected self._onCallback("1") } else { // No connected self._onCallback("0") } } |
如果使用NWPathMonitor实例(监视器)进行连接,则将返回
Objective-C面
1 2 | API_AVAILABLE(ios(12.0)) UniNWPathMonitor *_monitor; |
由于这次需要iOS12或更高版本才能使用NWPathMonitor,因此请在
UniNWPathMonitorBridge.mm
1 2 3 | [_monitor setCallbackOnCallback:^(NSString * value) { UnitySendMessage("UniNWPathMonitor", "Noti", [value UTF8String]); }]; |
设置UniNWPathMonitor的回调。
返回回调后,使用
有关Objective-C的描述,请参阅我之前在此处撰写的文章。
[Unity]在最近的Unity --Qiita
中从C#执行Swift的最短实现
统一边
这是在Unity端使用它的方式。
只需在UniNWPathMonitor上运行StartMonitor并设置回调即可。
Sample.cs
1 2 3 4 5 6 7 8 9 10 11 | void Start() { UniNWPathMonitor.StartMonitor(); UniNWPathMonitor.onChangeNetworkStatus += status => { if (status == UniNWPathMonitor.NetworkStatus.satisfied) // 接続中 else // 切断中 }; } |

点击这里查看我实际制作的内容。我打开和关闭Wifi,并在Unity端接收回调。在切换后回调返回到Unity之前似乎会有一点滞后。
终于
正如我在一开始提到的那样,Unity不需要此插件,因为存在Application.internetReachability。 *之所以成功,是因为我想做到。
对我来说,我实际上是从中学到了很多东西。很高兴能有机会学习我正在研究的S??wift,例如首次出现的Swift回调语法以及如何使iOS 12或更高版本成为必需。
所有资源都在下面上传,因此请参考它们。
https://github.com/baobao/UniNWPathMonitor
开发环境
- Unity 2019.4.4f1
- Xcode11.6
- iPhone6S iOS13.6