UnityWebGL screenshot/picture/file call browser download

If there is a requirement, click the button, call the browser to download and save the screenshots, pictures and other files in UnityWelGL through the browser. Baidu didn't find it, so Google checked it and put the solution here.

principle

C # code cannot directly operate the browser. Never mind, the jslib provided by Unity can interact with the browser! It is no different from ordinary js files!

A feasible method: C # calls the imported jslib, passes the serialized image/file data to the method in jslib, and jslib interacts with the browser and triggers the download.

Code (take downloading pictures as an example)

ImageDownloader.jslib

 //ImageDownloader.jslib var ImageDownloaderPlugin = { ImageDownloader: function (str, fn) { console.log("start jslib download"); var msg = Pointer_stringify(str); var fname = Pointer_stringify(fn); var contentType = 'image/jpeg'; function fixBinary(bin) { var length = bin.length; var buf = new ArrayBuffer(length); var arr = new Uint8Array(buf); for (var i = 0; i < length; i++) { arr[i] = bin.charCodeAt(i); } return buf; } //Atob decodes base64 encoded strings var binary = fixBinary(atob(msg)); var data = new Blob([binary], { type: contentType }); //Create an html dom to trigger the blob download var link = document.createElement('a'); link.download = fname; link.innerHTML = 'DownloadFile'; link.setAttribute('id', 'ImageDownloaderLink'); link.href = window. URL.createObjectURL(data); link.onclick = function () { var child = document.getElementById('ImageDownloaderLink'); child.parentNode.removeChild(child); }; link.style.display = 'none'; document.body.appendChild(link); link.click(); window. URL.revokeObjectURL(link.href); } }; //Incorporated into Unity, officially written. mergeInto(LibraryManager.library, ImageDownloaderPlugin)

Throw the jslib into the Plugins directory, so that it will be introduced into Unity as a plug-in.

C # code

Importing plug-ins

 [DllImport("__Internal")] private static extern void ImageDownloader(string str, string fn); public void DownloadImage(byte[] imageData, string imageFileName = "newpic") { if (imageData != null) { Debug. Log("Downloading..." + imageFileName); ImageDownloader(System.Convert.ToBase64String(imageData), imageFileName); } }

Call method for testing

 //TestJPEGDownload.cs using System; using System. Collections; using System. Collections. Generic; using UnityEngine; public class TestJPEGDownload : MonoBehaviour { public Sprite sprite; public UnityEngine. UI. Button button; private void Start() { button.onClick. AddListener(this.onButtonClick); } private void onButtonClick() { byte[] photoByte = getImageSprite();// Get the byte stream of the jpeg image if (photoByte != null) { DownloadImage(photoByte, sprite.name + ".jpg"); }else{ Debug. LogError; } } private byte[] getImageSprite() { if (sprite) { return sprite.texture. EncodeToJPG(); } return null; } }

The above is the method of calling the browser to download images in UnityWebGL. As for downloading other types of files, the methods are much the same.

Use jslib as a common js file and interact with the browser in the same way as common js.

Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/5355.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*

Comment area

  1. littleplus 11-29 16:14 reply

    Only the rabbit's blog is still alive (). My blog is alive, but dead(

  2. hare 12-02 21:17 reply

    This article only provides a solution, which has successfully verified the solution of transferring data from within Unity to the browser.
    As for multiple different formats or files, you can do your own processing, such as compression, mixing, and download queue, which is irrelevant to the content of this article.