2015-07-12 111 views
0

我正在構建一個基於網絡的應用程序,它會截取一個播放區域的截圖,然後將其張貼到Web服務器以在圖像庫中調用以供其他人查看。目前,在編輯器中運行時,我可以截取屏幕截圖並將其保存在本地,但部署完成後無法運行。我不知道如何截取屏幕截圖並將其保存到紋理(而不是磁盤)然後上傳到我的服務器。我該怎麼做呢?我是新來的,尤其是渲染紋理功能。有人可以幫我解決這個問題嗎?從網絡播放器保存屏幕截圖到服務器

回答

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

相關問題