Use Unity script custom resolution to cut a high-definition screenshot from your camera

Source: Internet
Author: User
Recent projects need to be in the game to cut a high-definition, research and write a script, easy to use later.
The script can customize the resolution and use the camera to intercept HD. You can use code dynamics or in edit mode.
Note that the aspect ratio is correct and that the aspect ratio is incorrect may cause problems.





Effect:












Script:
CameraCapture.cs


using UnityEngine;
using System.IO;

/// <summary>
/// camera
/// <para> ZhangYu 2018-07-06 </ para>
/// </ summary>
public class CameraCapture: MonoBehaviour {

    // size
    public enum CaptureSize {
        CameraSize,
        ScreenResolution,
        FixedSize
    }

    // target camera
    public Camera targetCamera;
    // size
    public CaptureSize captureSize = CaptureSize.CameraSize;
    // pixel size
    public Vector2 pixelSize;
    // save route
    public string savePath = "StreamingAssets /";
    // file name
    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> save </ summary>
    /// <param name = "camera"> target 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> camera </ summary>
    /// <param name = "camera"> target camera </ param>
    public static Texture2D capture (Camera camera) {
        return capture (camera, Screen.width, Screen.height);
    }

    /// <summary> camera </ summary>
    /// <param name = "camera"> target camera </ param>
    /// <param name = "width"> width </ param>
    /// <param name = "height"> 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> save stickers </ summary>
    /// <param name = "path"> save 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 ("Saved to:" + path);
        #endif
    }

}


Script Editor:



CameraCaptureEditor.cs


using UnityEditor;
using UnityEngine;

/// <summary>
/// Camera editor
/// <para> ZhangYu 2018-07-06 </ para>
/// </ summary>
[CanEditMultipleObjects]
[CustomEditor (typeof (CameraCapture))]
public class CameraCaptureEditor: Editor {

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

        // Redraw the GUI
        EditorGUI.BeginChangeCheck ();
        drawProperty ("targetCamera", "Target Camera");
        string [] options = new string [] {"Camera size", "Screen size", "Fixed size"};
        selected = EditorGUILayout.Popup ("Size", selected, options, GUILayout.ExpandWidth (true));
        script.captureSize = (CameraCapture.CaptureSize) selected;
        if (script.captureSize == CameraCapture.CaptureSize.FixedSize) {
            drawProperty ("pixelSize", "Pixel Size");
            EditorGUILayout.HelpBox ("Please keep the correct aspect ratio! \ NOtherwise there may be errors in the area.", MessageType.Info);
        }
        drawProperty ("savePath", "Save Path");
        drawProperty ("fileName", "file name");

        // save button
        bool isPress = GUILayout.Button ("Save", 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);
    }

} 


Related articles:



Vuforia Unity Camera Image Access



Using DIRECT2D in Unity



Related videos:



PHP Image Upload Tutorial


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.