2015-07-20 118 views
0

因此,我只是想快速入門,瞭解如何從基於瀏覽器的應用程序上傳屏幕截圖到網絡服務器。由於我無法在本地保存文件然後上傳,我是否需要將其存儲在紋理變量中?我對這個基礎知識有點困惑,但我只想指出正確的方向。我使用字符串變量在本地研究了在線地址的所有內容,但這對於基於瀏覽器的應用程序無效,對嗎?只是尋找一些關於如何開始爲此建立POC的指導。謝謝您的幫助。存儲屏幕截圖並將其從基於瀏覽器的應用程序上傳到網絡服務器

我所知道的: 我可以採取的截圖(但現在我只知道如何將它保存到本地) 我可以上傳一個文件(但只能從本地路徑)

大問題: 我如何才能將屏幕截圖保存在內存中?不知道這是否是一個正確的問題,但我希望有人知道我在想什麼。

最終我想要做的就是截圖,然後直接保存到MySQL服務器。

回答

0

Texture2D.EncodeToPNG的Unity幫助頁面有一個捕獲和上傳屏幕截圖的完整示例。

http://docs.unity3d.com/ScriptReference/Texture2D.EncodeToPNG.html

// Saves screenshot as PNG file. 
using UnityEngine; 
using System.Collections; 
using System.IO; 

public class PNGUploader : MonoBehaviour { 
    // Take a shot immediately 
    IEnumerator Start() { 
     yield return UploadPNG(); 
    } 

    IEnumerator UploadPNG() { 
     // We should only read the screen buffer after rendering is complete 
     yield return new WaitForEndOfFrame(); 

     // Create a texture the size of the screen, RGB24 format 
     int width = Screen.width; 
     int height = Screen.height; 
     Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false); 

     // Read screen contents into the texture 
     tex.ReadPixels(new Rect(0, 0, width, height), 0, 0); 
     tex.Apply(); 

     // Encode texture into PNG 
     byte[] bytes = tex.EncodeToPNG(); 
     Object.Destroy(tex); 

     // For testing purposes, also write to a file in the project folder 
     // File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes); 


     // Create a Web Form 
     WWWForm form = new WWWForm(); 
     form.AddField("frameCount", Time.frameCount.ToString()); 
     form.AddBinaryData("fileUpload",bytes); 

     // Upload to a cgi script 
     WWW w = new WWW("http://localhost/cgi-bin/env.cgi?post", form); 
     yield return w; 

     if (w.error != null) { 
      Debug.Log(w.error); 
     } else { 
      Debug.Log("Finished Uploading Screenshot"); 
     } 
    } 

} 
+0

好,冬暖夏涼。我將該代碼放入新的cs文件並將其附加到保存按鈕。我在腳本的頂部看到'IEnumerator Start()'。這是否意味着它應該在應用程序啓動後立即運行?在事情成功或失敗的時刻,我沒有在控制檯中得到任何反饋。我需要做什麼來執行這個腳本? – greyBow

+0

是的,啓動意味着它在啓動時立即運行。要附加到一個按鈕,您需要刪除啓動功能,並將以下內容替換爲: 'void TakeScreenshot(){ StartCoroutine(「UploadPNG」); }' 之後,將按鈕的OnClick處理程序附加到檢查器中的TakeScreenshot。當然,您仍然需要將您的特定上傳邏輯編碼到您自己的URL,因爲它當前正在將數據發送到示例URL。 –

相關問題