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);就不运行了。