亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

利用Unity腳本自定義分辨率如何實現相機截一張高清截圖

發布時間:2020-10-23 16:49:48 來源:億速云 閱讀:1083 作者:小新 欄目:編程語言

這篇文章主要介紹利用Unity腳本自定義分辨率如何實現相機截一張高清截圖,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!


腳本可以自定義分辨率,用相機截高清截圖。可以用代碼動態截圖,也可以在編輯模式下截圖。
注意截圖寬高比要正確,寬高比不正確時可能會出問題。

截圖效果:

利用Unity腳本自定義分辨率如何實現相機截一張高清截圖

利用Unity腳本自定義分辨率如何實現相機截一張高清截圖

利用Unity腳本自定義分辨率如何實現相機截一張高清截圖

腳本:
CameraCapture.cs

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
    }

}

腳本編輯器:

CameraCaptureEditor.cs

using UnityEditor;
using UnityEngine;

/// <summary>
/// 相機截圖 編輯器
/// <para>ZhangYu 2018-07-06</para>
/// </summary>
[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);
    }

}

以上是利用Unity腳本自定義分辨率如何實現相機截一張高清截圖的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

南通市| 泰宁县| 商河县| 峨山| 岳普湖县| 焦作市| 鄂托克前旗| 乐陵市| 蛟河市| 会东县| 宁河县| 随州市| 新和县| 高邑县| 于都县| 远安县| 黄平县| 卓尼县| 怀安县| 武强县| 汉沽区| 牡丹江市| 广德县| 大悟县| 余江县| 定远县| 东辽县| 镇巴县| 青海省| 隆安县| 静安区| 曲阜市| 保定市| 囊谦县| 呈贡县| 安宁市| 商水县| 孟村| 松滋市| 五大连池市| 建湖县|