- Get and then save file
```
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
public class GetImageFromWeb : MonoBehaviour
{
void Awake()
{
StartCoroutine(DownLoadGet("URL"));
}
public IEnumerator DownLoadGet(string URL)
{
UnityWebRequest request = UnityWebRequest.Get(URL);
//해더 정보 필요시
//request.SetRequestHeader("key", "timebox");
yield return request.SendWebRequest();
//에러 발생시
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError )
{
Debug.Log(request.error);
}
else
{
//다운로드 데이터를 파일로 저장
SaveFile (request.downloadHandler.data);
}
}
public void SaveFile(byte[] bytes)
{
string destination = Application.persistentDataPath + "/file.png";
//private string destination = Application.persistentDataPath + "/file.png"; // android
//string destination = "c:\file.png"; // for pc
if(File.Exists(destination))
{
File.Delete (destination);
}
File.WriteAllBytes (destination, bytes);
//Debug.Log(destination);
}
}
```
'tech > metaverse' 카테고리의 다른 글
21/09/13 Unity 3D House Interior (0) | 2021.09.13 |
---|---|
21/09/13 Loading 3D Models from the Web at Runtime in Unity (0) | 2021.09.13 |
21/09/11 Create a Note Appear System (0) | 2021.09.11 |
21/09/11 Interaction Feature (0) | 2021.09.11 |
21/09/11 Camera Zoom in Unity - C# (0) | 2021.09.11 |