/// <summary>Loads external CSS if a href is available and it's known to be css.</summary> public void LoadContent() { if (!IsCSS || string.IsNullOrEmpty(Href) || styleSheet != null) { return; } // Let's go get it! styleSheet = htmlDocument.AddStyle(this, null); DataPackage package = new DataPackage(Href, document.basepath); styleSheet.Location = package.location; package.onload = delegate(UIEvent e){ if (document == null || styleSheet == null) { return; } // The element is still somewhere on the UI. // Load it now: styleSheet.ParseCss(package.responseText); // Redraw: htmlDocument.RequestLayout(); }; package.send(); }
/// <summary>Displays an error in the given document.</summary> public static void Display(ErrorInfo error) { // Create a package: DataPackage package = new DataPackage("resources://standardErrors.html"); // Load: package.onload = delegate(UIEvent e){ // Apply the innerHTML: error.htmlDocument.innerHTML = package.responseText; // Get the status code: int code = error.StatusCode; // Get the status element: HtmlElement element = error.htmlDocument.getElementById("error-code") as HtmlElement; // Apply: element.innerHTML = code + ""; // Get the URL element: element = error.htmlDocument.getElementById("error-url") as HtmlElement; // Apply: element.innerHTML = error.Url.absolute; }; // Get it now: package.send(); }
/// <summary>Don't use this directly; use location instead.</summary> internal override void SetLocation(Location value, bool addHistory) { // Make sure it opens correctly: IsOpen = false; open(); if (value == null) { return; } // Clear cache path: cachePath = null; // Update location: location_ = value; if (addHistory) { // Push a history entry: window.history.DocumentNavigated(); } // Update its parent document: value.document = this; if (AfterClearBeforeSet != null) { // Invoke during load now: AfterClearBeforeSet(createEvent("event", "duringload")); } // Reset readyState (we don't reset resources loading because we haven't killed any that are currently loading): ReadyStateChange(0); // Load the html: DataPackage package = new DataPackage(value.absolute, null); package.onreadystatechange = delegate(Dom.Event e){ if (package.readyState == 4) { if (package.redirectedTo != null) { // Set redir path: value = package.redirectedTo; // Update its parent document: value.document = this; } // Apply now: GotDocumentContent(package.responseText, package.statusCode, false); } }; // Send now: package.send(); }
public Worker(string fileURL) { // Download that file now: DataPackage package = new DataPackage(fileURL); // Apply onload method: package.onload = delegate(UIEvent e){ // Start the worker // TODO // Compiles with it's own global scope. }; // Send now: package.send(); }
/// <summary>Sends a request off to get the content of this tag if it's external (i.e. has an src attribute).</summary> private void LoadContent() { if (!Loaded || string.IsNullOrEmpty(Src)) { return; } if (Engine == null) { return; } // The code index makes sure that this script is loaded into this position relative to other script on the page: CodeIndex = Engine.GetCodeIndex(); DataPackage package = new DataPackage(Src, document.basepath); package.onload = delegate(UIEvent e){ if (document_ == null || CodeIndex < 0) { return; } // The element is still somewhere on the UI. string code = package.responseText; if (Engine != null) { Engine.AddCode(code, CodeIndex); } CodeIndex = -1; // We might also be the last code to download - attempt a compile now, but only if the document is done parsing. // If the doc isn't done parsing, it might not have added all the script to the buffer yet (and will try compile itself). if (htmlDocument.FinishedParsing) { htmlDocument.TryCompile(); } }; package.send(); }
/// <summary>Submits this form using the given button. It may override the action etc.</summary> public void submit(HtmlElement clickedButton) { // Generate a nice dictionary of the form contents. // Step 1: find the unique names of the elements: Dictionary <string, string> uniqueValues = new Dictionary <string, string>(); // Get submittable elements: HTMLFormControlsCollection allInputs = GetAllInputs(InputSearchMode.Submittable); foreach (Element element in allInputs) { if (element is HtmlButtonElement) { // No buttons. continue; } string type = element.getAttribute("type"); if (type == "submit") { // No submit either. continue; } string name = element.getAttribute("name"); if (name == null) { name = ""; } // Step 2: For each one, get their value. // We might have a name repeated, in which case we check if they are radio boxes. if (uniqueValues.ContainsKey(name)) { // Ok the element is already added - we have two with the same name. // If they are radio, then only overwrite value if the new addition is checked. // Otherwise, overwrite - furthest down takes priority. if (element.Tag == "input") { HtmlInputElement tag = (HtmlInputElement)element; if (tag.Type == InputType.Radio && !tag.Checked) { // Don't overwrite. continue; } } } string value = (element as HtmlElement).value; if (value == null) { value = ""; } uniqueValues[name] = value; } // Get the action: string action = GetOverriden("action", clickedButton); FormEvent formData = new FormEvent(uniqueValues); formData.SetTrusted(true); formData.EventType = "submit"; // Hook up the form element: formData.form = this; if (dispatchEvent(formData)) { // Get ready to post now! DataPackage package = new DataPackage(action, document.basepath); package.AttachForm(formData.ToUnityForm()); // Apply request to the data: formData.request = package; // Apply load event: package.onload = package.onerror = delegate(UIEvent e){ // Attempt to run ondone (doesn't bubble): formData.Reset(); formData.EventType = "done"; if (dispatchEvent(formData)) { // Otherwise the ondone function quit the event. // Load the result into target now. string target = GetOverriden("target", clickedButton); HtmlDocument targetDocument = ResolveTarget(target); if (targetDocument == null) { // Posting a form to an external target. Log.Add("Warning: Unity cannot post forms to external targets. It will be loaded a second time."); // Open the URL outside of Unity: UnityEngine.Application.OpenURL(package.location.absoluteNoHash); } else { // Change the location: targetDocument.SetRawLocation(package.location); // History entry required: targetDocument.window.history.DocumentNavigated(); // Apply document content: targetDocument.GotDocumentContent(package.responseText, package.statusCode, true); } } }; // Send now! package.send(); } }
/// <summary>Gets a bundle using the given bundle URI.</summary> private void LoadBundle(Location uri, BundleReadyEvent bre) { // Main thread only. (Even things like bund!=null can fail). Callback.MainThread(delegate(){ // The underlying uri is uri.Path. string path = uri.Path; // Loading or loaded? AssetBundle bund = Bundles.Get(path); if (bund != null) { bre(bund); return; } DataPackage package; if (Bundles.Loading.TryGetValue(path, out package)) { // Loading - just add a listener (always runs after the bundle loading callback): package.addEventListener("onload", delegate(UIEvent e){ // Callback: bre(Bundles.Get(path)); }); } else { // Make a request: package = new DataPackage(path, null); package.addEventListener("onload", delegate(UIEvent e){ // 5.4.1 onwards #if !UNITY_5_4_0 && UNITY_5_4_OR_NEWER AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(package.responseBytes); #else AssetBundleCreateRequest request = AssetBundle.CreateFromMemory(package.responseBytes); #endif // Get the enumerator: IEnumerator enumerator = Loader(request); // Add updater: OnUpdateCallback cb = null; cb = OnUpdate.Add(delegate(){ // Move enumerator: enumerator.MoveNext(); // Request done? if (request.isDone) { // Great! Stop: cb.Stop(); // Set now: AssetBundle bundle = request.assetBundle; Bundles.Add(path, bundle); // Callback: bre(bundle); } }); }); // Send now: package.send(); } }); }