利用Unity指令碼自訂解析度實現相機截一張高清截圖

來源:互聯網
上載者:User
最近做項目的時候需要在遊戲裡截一張高清,研究了一下寫成指令碼,方便以後使用。
指令碼可以自訂解析度,用相機截高清。可以用代碼動態,也可以在編輯模式下。
注意寬高比要正確,寬高比不正確時可能會出問題。

效果:

指令碼:
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);    }}

相關文章:

Vuforia Unity Camera Image Access

在Unity中使用Direct2D

相關視頻:

PHP 圖片上傳教程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.