Unity從網路伺服器下載檔案(各種雲端資源)的方法:
using UnityEngine.Networking;
void Start(){ StartCoroutine( Download_File() ); }
IEnumerator Download_File(){
string url = "https://www.yourServer.com/unity.jpg"; // 欲下載圖片的網路位址
string savePath = "d:/unity.jpg"; // 圖片下載後的儲存路徑
var uwr = new UnityWebRequest(url);
uwr.method = UnityWebRequest.kHttpVerbGET;
var dh = new DownloadHandlerFile(savePath);
dh.removeFileOnAbort = true;
uwr.downloadHandler = dh;
yield return uwr.SendWebRequest();
if (uwr.isNetworkError || uwr.isHttpError) { Debug.Log(uwr.error); }
else { Debug.Log("檔案已下載到:" + savePath); }
}
建議延伸閱讀:
>>
unity 載入外部文字檔(txt)
>>
unity C# 語法教學入門
>>
Unity Script 常用語法教學(unity課程入門學習筆記)