123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using BestHTTP;
- namespace BestHTTP.Examples
- {
- public sealed class TextureDownloadSample : MonoBehaviour
- {
-
-
-
- const string BaseURL = "https://besthttp.azurewebsites.net/Content/";
- #region Private Fields
-
-
-
- string[] Images = new string[9] { "One.png", "Two.png", "Three.png", "Four.png", "Five.png", "Six.png", "Seven.png", "Eight.png", "Nine.png" };
-
-
-
- Texture2D[] Textures = new Texture2D[9];
- #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
-
-
-
- bool allDownloadedFromLocalCache;
- #endif
-
-
-
- int finishedCount;
-
-
-
- Vector2 scrollPos;
- #endregion
- #region Unity Events
- void Awake()
- {
-
-
- HTTPManager.MaxConnectionPerServer = 1;
-
- for (int i = 0; i < Images.Length; ++i)
- Textures[i] = new Texture2D(100, 150);
- }
- void OnDestroy()
- {
-
- HTTPManager.MaxConnectionPerServer = 4;
- }
- void OnGUI()
- {
- GUIHelper.DrawArea(GUIHelper.ClientArea, true, () =>
- {
- scrollPos = GUILayout.BeginScrollView(scrollPos);
-
- GUILayout.SelectionGrid(0, Textures, 3);
- #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
- if (finishedCount == Images.Length && allDownloadedFromLocalCache)
- GUIHelper.DrawCenteredText("All images loaded from the local cache!");
- #endif
- GUILayout.FlexibleSpace();
- GUILayout.BeginHorizontal();
- GUILayout.Label("Max Connection/Server: ", GUILayout.Width(150));
- GUILayout.Label(HTTPManager.MaxConnectionPerServer.ToString(), GUILayout.Width(20));
- HTTPManager.MaxConnectionPerServer = (byte)GUILayout.HorizontalSlider(HTTPManager.MaxConnectionPerServer, 1, 10);
- GUILayout.EndHorizontal();
- if (GUILayout.Button("Start Download"))
- DownloadImages();
- GUILayout.EndScrollView();
- });
- }
- #endregion
- #region Private Helper Functions
- void DownloadImages()
- {
-
- #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
- allDownloadedFromLocalCache = true;
- #endif
- finishedCount = 0;
- for (int i = 0; i < Images.Length; ++i)
- {
-
- Textures[i] = new Texture2D(100, 150);
-
- var request = new HTTPRequest(new Uri(BaseURL + Images[i]), ImageDownloaded);
-
- request.Tag = Textures[i];
-
- request.Send();
- }
- }
-
-
-
- void ImageDownloaded(HTTPRequest req, HTTPResponse resp)
- {
-
- finishedCount++;
- switch (req.State)
- {
-
- case HTTPRequestStates.Finished:
- if (resp.IsSuccess)
- {
-
- Texture2D tex = req.Tag as Texture2D;
-
- tex.LoadImage(resp.Data);
- #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
-
- allDownloadedFromLocalCache = allDownloadedFromLocalCache && resp.IsFromCache;
- #endif
- }
- else
- {
- Debug.LogWarning(string.Format("Request finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
- resp.StatusCode,
- resp.Message,
- resp.DataAsText));
- }
- break;
-
- case HTTPRequestStates.Error:
- Debug.LogError("Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception"));
- break;
-
- case HTTPRequestStates.Aborted:
- Debug.LogWarning("Request Aborted!");
- break;
-
- case HTTPRequestStates.ConnectionTimedOut:
- Debug.LogError("Connection Timed Out!");
- break;
-
- case HTTPRequestStates.TimedOut:
- Debug.LogError("Processing the request Timed Out!");
- break;
- }
- }
- #endregion
- }
- }
|