unity3d 截屏


    void OnGUI(){
        
        if(GUI.Button(new Rect(10,70,50,50),"ScreenShot")){
                    StartCoroutine(ScreenShot());  //協程調用
        }        
    }
    
    IEnumerator ScreenShot(){
            int width = Screen.width;
            int height = Screen.height;
            yield return new WaitForEndOfFrame(); //去掉協程試試看會發生什么。
            Texture2D tex = new Texture2D(width,height,TextureFormat.RGB24,false);//設置Texture2D
            tex.ReadPixels(new Rect(0,0,width,height),0,0);//獲取Pixels           
            tex.Apply();//應用改變
            byte[] bytes = tex.EncodeToPNG();//轉換為byte[]
       Destroy(tex); Stream flstr
= new FileStream(@"d:\1.png", FileMode.Create);//文件操作 BinaryWriter sw = new BinaryWriter(flstr, Encoding.Unicode); sw.Write(bytes); sw.Close(); flstr.Close(); }

另一種方法:

using UnityEngine; 

using System.Collections;
 
public class example : MonoBehaviour 

{ 

  void OnMouseDown() 

  { 

    Application.CaptureScreenshot("Screenshot.png"); 

  } 

}
 
 
 

function OnGUI(){
    if(GUI.Button(Rect(Screen.width*0.5-50,Screen.height*0.5-50,100,100),"screen")){
        Application.CaptureScreenshot("Screenshot.png");
    }
 }
 
 


這張Screenshot.png圖片被存在了當前工程的子目錄下了。

 

 


免責聲明!

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



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