首页 > 其他 > 详细

Unity3D里截图并保存在指定目录下

时间:2014-02-25 08:25:27      阅读:283      评论:0      收藏:0      [点我收藏+]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
private void OnScreenCapture ()
    {
        //yield return new WaitForEndOfFrame();
        try {
            int width = Screen.width;
            int height = Screen.height-reduceHigh;
             
            Texture2D tex = new Texture2D (width, height, TextureFormat.RGB24, false);
             
            tex.ReadPixels (new Rect (0, bottomStarH, width, height), 0, 0, true);
             
            byte[] imagebytes = tex.EncodeToPNG ();
             
            tex.Compress (false);
            tex.Apply();
            mScreenShotImgae = tex;
             
            if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
            {
                path = Application.persistentDataPath + screenShotFileName +".png";
                string origin = path;
                 
                string destination = "/mnt/sdcard/moliputao";
                 
                if(!Directory.Exists(destination))
                {
                    Directory.CreateDirectory(destination);
                }
                 
                destination = destination + "/" + screenShotFileName +".png";
                 
                if(System.IO.File.Exists(origin))
                {
                    System.IO.File.Move(origin,destination);
                }
                path = destination;
                 
            }
            else if (Application.platform == RuntimePlatform.WindowsEditor) { 
                path = Application.dataPath;
                path = path.Replace ("/Assets", "/"+screenShotFileName + ".png");
            }
             
            File.WriteAllBytes (path, imagebytes);
            mScreenShotImgae = tex;
             
        } catch (System.Exception e) {
            Debug.Log ("ScreenCaptrueError:" + e);
        }
    }

  只放出核心代码!以后有时间会补全和详细讲解!

因为我是需要截图后出现预览效果,所以并没有使用协程,如果用协程需要加一句代码,等待当前那一帧结束!

Unity3D里截图并保存在指定目录下

原文:http://www.cnblogs.com/vital/p/3564206.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!