2017-03-01 297 views
1

我已經在我的應用程序中實現了該代碼,因此用戶可以截屏並保存/共享,當我按相機按鈕時,screeshot功能工作正常,但加載功能不是,我看不到在我的GameObject上截取的截圖。在Unity UI上不顯示屏幕截圖圖像

enter image description here

using UnityEngine; 
using System.Collections; 
using System.IO; 
using UnityEngine.UI; 

public class SnapshotShare : MonoBehaviour 
{ 
    private AndroidUltimatePluginController androidUltimatePluginController; 
    Camera mainCamera; 
    RenderTexture renderTex; 
    Texture2D screenshot; 
    Texture2D LoadScreenshot; 
    int width = Screen.width; // for Taking Picture 
    int height = Screen.height; // for Taking Picture 
    string fileName; 
    string screenShotName = "PictureShare.png"; 

    void Start() 
    { 
     androidUltimatePluginController = AndroidUltimatePluginController.GetInstance(); 

    } 

    public void Snapshot() 
    { 
     StartCoroutine (CaptureScreen()); 
    } 

    public IEnumerator CaptureScreen() 
    { 
     yield return null; // Wait till the last possible moment before screen rendering to hide the UI 

     GameObject.Find ("Canvas").GetComponent<Canvas>().enabled = false; 
     yield return new WaitForEndOfFrame(); // Wait for screen rendering to complete 
     if (Screen.orientation == ScreenOrientation.Portrait || Screen.orientation == ScreenOrientation.PortraitUpsideDown) { 
      mainCamera = Camera.main.GetComponent<Camera>(); // for Taking Picture 
      renderTex = new RenderTexture (width, height, 24); 
      mainCamera.targetTexture = renderTex; 
      RenderTexture.active = renderTex; 
      mainCamera.Render(); 
      screenshot = new Texture2D (width, height, TextureFormat.RGB24, false); 
      screenshot.ReadPixels (new Rect (0, 0, width, height), 0, 0); 
      screenshot.Apply(); //false 
      RenderTexture.active = null; 
      mainCamera.targetTexture = null; 


     } 
     if (Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight) { 
      mainCamera = Camera.main.GetComponent<Camera>(); // for Taking Picture 
      renderTex = new RenderTexture (height, width, 24); 
      mainCamera.targetTexture = renderTex; 
      RenderTexture.active = renderTex; 
      mainCamera.Render(); 
      screenshot = new Texture2D (height, width, TextureFormat.RGB24, false); 
      screenshot.ReadPixels (new Rect (0, 0, height, width), 0, 0); 
      screenshot.Apply(); //false 
      RenderTexture.active = null; 
      mainCamera.targetTexture = null; 

     } 
     // on Win7 - C:/Users/Username/AppData/LocalLow/CompanyName/GameName 
     // on Android - /Data/Data/com.companyname.gamename/Files 
     File.WriteAllBytes (Application.persistentDataPath + "/" + screenShotName, screenshot.EncodeToPNG()); 

     // on Win7 - it's in project files (Asset folder) 
     //File.WriteAllBytes (Application.dataPath + "/" + screenShotName, screenshot.EncodeToPNG()); 
     //File.WriteAllBytes ("picture1.png", screenshot.EncodeToPNG()); 
     //File.WriteAllBytes (Application.dataPath + "/../../picture3.png", screenshot.EncodeToPNG()); 
     //Application.CaptureScreenshot ("picture2.png"); 
     GameObject.Find ("Canvas").GetComponent<Canvas>().enabled = true; // Show UI after we're done 
     LoadImage(); 

    } 

    public void ShareImage() 
    { 
     string path = Application.persistentDataPath + "/" + screenShotName; 
     androidUltimatePluginController.ShareImage("subject","Teste de compartilhamento",path); 

    } 

    public void LoadImage() 
    { 
     string path = Application.persistentDataPath + "/" + screenShotName; 
     byte[] bytes; 
     bytes = System.IO.File.ReadAllBytes(path); 
     LoadScreenshot = new Texture2D(90,90); 
     LoadScreenshot.LoadImage(bytes); 
     GameObject.Find("Picture").GetComponent<Renderer>().material.mainTexture = LoadScreenshot; 
     Debug.Log("LOAD IMAGE"); 
    } 

    public void close() 
    { 
#if UNITY_EDITOR 
     UnityEditor.EditorApplication.isPlaying = false; 
#else 
     Application.Quit(); 
#endif 
    } 
} 

的溶液,作爲岡瑟福克斯說:

public void LoadImage() 
    { 

     string path = Application.persistentDataPath + "/" + screenShotName; 
     byte[] bytes; 
     bytes = System.IO.File.ReadAllBytes(path); 
     LoadScreenshot = new Texture2D(4, 4); 
     LoadScreenshot.LoadImage(bytes); 
     Sprite sprite = Sprite.Create(screenshot, new Rect(0, 0, width, height), new Vector2(0.5f, 0.0f), 1.0f); 

     GameObject.Find("Picture").GetComponent<SpriteRenderer>().sprite = sprite; 

     Debug.Log("LOAD IMAGE"); 

    } 
+0

幾個問題:你得到一個錯誤?你會得到紅色問號作爲紋理還是沒有?你有沒有得到LOAD IMAGE打印?在場景中是否有另一個名爲Picture的對象? – Everts

+0

嗨@Everts,我沒有得到任何錯誤,我可以看到LOAD IMAGE打印,並且我的場景中沒有另一個圖片對象 – AND4011002849

回答

1

的問題是,你在一個SpriteRenderer設置MaterialTexture;你應該設置爲the sprite property。退房this Unity doc on creating a Sprite,然後你可以將這些知識應用於您的情況:

加載你的紋理之後創建一個Sprite對象,然後使用GetComponent<SpriteRenderer>改變sprite屬性。你不應該改變任何Materials

+1

它工作!謝謝! – AND4011002849