zoukankan      html  css  js  c++  java
  • unity3d屏幕截图功能

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

    这张Screenshot.png图片被存在了当前工程的子目录下了。之前在android上面一直不知道路径,后来把所有路径试玩了总算ok了,呵呵~~~在android上截取的图片存在Application.persistentDataPath上面,在pc上存在Application.dataPath。

    1. private var www:WWW;  
    2. private var image:Texture;  
    3. private var path:String;  
    4. function Awake(){  
    5.    Application.CaptureScreenshot("Screenshot.png");  
    6. }  
    7. function Start () {  
    8.    if(Application.platform==RuntimePlatform.Android){  
    9.       path=Application.persistentDataPath;  
    10.    }else if(Application.platform==RuntimePlatform.WindowsPlayer){  
    11.       path=Application.dataPath;  
    12.    }else if(Application.platform==RuntimePlatform.WindowsEditor){  
    13.       path=Application.dataPath;  
    14.       path=path.Replace("/Assets",null);  
    15.     }    
    16.    www=new WWW("file://"+path+"/Screenshot.png");  
    17.    yield www;  
    18.    image=www.texture;  
    19. }  
    20.   
    21. function OnGUI(){  
    22.     GUI.Label(Rect(0,0,400,50),"1:"+Application.persistentDataPath);  
    23.     GUI.Label(Rect(0,50,400,50),"2:"+Application.dataPath);  
    24.     GUI.Label(Rect(0,100,400,50),"3:"+Application.temporaryCachePath);  
    25.     GUI.Label(Rect(0,150,400,50),"4:"+Application.absoluteURL);  
    26.     GUI.Label(Rect(0,200,400,50),"5:"+Application.streamingAssetsPath);   
    27.     GUI.DrawTexture(Rect(0,250,300,200),image);  
    28. }  

    在编辑器上各种路径:

        

    在pc上各种路径:

          

    在web上各种路径:

        

    在android上面各种路径:

       

    第二种方法:

    1. import System.IO;  
    2. var www:WWW;  
    3. var image:Texture;  
    4. function Start () {  
    5.   
    6. }  
    7.   
    8. function OnGUI() {  
    9.     if(GUI.Button(Rect(0,0,100,100),"png")){  
    10.         writeFile();  
    11.     }  
    12.     if(Input.GetKey(KeyCode.Escape)){  
    13.        Application.Quit();  
    14.     }  
    15.       
    16.       www=new WWW("file://"+path);  
    17.       image=www.texture;  
    18.       GUI.Button(Rect(Screen.width-100,0,100,100),image);  
    19.     
    20. }  
    21. private var fileName : String = "jietu2";  
    22. private var path:String;  
    23. function writeFile()  
    24. {  
    25.     path=Application.dataPath;  
    26.     var tex : Texture2D= new Texture2D(Screen.width,Screen.height,TextureFormat.RGB24, false);  
    27.     tex.ReadPixels(Rect(0,0,Screen.width,Screen.height),0,0);  
    28.     tex.Apply();  
    29.     var bytes : byte[]=tex.EncodeToPNG();  
    30.     Destroy(tex);  
    31.     var thisName : String = fileName+".png";  
    32.     if(Application.platform==RuntimePlatform.Android){  
    33.         path="/mnt/sdcard/"+thisName;  
    34.     }else{  
    35.        path=path+thisName;  
    36.     }      
    37.     var cache = new FileStream(path, FileMode.Create);  
    38.     cache.Write(bytes,0,bytes.Length);  
    39.     cache.Close();  
    40. }  

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

  • 相关阅读:
    深度学习模型参数计算
    keras多输出多输出示例(keras教程一)
    keras可视化报错:OSError: `pydot` failed to call GraphViz.Please install GraphViz问题解决
    git版本管理,git tag
    python封装自己的模块,pip install安装到python环境
    如何理解Virtual DOM
    使用 Hbuilder 连接手机调试移动端项目
    js 常用排序
    博客漂浮的小人
    开发者必备Linux命令
  • 原文地址:https://www.cnblogs.com/forlove/p/3795863.html
Copyright © 2011-2022 走看看