यूनिटी कैप्चर स्क्रीनशॉट ट्यूटोरियल

इस पोस्ट में, मैं दिखाऊंगा कि Unity में इन-गेम स्क्रीनशॉट कैसे कैप्चर करें।

  • एक नई स्क्रिप्ट बनाएं, इसे SC_ScreenAPI कहें, फिर इसके अंदर नीचे दिया गया कोड पेस्ट करें:

SC_ScreenAPI.cs

using UnityEngine;

public static class SC_ScreenAPI
{
    /// <summary>
    /// </summary>
    /// <param name="limitSize">If above 0, will make sure that the width and height of the captured image are equal or less than specified.</param>
    /// <returns>Returns a Texture2D.</returns>
    public static Texture2D CaptureScreen(int limitSize = 0)
    {
        //Create a texture the size of the screen, RGB24 format
        int width = Screen.width;
        int height = Screen.height;
        Texture2D screenshot = new Texture2D(width, height, TextureFormat.RGB24, false);
        //Read screen contents into the texture
        screenshot.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        screenshot.Apply();

        if (limitSize > 0)
        {
            screenshot = ScaleTexture(screenshot, limitSize);
        }

        return screenshot;
    }

    static Texture2D ScaleTexture(Texture2D source, int limitSize)
    {
        int width = source.width;
        int height = source.height;
        bool resize = false;

        if (limitSize > 0)
        {
            if (width > limitSize || height > limitSize)
            {
                int newWidth = 0;
                int newHeight = 0;

                float tmpRatio = (width * 1.000f) / (height * 1.000f);
                if (tmpRatio == 1)
                {
                    newWidth = limitSize;
                    newHeight = limitSize;
                }
                else
                {
                    if (tmpRatio > 1)
                    {
                        newWidth = limitSize;
                        newHeight = (int)(limitSize / tmpRatio);
                    }
                    else
                    {
                        newWidth = (int)(limitSize * tmpRatio);
                        newHeight = limitSize;
                    }
                }

                width = newWidth;
                height = newHeight;
                if(width > 0 && height > 0)
                {
                    resize = true;
                }
            }
        }

        if (resize)
        {
            Texture2D result = new Texture2D(width, height, source.format, true);
            Color[] rpixels = result.GetPixels(0);
            float incX = (1.0f / (float)width);
            float incY = (1.0f / (float)height);
            for (int px = 0; px < rpixels.Length; px++)
            {
                rpixels[px] = source.GetPixelBilinear(incX * ((float)px % width), incY * ((float)Mathf.Floor(px / width)));
            }
            result.SetPixels(rpixels, 0);
            result.Apply();
            return result;
        }

        return source;
    }
}

का उपयोग कैसे करें

  • स्क्रीन को SC_ScreenAPI.CaptureScreen(); पर कॉल करके कैप्चर किया जाता है, जो Texture2D लौटाता है
  • वैकल्पिक रूप से आप एक सीमा आकार मान प्रदान कर सकते हैं जो छवि को छोटा कर देगा (यदि इसकी चौड़ाई या ऊंचाई सीमा मान से बड़ी है)।

नोट: SC_ScreenAPI.CaptureScreen() को कॉल करना; त्रुटियों से बचने के लिए फ़्रेम के अंत के बाद इसे करने की आवश्यकता है, इसलिए इसे IEnumerator के अंदर उपयोग करें

SC_ScreenCaptureTest.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SC_ScreenCaptureTest : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        //Press Q to capture screenshot
        if (Input.GetKeyDown(KeyCode.Q))
        {
            StartCoroutine(CaptureScreen());
        }
    }

    IEnumerator CaptureScreen()
    {
        yield return new WaitForEndOfFrame();

        //Here is a captured screenshot
        Texture2D screenshot = SC_ScreenAPI.CaptureScreen();

        //And here is how you get a byte array of the screenshot, you can use it to save the image locally, upload it to server etc.
        byte[] bytes = screenshot.EncodeToPNG();
    }
}
सुझाए गए लेख
एकता एफपीएस काउंटर
एसेट स्टोर से शीर्ष यूनिटी एसेट्स
यूनिटी में नई एचडीआरपी जल प्रणाली का उपयोग कैसे करें
यूनिटी में एक्सबॉक्स कंट्रोलर का उपयोग कैसे करें
यूनिटी में लाइट स्विच बनाने की स्क्रिप्ट
यूनिटी के लिए रेकास्ट और प्रोजेक्टाइल-आधारित गन शूटिंग स्क्रिप्ट
एकता में गति के लिए जॉयस्टिक नियंत्रक कैसे स्थापित करें