利用Unity脚本自定义分辨率实现相机截一张高清截图


本文摘自PHP中文网,作者php是最好的语言,侵删。

最近做项目的时候需要在游戏里截一张高清截图,研究了一下写成脚本,方便以后使用。
脚本可以自定义分辨率,用相机截高清截图。可以用代码动态截图,也可以在编辑模式下截图。
注意截图宽高比要正确,宽高比不正确时可能会出问题。

截图效果:

1.png

2.png

3.png

脚本:
CameraCapture.cs

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

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

using UnityEngine;

using System.IO;

 

/// <summary>

/// 相机截图

/// <para>ZhangYu 2018-07-06</para>

/// </summary>

public class CameraCapture : MonoBehaviour {

 

    // 截图尺寸

    public enum CaptureSize {

        CameraSize,

        ScreenResolution,

        FixedSize

    }

 

    // 目标摄像机

    public Camera targetCamera;

    // 截图尺寸

    public CaptureSize captureSize = CaptureSize.CameraSize;

    // 像素尺寸

    public Vector2 pixelSize;

    // 保存路径

    public string savePath = "StreamingAssets/";

    // 文件名称

    public string fileName = "cameraCapture.png";

 

    #if UNITY_EDITOR

    private void Reset() {

        targetCamera = GetComponent<Camera>();

        pixelSize = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);

    }

    #endif

 

    /// <summary> 保存截图 </summary>

    /// <param name="camera">目标摄像机</param>

    public void saveCapture() {

        Vector2 size = pixelSize;

        if (captureSize == CaptureSize.CameraSize) {

            size = new Vector2(targetCamera.pixelWidth, targetCamera.pixelHeight);

        } else if (captureSize == CaptureSize.ScreenResolution) {

            size = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);

        }

        string path = Application.dataPath + "/" + savePath + fileName;

        saveTexture(path, capture(targetCamera, (int)size.x, (int)size.y));

    }

 

    /// <summary> 相机截图 </summary>

    /// <param name="camera">目标相机</param>

    public static Texture2D capture(Camera camera) {

        return capture(camera, Screen.width, Screen.height);

    }

 

    /// <summary> 相机截图 </summary>

    /// <param name="camera">目标相机</param>

    /// <param name="width">宽度</param>

    /// <param name="height">高度</param>

    public static Texture2D capture(Camera camera, int width, int height) {

        RenderTexture rt = new RenderTexture(width, height, 0);

        rt.depth = 24;

        rt.antiAliasing = 8;

        camera.targetTexture = rt;

        camera.RenderDontRestore();

        RenderTexture.active = rt;

        Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false, true);

        Rect rect = new Rect(0, 0, width, height);

        texture.ReadPixels(rect, 0, 0);

        texture.filterMode = FilterMode.Point;

        texture.Apply();

        camera.targetTexture = null;

        RenderTexture.active = null;

        Destroy(rt);

        return texture;

    }

 

    /// <summary> 保存贴图 </summary>

    /// <param name="path">保存路径</param>

    /// <param name="texture">Texture2D</param>

    public static void saveTexture(string path, Texture2D texture) {

        File.WriteAllBytes(path, texture.EncodeToPNG());

        #if UNITY_EDITOR

        Debug.Log("已保存截图到:" + path);

        #endif

    }

 

}

脚本编辑器:

阅读剩余部分

相关阅读 >>

C#实现网络电子白板、课件功能 (在线教学系统)

C#获取本机ip搜集整理7种方法的示例代码分享

详细介绍C#该行已经属于另一个表的解决方法

分享125个基本的C#面试问答

C#如何使用reflect获取dll文件中的类型并调用?

C#实现原图片转缩略图

C#中载入界面的经典实例

C#开发实例-订制屏幕截图工具(九)使用自定义光标和qq截图时的光标(图)

使用C#实现发送自定义的html格式邮件的代码案例

C#类的声明详解及实例

更多相关阅读请进入《C#》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...