• 【100个 Unity实用技能】☀️ | Unity中 检查当前设备网络状态 的几种方法整理


    在这里插入图片描述

    Unity 小科普

    老规矩,先介绍一下 Unity 的科普小知识:

    • Unity是 实时3D互动内容创作和运营平台 。
    • 包括游戏开发美术建筑汽车设计影视在内的所有创作者,借助 Unity 将创意变成现实。
    • Unity 平台提供一整套完善的软件解决方案,可用于创作、运营和变现任何实时互动的2D和3D内容,支持平台包括手机平板电脑PC游戏主机增强现实虚拟现实设备。
    • 也可以简单把 Unity 理解为一个游戏引擎,可以用来专业制作游戏
    • 🎬 博客主页:https://xiaoy.blog.csdn.net

    • 🎥 本文由 呆呆敲代码的小Y 原创,首发于 CSDN🙉

    • 🎄 学习专栏推荐:Unity系统学习专栏

    • 🌲 游戏制作专栏推荐:游戏制作

    • 🌲Unity实战100例专栏推荐:Unity 实战100例 教程

    • 🏅 欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!

    • 📆 未来很长,值得我们全力奔赴更美好的生活✨

    • ------------------❤️分割线❤️-------------------------

    请添加图片描述


    Unity 实用小技能学习

    Unity中 检查当前设备网络状态 的几种方法整理

    在做项目时有时候可能需要拿到当前设备的网络状态,所以本文整理了在Unity中可以用到的一些拿到网络状态的方法,下面一起来看看吧。

    第一种:使用Unity自己的API判断网络状态

    Unity API 如下:https://docs.unity3d.com/ScriptReference/NetworkReachability.ReachableViaLocalAreaNetwork.html

    /// 
    ///   Describes network reachability options.
    /// 
    public enum NetworkReachability
    {
    	/// 
    	///   Network is not reachable.
    	/// 
    	NotReachable,
    	/// 
    	///   Network is reachable via carrier data network.
    	/// 
    	ReachableViaCarrierDataNetwork,
    	/// 
    	///   Network is reachable via WiFi or cable.
    	/// 
    	ReachableViaLocalAreaNetwork
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    使用方法如下:

        void Start()
        {   
            // 1
            Debug.Log("当前网络状态:"+ Application.internetReachability);
        
            // 2
            switch (Application.internetReachability)
            {
                case NetworkReachability.NotReachable:
                    Debug.Log("当前网络不可用");
                    break;
                case NetworkReachability.ReachableViaCarrierDataNetwork:
                    Debug.Log("当前网络为移动网络");
                    break;
                case NetworkReachability.ReachableViaLocalAreaNetwork:
                    Debug.Log("当前网络为Wifi");
                    break;
                default:
                    break;
            }
            
            // 3
            if (Application.internetReachability == NetworkReachability.NotReachable)
            {
                Debug.Log("当前网络不可用");
            }
            else if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
            {
                Debug.Log("当前网络为移动网络");
            }
            else if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)
            {
                Debug.Log("当前网络为Wifi");
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    优点:方便快捷,使用一行API代码就可以查询此时的网络状态
    缺点:项目打包成exe后无法通过该方法判断网络状态

    第二种方法:引用外部库wininet

        [DllImport("wininet")]
        private extern static bool InternetGetConnectedState(ref int connectionDescription, int reservedValue);
    
        public bool IsConnectedInternet()
        {
            int i = 0;
            if (InternetGetConnectedState(ref i, 0))
            {
                Debug.Log("已联网");
                return true;
            }
            else
            {
                Debug.Log("未联网");
                return false;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    第三种方法:Ping命令检测网络是否畅通

        public bool PingIsHaveNet(string url)
        {
            System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
    
            PingReply ret = ping.Send(url);
            try
            {
                if (ret.Status != IPStatus.Success)
                {
                    Debug.Log("未联网");
                    return false;
                }
                else
                {
                    Debug.Log("已联网");
                    return true;
                }
            }
            catch (Exception e)
            {
                Debug.Log("Ping URL 失败");
                return false;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    第四种方法: 通过 .Net.NetworkInformation 方法检测

    需要引用命名空间 using System.Net.NetworkInformation

        private bool UrlExistsUsingSockets(string url)
        {
            if (url.StartsWith("https://")) url = url.Remove(0, "https://".Length);
            try
            {
                System.Net.IPHostEntry ipHost = System.Net.Dns.GetHostEntry(url);// System.Net.Dns.Resolve(url);
                Debug.Log("已联网");
                return true;
            }
            catch (System.Net.Sockets.SocketException se)
            {
                System.Diagnostics.Trace.Write(se.Message);
                return false;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    以上就是整理的几种查询当前设备网络状态的几种方法,如果有更好的方法可以评论区留言提出哦~


    在这里插入图片描述

  • 相关阅读:
    软件测试学习(三)易用性测试、测试文档、软件安全性测试、网站测试
    Spring Boot前后端分离之后端开发
    前后端结合解决Excel海量公式计算的性能问题
    tensorflow遇到的问题
    Java给图片增加水印,根据图片大小自适应,右下角/斜角/平铺
    [附源码]java毕业设计网上点餐系统
    MobTech短信验证ApiCloud SDK
    day065:IO流、字节流、字节流写数据
    React router6--路由表多级路由嵌套的默认子路由问题
    如何根据rfc5280中的crl流程写一个crl吊销查询代码
  • 原文地址:https://blog.csdn.net/zhangay1998/article/details/127290233