Unity 保存游戲效果圖片,並顯示;



 方案1:截屏保存,再顯示。
核心知識:
1,Application.CaptureScreenshot("wuk.png");Unity會自動截屏保存在 Application.persistentDataPath+"/"+"wuk.png";
2,WWW 網頁;
3,獲取權限;

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />  

例1:

 

public   Material   image;


 

 

void Awake(){  

   Application.CaptureScreenshot("wuk.png");  

}   

void Start () {

 

    StartCoroutine(getCapture());

 

}

 

IEnumerator   getCapture(){

 

    path=Application.persistentDataPath;

 

    Debug.Log("path  "+path);

  www=new WWW("file://"+path+"/wuk.png");  

  yield  return www;  

  image.mainTexture=www.texture;

}

    方案2:保存指定屏幕區域為png,並顯示;

核心知識:

    Texture2D.ReadPixels;System.IO.File.WriteAllBytes;

關鍵方法:

    Texture2D.ReadPixels; Texture2D.Apply();Texture2D.EncodeToPHG;  

例1:

 

string path=Application.persistentDataPath+"/wukuaTurret.jpg";

public   Material   image;

 

 

IEnumerator getTexture2d()  {  

    yield return new WaitForEndOfFrame();  

    Texture2D t = new Texture2D(200180);

   

    t.ReadPixels(new Rect(200320200180), 00false);  

    t.Apply();  

   

    byte[] byt = t.EncodeToPNG();  

 

  System.IO.File.WriteAllBytes(path, byt);  

}  

IEnumerator   getCaptureFf(){

path=Application.persistentDataPath;

Debug.Log("path  "+path);

  www=new WWW("file://"+path+"/Screenshot.png");  

  yield  return www;  

  image.mainTexture=www.texture;

}

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM