Unity3d 适配IPV6

来源:互联网 发布:广告市场有多大 知乎 编辑:程序博客网 时间:2024/05/20 06:50

原博:http://www.jianshu.com/p/8d6d5e5aca97

背景

WWDC2015苹果宣布在ios9支持纯ipv6的网络服务,并且要求2016年提交到AppStore的应用必须适配纯ipv6的网络环境,要求适配的系统版本ios9及以上5.3.4p4以后的Unity已经对支持IPV6,如果你的项目里只用到WWW 或者 UnityWebRequest,恭喜,你不用做任何事情,只需要搭建ipv6的网络环境,然后在ipv6的环境测试即可Unity and IPV6 Support

适配IPV6

如果项目中使用了非www,UnityWebRequest的其他网络请求方式,就需要把ipv4的地址转换成ipv6进行请求,所以在纯ipv6的环境下,是可以使用ipv4的ip进行网络访问的,ios9会把ipv4 的地址转换成ipv6的地址git上已经ipv4转ipv6的项目,直接拿来用,不用自己写oc脚本了,哈哈 unity-ios-ipv6-ready

具体步骤如下1、把git项目里的 IPv6.m和 IPv6.h 放到 Plugins/IOS下2、模仿git项目中的DemoTest.cs 写出以下接口

//调用ios插件转换ipv4到ipv6[DllImport ("__Internal")]    private static extern string getIPv6 (string host);
/// <summary>    /// 拿当前的ip地址或者域名来获取对应ipv6的地址,,如果当前环境不支持ipv6,返回 当前的ipv4地址或者对应域名    /// </summary>    /// <param name="hostOrHostName">Host or host name.</param>    public static string GetIpV6 (string hostOrHostName)    {        string ip = hostOrHostName;        #if UNITY_IPHONE &&!UNITY_EDITOR        if (IsIPAdress (hostOrHostName)) {            try{                ip = getIPv6 (hostOrHostName);                if (!string.IsNullOrEmpty (ip)) {                    string[] tmp = System.Text.RegularExpressions.Regex.Split (ip, "&&");                    if (tmp != null && tmp.Length >= 2) {                        string type = tmp[1];                        if (type == "ipv6") {                            ip = tmp[0];                            Debug.Log ("---ipv6--- " + ip);                        }else if(type == "ipv4") {                            ip = tmp[0];                            Debug.Log ("---ipv4--- " + ip);                        }                    }                }            }catch(Exception e){                Debug.LogErrorFormat ("GetIPv6 error: {0}", e.Message);            }        } else {            ip = GetIPV6Adress (hostOrHostName);        }        #else        return hostOrHostName;        #endif        Debug.Log ("hostOrHostName: -----" + hostOrHostName + "  -------- ip " + ip);        return ip;    }
        //判断str是域名还是ip    private static bool IsIPAdress (string str)    {        Match match = Regex.Match (str, @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");        return match.Success;    }
/// <summary>    /// 获取域名对应ipv6地址    /// </summary>    /// <returns>The IP v6 adress.</returns>    /// <param name="hostName">Host name.</param>    private static string GetIPV6Adress (string hostName)    {        //基础操作系统和网络适配器是否支持 Internet 协议版本 6 (IPv6)。 ,,并且域名不为null        if (!System.Net.Sockets.Socket.OSSupportsIPv6 || string.IsNullOrEmpty (hostName))            return null;        System.Net.IPHostEntry host;        string connectIP = "";        try {            host = System.Net.Dns.GetHostEntry (hostName);            foreach (System.Net.IPAddress ip in host.AddressList) {                if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) {                    connectIP = ip.ToString ();                } else if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) {                    connectIP = ip.ToString ();                }            }        } catch (Exception e) {            Debug.LogErrorFormat ("GetIPAddress error: {0}", e.Message);        }        Debug.Log ("---connectIP--- " + connectIP);        return connectIP;    }
//传一个url转换成ipv6 的地址public string FinalUrl(string url)    {        #if UNITY_IPHONE &&!UNITY_EDITOR        string[] strs = url.Split ('/');        if (strs.Length < 2)            return url;        string hostOrName = strs [2];        string finalIp = "";                //如果有端口去掉端口        if (hostOrName.Contains (":")) {            hostOrName = hostOrName.Split (':') [0];        }        finalIp =GetIpV6 (hostOrName);        //解析后的域名,通过是否包含冒号来判断是ipv6还是ipv4如果是ipv6格式的加上[] 不是ivp6格式不需要加,,,这块比较坑 不加[] 会报错,,非法的端口,,        if (finalIp.Contains (":")) {            finalIp = string.Format ("[{0}]", finalIp);        }        string finalUrl = url.Replace (hostOrName, finalIp);        return finalUrl;        #endif        //只有在苹果真机上才会处理IP 其他情况直接返回 url        return url;    }

3、把项目里所有网络请求的地方检查一遍,把非WWW和UnityWebRequest的地方 的url 转换一下

这样的OK了,打包,在ipv6网络环境下测试就行了

最后附上 ipv6 的搭建方法IPV6热点搭建



作者:串串香
链接:http://www.jianshu.com/p/8d6d5e5aca97
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

原创粉丝点击