You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity IL2CPP构建下HttpClient配置代理的可行解决方案

Unity IL2CPP构建下HttpClient系统代理失效的解决方案

这段代码在Unity编辑器及Mono构建环境中可正常运行,但切换到IL2CPP构建后失效,官方已明确表示不会修复该问题。针对IL2CPP构建环境,以下是几种可行的解决方案:

原问题代码

if (Settings.GetBool_AndIsTrue("useSystemProxy"))
{
    print("use System proxy");
    HttpClientHandler handler = new();
    handler.Proxy = WebRequest.GetSystemWebProxy();
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
                                           | SecurityProtocolType.Tls
                                           | (SecurityProtocolType)0x300 //Tls11 .net framework 4.0
                                           | (SecurityProtocolType)0xC00; //Tls12 .net framework 4.0
    httpClient = new HttpClient(handler);
}

httpClient.Timeout = new TimeSpan(0, 0, 0, 10);

string result = string.Empty;
//first try
try
{
    result = httpClient.GetStringAsync(url).Result;
}

可行解决方案

1. 手动读取系统代理信息并配置

IL2CPP环境下WebRequest.GetSystemWebProxy()无法正常工作,可通过读取系统底层配置(如Windows注册表、macOS/iOS系统偏好)获取代理地址和端口,手动创建WebProxy实例:

string proxyAddress = string.Empty;
int proxyPort = 8080;

// Windows平台读取注册表示例
#if UNITY_STANDALONE_WIN
using (var regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings"))
{
    if (regKey != null)
    {
        var proxyEnable = regKey.GetValue("ProxyEnable");
        var proxyServer = regKey.GetValue("ProxyServer");
        
        if (proxyEnable != null && (int)proxyEnable == 1 && proxyServer != null)
        {
            string proxyStr = proxyServer.ToString();
            if (proxyStr.Contains(":"))
            {
                var parts = proxyStr.Split(':');
                proxyAddress = parts[0];
                int.TryParse(parts[1], out proxyPort);
            }
            else
            {
                proxyAddress = proxyStr;
            }
        }
    }
}
#endif

// macOS/iOS平台可通过调用系统API读取代理,此处省略示例

if (!string.IsNullOrEmpty(proxyAddress))
{
    var handler = new HttpClientHandler();
    handler.Proxy = new WebProxy(proxyAddress, proxyPort);
    httpClient = new HttpClient(handler);
}

2. 替换为UnityWebRequest实现

UnityWebRequest在IL2CPP环境下对系统代理的支持更稳定,默认会自动适配系统代理,可直接替换原有HttpClient逻辑:

IEnumerator FetchDataWithUnityWebRequest(string url)
{
    if (Settings.GetBool_AndIsTrue("useSystemProxy"))
    {
        print("use System proxy");
    }

    using var request = UnityWebRequest.Get(url);
    request.timeout = 10;
    // 自动使用系统代理,无需额外配置
    yield return request.SendWebRequest();

    if (request.result != UnityWebRequest.Result.Success)
    {
        Debug.LogError(request.error);
    }
    else
    {
        string result = request.downloadHandler.text;
        // 处理返回结果
    }
}

3. 调整IL2CPP相关配置

在Unity编辑器中修改以下配置,部分场景下可修复代理兼容性问题:

  • 打开PlayerSettings > Other Settings > Configuration,将Api Compatibility Level设置为.NET Framework(而非.NET Standard)
  • 开启Allow unsafe code选项

内容的提问来源于stack exchange,提问作者kei233

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 23:55:21