Created
March 28, 2015 01:36
-
-
Save grimmdev/7ecdb0e6beb5ababcbe9 to your computer and use it in GitHub Desktop.
Unity render screen to web page image.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copy the function below and put it into your Web Template and build and launch for Web. | |
// | |
//function WebRender(base64) { | |
// console.log(base64); | |
// var img=document.createElement("img"); | |
// img.alt="Render"; | |
// img.src="data:image/png;base64,"+base64; | |
// document.body.appendChild(img); | |
//} | |
using UnityEngine; | |
using System.Collections; | |
public class RenderToWeb : MonoBehaviour { | |
// You can delete this whole OnGUI function for real use, this is for demo purposes only. | |
private void OnGUI(){ | |
if (GUILayout.Button ("Render")) { | |
StartCoroutine(Render()); | |
} | |
} | |
// This function does all the work | |
public IEnumerator Render() { | |
// First we wait for end of frame | |
yield return new WaitForEndOfFrame(); | |
// Create a texture with the dimensions of the screen | |
Texture2D tex = new Texture2D(Screen.width, Screen.height); | |
// Read all the pixels from the current screen | |
tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); | |
// Apply it to our texture | |
tex.Apply(); | |
// Make an external call and convert to Base64 string, so our web can render. | |
Application.ExternalCall("WebRender", System.Convert.ToBase64String(tex.EncodeToPNG())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment