2020. 8. 20. 15:54
Unity/C#
파일을 다운로드
public string url;
public string fileName;
public string fileType;
IEnumerator DownloadFile()
{
string filePath= $"{Application.persistentDataPath}/{fileName}";
if( (fileType != null)&& (fileType.Trim() != "") )
{
filePath += $".{fileType}";
}
UnityWebRequest unityWebRequest = UnityWebRequest.Get(url);
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.isNetworkError)
{
Debug.LogError(unityWebRequest.error);
}
else
{
File.WriteAllBytes(filePath, unityWebRequest.downloadHandler.data);
}
}
V2
/// <summary>
/// 다운 다되면 파일경로를 반환
/// </summary>
/// <param name="downloadlink"></param>
/// <param name="filePath"></param>
/// <returns></returns>
IEnumerator DownloadFile(string downloadlink, string filePath)
{
Debug.Log($"Download:{downloadlink}");
{
DOWNLOAD_RETRY:;
{
var unityWebRequest = UnityWebRequest.Get(downloadlink);
var operation = unityWebRequest.SendWebRequest();
yield return new WaitUntil(() => operation.isDone);
if (unityWebRequest.isNetworkError)
{
Debug.LogError(unityWebRequest.error);
yield return new WaitForSeconds(3f);
goto DOWNLOAD_RETRY;
}
else
{
Debug.Log(filePath);
var folderPath = System.IO.Path.GetDirectoryName(filePath);
Debug.Log(folderPath);
if (System.IO.Directory.Exists(folderPath) == false)//폴더가 없으면 생성
{
System.IO.Directory.CreateDirectory(folderPath);
}
System.IO.File.WriteAllBytes(filePath, unityWebRequest.downloadHandler.data);
}
}
}
yield return filePath;
}
DownloadHandlerFile 방식
메모리를 딱 필요한만큼만 쓴다. 위에는 2배사용
V3
List<string> downloadList = new List<string>();
IEnumerator DownloadFile(string downloadlink, string filePath)
{
downloadList.Add(downloadlink);
DOWNLOADRETRY:;
{
UnityWebRequest unityWebRequest = UnityWebRequest.Get(downloadlink);
unityWebRequest.downloadHandler = new DownloadHandlerFile(filePath);
var operation = unityWebRequest.SendWebRequest();
yield return new WaitUntil(() => operation.isDone);
if (unityWebRequest.isNetworkError)
{
Debug.LogError(unityWebRequest.error);
yield return new WaitForSeconds(1f);
goto DOWNLOADRETRY;
}
else
{
Debug.Log(filePath);
}
}
downloadList.Remove(downloadlink);
}
V1~V2
더보기
IEnumerator DownloadFile(string downloadlink, string filePath)
{
UnityWebRequest unityWebRequest = UnityWebRequest.Get(downloadlink);
unityWebRequest.downloadHandler = new DownloadHandlerFile(filePath);
var operation = unityWebRequest.SendWebRequest();
while (!operation.isDone)
{
yield return new WaitForSeconds(0.01f);
}
if (unityWebRequest.isNetworkError)
{
Debug.LogError(unityWebRequest.error);
}
else
{
Debug.Log(filePath);
}
}
V2
List<String> DownloadList = new List<String>();
IEnumerator DownloadFile(string downloadlink, string filePath)
{
UnityWebRequest unityWebRequest = UnityWebRequest.Get(downloadlink);
unityWebRequest.downloadHandler = new DownloadHandlerFile(filePath);
DownloadList.Add(downloadlink);
DOWNLOADRETRY:;
var operation = unityWebRequest.SendWebRequest();
while (!operation.isDone)
{
yield return new WaitForSeconds(0.01f);
}
if (unityWebRequest.isNetworkError)
{
Debug.LogError(unityWebRequest.error);
yield return new WaitForSeconds(1f);
goto DOWNLOADRETRY;
}
else
{
Debug.Log(filePath);
}
DownloadList.RemoveAt(DownloadList.FindIndex(0, DownloadList.Count, x => x == downloadlink));
}
HTML 다운로드
public string urlSample= "https://wmmu.tistory.com/";
IEnumerator DownloadHTML(string url)
{
UnityWebRequest unityWebRequest;
unityWebRequest = UnityWebRequest.Get(url);
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.isNetworkError)
{
Debug.LogError(unityWebRequest.error);
}
else
{
Debug.Log(unityWebRequest.downloadHandler.text);
}
}
오디오 파일 로드
//전용 세이브 폴더 참조시에는
//$"file://{UnityEngine.Application.persistentDataPath}/Audio.mp3";
static AudioClip LoadAudioClip(string downloadLink)
{
var fileName= downloadLink;
if (System.IO.File.Exists(downloadLink))
{
downloadLink = $"file://{downloadLink}";
fileName = System.IO.Path.GetFileNameWithoutExtension(downloadLink);
}
UnityWebRequest unityWebRequest = UnityWebRequestMultimedia.GetAudioClip(downloadLink, AudioType.UNKNOWN);
var operation = unityWebRequest.SendWebRequest();
while (!operation.isDone)
{
Thread.Sleep(1);
}
if (unityWebRequest.isNetworkError)
{
Debug.LogError(unityWebRequest.error);
}
else
{
//Debug.Log("LoadAudioClipSuccess");
}
var clip = DownloadHandlerAudioClip.GetContent(unityWebRequest);
clip.name = fileName;
return clip;
}
캐시 테스트
public string url;
public string cookie;
public string fileName;
public string fileType;
IEnumerator DownloadFile()
{
string filePath = $"{Application.persistentDataPath}/{fileName}";
if ((fileType != null) && (fileType.Trim() != ""))
{
filePath += $".{fileType}";
}
UnityWebRequest unityWebRequest = UnityWebRequest.Get(url);
if (string.IsNullOrWhiteSpace(cookie)==false)
{
unityWebRequest.SetRequestHeader("Cookie", cookie);
}
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.isNetworkError)
{
Debug.LogError(unityWebRequest.error);
}
else
{
Debug.Log(unityWebRequest.downloadHandler.text);
//File.WriteAllBytes(filePath, unityWebRequest.downloadHandler.data);
}
}
// Start is called before the first frame update
void Start()
{
StartCoroutine(DownloadFile());
}
'Unity > C#' 카테고리의 다른 글
c# MD5 (0) | 2020.08.25 |
---|---|
형변환 모음 (0) | 2020.08.11 |
유니티 씬 계열 함수 모음 (0) | 2020.07.30 |