首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从web播放器将屏幕截图保存到服务器

从web播放器将屏幕截图保存到服务器
EN

Stack Overflow用户
提问于 2015-07-12 22:39:01
回答 1查看 364关注 0票数 0

目前,当在编辑器中运行时,我可以获取屏幕截图并将其保存在本地,但一旦部署,这将不起作用。我不知道如何将屏幕截图保存到纹理(而不是磁盘),然后上传到我的服务器。我该怎么做呢?我是个新手,尤其是在渲染纹理功能方面。有人能帮我解决这个问题吗?

EN

Stack Overflow用户

发布于 2015-07-12 23:23:05

我在这里的一个论坛上找到了这段代码。但不是我自己在WebPlayer上测试的。

代码语言:javascript
复制
using UnityEngine;
using System.Collections;

public class Main : MonoBehaviour
{
    private string _data = string.Empty;
    public Texture2D bg;

    void OnGUI()
    {
        if (GUI.Button(new Rect(Screen.width*0.5f-32,32,64,32),"Save"))
            StartCoroutine(ScreeAndSave());
    }

    IEnumerator ScreeAndSave()
    {
        yield return new WaitForEndOfFrame();
        var newTexture = ScreenShoot(Camera.main, bg.width, bg.height);
        LerpTexture(bg, ref newTexture);
        _data = System.Convert.ToBase64String(newTexture.EncodeToPNG());
        Application.ExternalEval("document.location.href='data:octet-stream;base64," + _data + "'");
    }

    private static Texture2D ScreenShoot(Camera srcCamera, int width, int height)
    {
        var renderTexture = new RenderTexture(width, height, 0);
        var targetTexture = new Texture2D(width, height, TextureFormat.RGB24, false);
        srcCamera.targetTexture = renderTexture;    
        srcCamera.Render();
        RenderTexture.active = renderTexture;
        targetTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        targetTexture.Apply();
        srcCamera.targetTexture = null;
        RenderTexture.active = null;
        srcCamera.ResetAspect();
        return targetTexture;
    }

    private static void LerpTexture(Texture2D alphaTexture, ref Texture2D texture)
    {
        var bgColors = alphaTexture.GetPixels();
        var tarCols = texture.GetPixels();
        for (var i = 0; i < tarCols.Length; i++)
            tarCols[i] = bgColors[i].a > 0.99f ? bgColors[i] :  Color.Lerp(tarCols[i], bgColors[i], bgColors[i].a);
        texture.SetPixels(tarCols);
        texture.Apply();
    }
}

Reference Link

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31368940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档