Unity 2022.3.10.f1 XR项目构建后API请求报错:无法解析目标主机
问题:Unity XR VR构建包后REST API请求失败,提示"Cannot resolve destination host"
在Unity 2022.3.10.f1开发XR VR项目时,调试模式下REST API请求正常,但构建为VR包后请求失败,错误信息:
"Cannot resolve destination host"
已配置的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.unity3d.player" xmlns:tools="http://schemas.android.com/tools"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application> <activity android:name="com.unity3d.player.UnityPlayerActivity" android:theme="@StyLe/UnityThemeSelector"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="unityplayer.UnityActivity" android:value="true" /> </activity> </application> </manifest>
使用的REST请求代码:
public static IEnumerator GetRequest(string uri) { using (UnityWebRequest webRequest = new UnityWebRequest(uri, "GET")) { webRequest.downloadHandler = new DownloadHandlerBuffer(); // Send the request and wait for a response yield return webRequest.SendWebRequest(); if (webRequest.result == UnityWebRequest.Result.ConnectionError) { // Log any errors Debug.Log(": Error: " + webRequest.error); } else { // Log the response Debug.Log(": Received: " + webRequest.downloadHandler.text); } } }
调用示例:
GetRequest("https://www.google.com/")
已尝试的无效方案:
- 在项目设置>Player>发布设置中启用Internet权限
- 使用自定义清单文件配置Internet和本地访问权限
解决方案排查步骤
1. 配置Android网络安全文件(适配Android 9+)
Android 9及以上对网络请求有严格限制,即使是HTTPS请求也可能因安全配置问题触发解析错误:
- 在
Assets/Plugins/Android/res/xml目录下创建network_security_config.xml文件,内容如下:
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <base-config cleartextTrafficPermitted="true"> <trust-anchors> <certificates src="system" /> </trust-anchors> </base-config> </network-security-config>
- 在AndroidManifest.xml的
<application>标签中添加该配置引用:
<application android:networkSecurityConfig="@xml/network_security_config">
2. 排查DNS解析问题
调试模式下PC的DNS与VR设备的DNS环境不同,可能导致域名解析失败:
- 在VR设备的网络设置中手动指定公共DNS(如8.8.8.8或1.1.1.1)
- 用目标域名对应的IP地址替换URL测试请求,比如将
https://www.google.com/替换为https://142.250.185.14/,验证是否为DNS解析问题
3. 检查Unity Player构建设置
- 打开
Edit > Project Settings > Player > Android > Publishing Settings,确认Custom Main Manifest已勾选,且路径指向你配置的AndroidManifest.xml - 在
Player Settings > Other Settings > Configuration中,将Internet Access设置为Require - 确认
Minimum API Level适配VR设备的Android版本,建议设置为Android 9(API Level 28)及以上
4. 验证VR设备的应用权限
部分VR设备不会自动授予网络权限:
- 在设备系统设置中找到你的应用,手动开启网络/Internet权限
- 若为Oculus Quest系列设备,可通过手机端Oculus应用管理应用权限
5. 简化UnityWebRequest实现
尝试使用Unity官方封装的UnityWebRequest.Get方法,避免手动配置遗漏:
public static IEnumerator GetRequest(string uri) { using (UnityWebRequest webRequest = UnityWebRequest.Get(uri)) { yield return webRequest.SendWebRequest(); if (webRequest.result != UnityWebRequest.Result.Success) { Debug.LogError("Request Error: " + webRequest.error); } else { Debug.Log("Response: " + webRequest.downloadHandler.text); } } }
内容的提问来源于stack exchange,提问作者Partha Sai
相关产品推荐
相关产品推荐

