Вопрос

Я использую этот код для создания текстуры2d из ранртуднойuура всех камер.

код:

    var w = 256;
    var h = 256;
    var  cameras:Camera[] = Camera.allCameras;

    var renderTexture:RenderTexture = RenderTexture.GetTemporary(Screen.width, Screen.height, 24);
    RenderTexture.active = renderTexture;  

    for (var camera:Camera in cameras)
    {
        if (camera.enabled)
        {
            var fov:float = camera.fov;
            camera.targetTexture = renderTexture;
            camera.Render();
            camera.targetTexture = null;
            camera.fov = fov;
        }
    }

    var result:Texture2D = new Texture2D(w, h, TextureFormat.ARGB32, false);
    result.ReadPixels(Rect(0.0f, 0.0f, Screen.width, Screen.height), 0, 0, false);
    result.Apply();


    Application.ExternalCall("exportImage",System.Convert.ToBase64String(result.EncodeToPNG()));
.

Работает прямо на веб-процессуаре, но при попытке преобразовать эту текстуру2d на Base64 и отправку в браузер (по внешнему экрану ()) результатом является серый полупрозрачный образ.

Любая помощь была бы очень ценится!Спасибо.

Это было полезно?

Решение

Я смог опубликовать скриншот в WebPlayer с:

using UnityEngine;
using System.Collections;

public class ScreenshotTest : MonoBehaviour {

    void Update () {
        if(Input.GetKeyDown(KeyCode.Space))
            StartCoroutine(Run());
    }


    IEnumerator Run() {
        yield return new WaitForEndOfFrame();

        Texture2D tex = new Texture2D(Screen.width, Screen.height);
        tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        tex.Apply();

        // to test the texture
        //  renderer.material.mainTexture = tex;

        // to save it as file, not available in webplayer
        //  System.IO.File.WriteAllBytes(Application.streamingAssetsPath+"/png.png", tex.EncodeToPNG());

        Application.ExternalCall("screenshot", System.Convert.ToBase64String(tex.EncodeToPNG()));
    }

}
.

и на стороне браузера:

function screenshot(base64) {
    console.log(base64);
    var img=document.createElement("img");
    img.alt="screenshot";
    img.src="data:image/png;base64,"+base64;
    document.body.appendChild(img);
}
.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top