您的位置:首页 > 移动开发 > Unity3D

unity3d 截屏

2015-01-26 10:15 225 查看

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");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: