/// <summary>Get the raw file data (based on the PowerUI FileProtocol system).</summary> public override void OnGetDataNow(ContentPackage package) { // The endpoint (this is e.g. /v1/hello): string endpointPath = package.location.pathname; if (endpointPath.StartsWith("/v1")) { if (!endpointPath.EndsWith("/")) { endpointPath += "/"; } // The request payload (can be null): Json.JSObject request = Json.JSON.Parse(package.requestText); package.responseHeaders["content-type"] = "application/json; charset=utf-8"; // Get an endpoint: Endpoint ep = Endpoint.Get(endpointPath); if (ep == null) { // Endpoint not found. package.Failed(404); } else { Response response = new Response(); // Pass the request to the endpoint: ep.OnRequest(package, response, request); // Output the response: package.ReceivedText(response.ToString()); } return; } // Serve a file from PowerTools. // The browser embedded in the Unity editor (not PowerUI) // is webkit which doesn't allow ajax to file:// uri's. if (!File.Exists(PowerToolsPath + endpointPath)) { // Doesn't exist: package.Failed(404); } else { // Currently always HTML files down here: package.responseHeaders["content-type"] = "text/html"; // Read the file: byte[] data = File.ReadAllBytes(PowerToolsPath + endpointPath); // Serve it up: package.ReceivedData(data, 0, data.Length); } }
/// <summary>Advances this request by checking in on it's progress.</summary> public void Update(float deltaTime) { if (WWWRequest == null) { // Not setup yet. return; } if (LoadingEnumerator == null) { // Let's go! LoadingEnumerator = Loader(); // Advance the first time: LoadingEnumerator.MoveNext(); } Duration += deltaTime; if (Duration >= Timeout_) { // Timeout! Package.TimedOut(); // Done: Remove(); return; } if (WWWRequest.isDone) { if (Package.readyState_ < 2) { // Load the headers: HandleHeaders(); } if (Errored) { // Figure out a suitable error code: Package.statusCode = ErrorHandlers.GetUnityErrorCode(Error); } if (!WWWRequest.isDone) { // Yes, this is actually possible! Quit and go around again: return; } // Received: #if UNITY_2017_1_OR_NEWER byte[] bytes = WWWRequest.downloadHandler.data; #else byte[] bytes = WWWRequest.bytes; #endif Package.ReceivedData(bytes, 0, bytes.Length); // Pop it from the update queue: Remove(); } else if (Progress != 0f) { if (Package.readyState_ < 2) { // Got headers yet? HandleHeaders(); #if !MOBILE && !UNITY_WEBGL && !UNITY_TVOS } else if (Package.readyState_ == 2 && Movie != null && Movie.isReadyToPlay) { // Downloaded it far enough to try playing it - let the package know: Package.ReceivedMovieTexture(Movie); #endif } } }