unitywebrequest下载文本文件
时间: 2023-09-10 21:10:37 浏览: 100
可以使用UnityWebRequest来下载文本文件,具体步骤如下:
1. 创建UnityWebRequest对象,并设置下载地址和请求方式:
```csharp
string url = "http://www.example.com/textfile.txt";
UnityWebRequest request = UnityWebRequest.Get(url);
```
2. 发送请求并等待响应:
```csharp
yield return request.SendWebRequest();
```
3. 检查是否有错误发生:
```csharp
if (request.result != UnityWebRequest.Result.Success)
{
Debug.Log(request.error);
}
else
{
// 下载完成,可以获取文本内容
string text = request.downloadHandler.text;
Debug.Log(text);
}
```
完整示例代码:
```csharp
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class TextFileDownloader : MonoBehaviour
{
IEnumerator Start()
{
string url = "http://www.example.com/textfile.txt";
UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
Debug.Log(request.error);
}
else
{
string text = request.downloadHandler.text;
Debug.Log(text);
}
}
}
```
阅读全文