Unity3d 適配IPV6

背景

WWDC2015蘋果宣布在ios9支持純ipv6的網(wǎng)絡(luò)服務(wù),并且要求2016年提交到AppStore的應(yīng)用必須適配純ipv6的網(wǎng)絡(luò)環(huán)境,要求適配的系統(tǒng)版本ios9及以上
5.3.4p4以后的Unity已經(jīng)對支持IPV6,如果你的項目里只用到WWW 或者 UnityWebRequest,恭喜,你不用做任何事情,只需要搭建ipv6的網(wǎng)絡(luò)環(huán)境,然后在ipv6的環(huán)境測試即可Unity and IPV6 Support

適配IPV6

如果項目中使用了非www,UnityWebRequest的其他網(wǎng)絡(luò)請求方式,就需要把ipv4的地址轉(zhuǎn)換成ipv6進(jìn)行請求,所以在純ipv6的環(huán)境下,是可以使用ipv4的ip進(jìn)行網(wǎng)絡(luò)訪問的,ios9會把ipv4 的地址轉(zhuǎn)換成ipv6的地址
git上已經(jīng)ipv4轉(zhuǎn)ipv6的項目,直接拿來用,不用自己寫oc腳本了,哈哈 unity-ios-ipv6-ready

具體步驟如下
1、把git項目里的 IPv6.m和 IPv6.h 放到 Plugins/IOS下
2、模仿git項目中的DemoTest.cs 寫出以下接口

//調(diào)用ios插件轉(zhuǎn)換ipv4到ipv6
[DllImport ("__Internal")]
    private static extern string getIPv6 (string host);
/// <summary>
    /// 拿當(dāng)前的ip地址或者域名來獲取對應(yīng)ipv6的地址,,如果當(dāng)前環(huán)境不支持ipv6,返回 當(dāng)前的ipv4地址或者對應(yīng)域名
    /// </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>
    /// 獲取域名對應(yīng)ipv6地址
    /// </summary>
    /// <returns>The IP v6 adress.</returns>
    /// <param name="hostName">Host name.</param>
    private static string GetIPV6Adress (string hostName)
    {
        //基礎(chǔ)操作系統(tǒng)和網(wǎng)絡(luò)適配器是否支持 Internet 協(xié)議版本 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轉(zhuǎn)換成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、把項目里所有網(wǎng)絡(luò)請求的地方檢查一遍,把非WWW和UnityWebRequest的地方 的url 轉(zhuǎn)換一下

這樣的OK了,打包,在ipv6網(wǎng)絡(luò)環(huán)境下測試就行了

最后附上 ipv6 的搭建方法IPV6熱點搭建

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容