【问题标题】:Saving a screen capture to server from web player从网络播放器将屏幕截图保存到服务器
【发布时间】:2015-07-12 14:39:00
【问题描述】:

我正在构建一个基于 Web 的应用程序,它会截取游戏区域的屏幕截图,然后将其发布到 Web 服务器,以便在图片库中调用以供其他人查看。目前,在编辑器中运行时,我可以截取屏幕截图并将其保存在本地,但一旦部署就无法使用。我不知道如何截取该屏幕截图并将其保存到纹理(而不是磁盘)然后上传到我的服务器。我该怎么做呢?我是新手,尤其是渲染纹理功能的新手。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: c# unity3d textures encode


    【解决方案1】:

    我在这里的一个论坛上找到了这个片段。但我自己没有在 WebPlayer 上测试过。

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 2012-11-17
      • 2013-05-02
      • 1970-01-01
      相关资源
      最近更新 更多