unity webrequest 发布android 支持 http

解决Unity手游在Android平台上无法直接访问HTTP的问题,通过在plugins下创建Android文件夹及AndroidManifest.xml,设置应用使用明文HTTP流量,确保游戏能正常进行网络请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

unity webrequest支持 http


在unity 发布到手机上 android 不支持直接访问 http 需要设置一下。
否则得到 unknow 错误

解决如下:

1,在plugins 中 建立 文件夹 Android

2,建立一个 AndroidManifest.xml

3,内容填入:

<?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" android:installLocation="preferExternal">
    <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
    <application android:theme="@style/UnityThemeSelector" android:icon="@mipmap/app_icon" android:label="@string/app_name" android:usesCleartextTraffic="true">
        <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name">
            <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>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

4, 问题解决

参考官网:

https://forum.unity.com/threads/android-9-cleartext-http-traffic-to-my_ip-not-permitted.594298/

### 使用 UnityWebRequest 读取本地文件 为了在 Unity 中使用 `UnityWebRequest` 来读取本地 JSON 文件,可以采用以下方法。需要注意的是,在 Unity 编辑器环境中访问本地文件路径的方式与打包后的游戏有所不同。 对于编辑器环境中的开发测试,可以通过指定相对路径来加载位于项目 Assets 文件夹下的资源: ```csharp using UnityEngine; using System.IO; public class LoadLocalJson : MonoBehaviour { void Start() { string filePath = Path.Combine(Application.streamingAssetsPath, "example.json"); // 如果是在 PC 或 Mac 上运行,则可以直接通过 File.ReadAllText 加载文件内容 #if UNITY_STANDALONE || UNITY_EDITOR string jsonString = File.ReadAllText(filePath); Debug.Log(jsonString); #endif // 对于 Android 和 iOS 平台上的构建版本,需使用 UnityWebRequest 获取 StreamingAssets 路径下文件的内容 #if UNITY_ANDROID || UNITY_IOS StartCoroutine(LoadJsonFromStreamingAssets()); #endif } IEnumerator LoadJsonFromStreamingAssets() { using (UnityWebRequest www = UnityWebRequest.Get(Path.Combine("file://" + Application.streamingAssetsPath, "example.json"))) { yield return www.SendWebRequest(); if(www.result != UnityWebRequest.Result.Success) Debug.LogError($"Failed to load json file: {www.error}"); else { string jsonString = www.downloadHandler.text; Debug.Log(jsonString); } } } } ``` 上述代码展示了如何根据不同平台处理本地 JSON 文件的读取操作[^1]。当处于 Windows、MacOS 玉米编译环境下时,直接利用 C# 的 IO 类库读取;而在移动平台上则借助 `UnityWebRequest` 发起请求获取数据并解析返回的结果字符串作为 JSON 数据源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值