利用Unity腳本自訂解析度實現相機截一張高清截圖

php是最好的语言
發布: 2018-07-27 11:22:17
原創
8173 人瀏覽過

最近做專案的時候需要在遊戲裡截一張高畫質截圖,研究了一下寫成腳本,方便以後使用。
腳本可以自訂分辨率,用相機截高清截圖。可以用程式碼動態截圖,也可以在編輯模式下截圖。
注意截圖寬高比要正確,寬高比不正確時可能會出問題。

截圖效果:

利用Unity腳本自訂解析度實現相機截一張高清截圖

利用Unity腳本自訂解析度實現相機截一張高清截圖

利用Unity腳本自訂解析度實現相機截一張高清截圖


#腳本:

CameraCapture.cs

using UnityEngine;
using System.IO;

/// 
/// 相机截图
/// ZhangYu 2018-07-06
/// 
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();
        pixelSize = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);
    }
    #endif

    ///  保存截图 
    /// 目标摄像机
    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));
    }

    ///  相机截图 
    /// 目标相机
    public static Texture2D capture(Camera camera) {
        return capture(camera, Screen.width, Screen.height);
    }

    ///  相机截图 
    /// 目标相机
    /// 宽度
    /// 高度
    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;
    }

    ///  保存贴图 
    /// 保存路径
    /// Texture2D
    public static void saveTexture(string path, Texture2D texture) {
        File.WriteAllBytes(path, texture.EncodeToPNG());
        #if UNITY_EDITOR
        Debug.Log("已保存截图到:" + path);
        #endif
    }

}
登入後複製
腳本編輯器:

CameraCaptureEditor.cs

using UnityEditor;
using UnityEngine;

/// 
/// 相机截图 编辑器
/// ZhangYu 2018-07-06
/// 
[CanEditMultipleObjects]
[CustomEditor(typeof(CameraCapture))]
public class CameraCaptureEditor : Editor {

    public override void OnInspectorGUI() {
        // 属性
        CameraCapture script = (CameraCapture)target;
        int selected = (int)script.captureSize;

        // 重绘GUI
        EditorGUI.BeginChangeCheck();
        drawProperty("targetCamera", "目标像机");
        string[] options = new string[] { "像机尺寸", "屏幕尺寸", "固定尺寸"};
        selected = EditorGUILayout.Popup("截图尺寸", selected, options, GUILayout.ExpandWidth(true));
        script.captureSize = (CameraCapture.CaptureSize)selected;
        if (script.captureSize == CameraCapture.CaptureSize.FixedSize) {
            drawProperty("pixelSize", "像素尺寸");
            EditorGUILayout.HelpBox("请保持正确的宽高比!\n否则截图区域可能出现错误。", MessageType.Info);
        }
        drawProperty("savePath", "保存路径");
        drawProperty("fileName", "文件名称");

        // 保存截图按钮
        bool isPress = GUILayout.Button("保存截图", GUILayout.ExpandWidth(true));
        if (isPress) script.saveCapture();
        if (EditorGUI.EndChangeCheck()) serializedObject.ApplyModifiedProperties();
    }

    private void drawProperty(string property, string label) {
        EditorGUILayout.PropertyField(serializedObject.FindProperty(property), new GUIContent(label), true);
    }

}
登入後複製
相關文章:

###Vuforia Unity Camera Image Access############在Unity中使用Direct2D#########相關影片:########## PHP 圖片上傳教學######

以上是利用Unity腳本自訂解析度實現相機截一張高清截圖的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!