- Home /
How can encode an image with Unity to be displayed in the website using Javascript and HTML?
I would to send images from Unity-webgl to Javascript to show them in the site without saving the images on the server. My idea is encoding images and then send them to Javascript, whichowould show them using
<img src="data:image/jpeg;base64,xxxxxx">
How can I do it? How can encode an image with Unity to be displayed in the website using Javascript and HTML?
I replied with a working answer, just waiting for a moderator to approve.
Answer by ijidau · Nov 06, 2015 at 01:45 PM
Assuming you have a Texture2D, you will need to set the asset import type to 'advanced' and enable the read/write checkbox.
using UnityEngine;
using System;
using System.Collections;
using System.Text;
public class TextureToBase64PNG : MonoBehaviour {
void Start () {
Renderer renderer = gameObject.GetComponent<Renderer>();
Texture2D texture = (Texture2D)renderer.material.mainTexture;
byte[] bytes = texture.EncodeToPNG();
string base64encoded = Convert.ToBase64String(bytes);
}
}
The string will then be ready to use in HTML as you mentioned, simply replace the xxxxxx.
Your answer
Follow this Question
Related Questions
Can't connect to Web socket, connection refused Unity WebGL 0 Answers
WebGL builds: decipher jsStackTrace errors with symbol map? 1 Answer
Browser interaction to Unity 0 Answers
Unity WebGL Problem 0 Answers
ReferenceError: Call is not defined 1 Answer