unity3d屏幕截图功能

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图片被存在了当前工程的子目录下了。之前在android上面一直不知道路径,后来把所有路径试玩了总算ok了,呵呵~~~在android上截取的图片存在Application.persistentDataPath上面,在pc上存在Application.dataPath。

private var www:WWW;
private var image:Texture;
private var path:String;
function Awake(){
   Application.CaptureScreenshot("Screenshot.png");
}
function Start () {
   if(Application.platform==RuntimePlatform.Android){
      path=Application.persistentDataPath;
   }else if(Application.platform==RuntimePlatform.WindowsPlayer){
      path=Application.dataPath;
   }else if(Application.platform==RuntimePlatform.WindowsEditor){
      path=Application.dataPath;
      path=path.Replace("/Assets",null);
    }  
   www=new WWW("file://"+path+"/Screenshot.png");
   yield www;
   image=www.texture;
}

function OnGUI(){
    GUI.Label(Rect(0,0,400,50),"1:"+Application.persistentDataPath);
    GUI.Label(Rect(0,50,400,50),"2:"+Application.dataPath);
    GUI.Label(Rect(0,100,400,50),"3:"+Application.temporaryCachePath);
    GUI.Label(Rect(0,150,400,50),"4:"+Application.absoluteURL);
    GUI.Label(Rect(0,200,400,50),"5:"+Application.streamingAssetsPath); 
    GUI.DrawTexture(Rect(0,250,300,200),image);
}
在编辑器上各种路径:

    

在pc上各种路径:

      

在web上各种路径:

    

在android上面各种路径:

   






第二种方法:

import System.IO;
var www:WWW;
var image:Texture;
function Start () {

}

function OnGUI() {
    if(GUI.Button(Rect(0,0,100,100),"png")){
        writeFile();
    }
    if(Input.GetKey(KeyCode.Escape)){
       Application.Quit();
    }
    
      www=new WWW("file://"+path);
      image=www.texture;
      GUI.Button(Rect(Screen.width-100,0,100,100),image);
  
}
private var fileName : String = "jietu2";
private var path:String;
function writeFile()
{
    path=Application.dataPath;
    var tex : Texture2D= new Texture2D(Screen.width,Screen.height,TextureFormat.RGB24, false);
    tex.ReadPixels(Rect(0,0,Screen.width,Screen.height),0,0);
    tex.Apply();
    var bytes : byte[]=tex.EncodeToPNG();
    Destroy(tex);
    var thisName : String = fileName+".png";
    if(Application.platform==RuntimePlatform.Android){
        path="/mnt/sdcard/"+thisName;
    }else{
       path=path+thisName;
    }    
    var cache = new FileStream(path, FileMode.Create);
    cache.Write(bytes,0,bytes.Length);
    cache.Close();
}



后来我又试了一下这个方法,结果又不行了,运行到var cache = new FileStream(path, FileMode.Create);就不运行了。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值