Notice
Recent Posts
Archives
Today
Total
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Recent Comments
관리 메뉴

우당탕탕 개발일지

[Unity] ScreenShot 촬영 및 저장하기 본문

Unity

[Unity] ScreenShot 촬영 및 저장하기

devchop 2024. 8. 11. 11:02

Unity 에서 제공하는 라이브러리인 ScreenCapture를 이용해 스크린샷을 찍고, 이를 저장하는 기능을 만들어보자. ScreenCapture의 공식 문서는 아래에 있다.

https://docs.unity3d.com/ScriptReference/ScreenCapture.CaptureScreenshot.html

 

Unity - Scripting API: ScreenCapture.CaptureScreenshot

If the screenshot exists already, ScreenCapture.CaptureScreenshot overwrites it with a new screenshot. Add .png to the end of filename to save the screenshot as a .png file. On mobile platforms, filename is appended to the persistent data path. Refer to Ap

docs.unity3d.com

 

private IEnumerator MakeScreenShot(Action<Texture2D> handler)
    {
        //close UI
        yield return new WaitForEndOfFrame();

        Texture2D screenShot = ScreenCapture.CaptureScreenshotAsTexture();
        Texture2D newScreenShot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
        newScreenShot.SetPixels(screenShot.GetPixels());
        newScreenShot.Apply();

        //open UI
        handler?.Invoke(newScreenShot);
    }

 

스크린샷을 찍기 직전, 게임UI를 숨기고  Frame만큼의 딜레이를 준 후 스크린샷을 찍는것을 알 수 있다. 인게임만 스크린샷을 찍고싶기때문.

 

var texture = ScreenCapture.CaptureScreenshotAsTexture(); 을 통해 texture를 얻을 수 있다. 그러나 나의 경우 유니티 에러인지 스크린샷이 뿌옇게 나오는 현상이 발생했다. 이를 해결하기 위해 새로운 newScreenShot 을 만들어 뿌연 현상을 제거하였다.

 

Texture 를 Sprite로 변경

Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));

 

갤러리에 저장

PC와 모바일디바이스일대 서로 다르게 저장하는것을 구현하였다.

    public void SaveGallery(Texture2D texture)
    {
        string timestamp = System.DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
        string fileName = "BAMBOO-SCREENSHOT-" + timestamp + ".png";

#if UNITY_EDITOR
        SaveGalleryForPC(fileName, texture);
#elif UNITY_IPHONE || UNITY_ANDROID
        SaveGalleryForMobile(fileName, texture);
#else
        SaveGalleryForPC(fileName,texture);
#endif

    }

    private void SaveGalleryForPC(string fileName, Texture2D texture)
    {
        var png = texture.EncodeToPNG();

        string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads");
        File.WriteAllBytes(path + fileName, png);
    }

    private void SaveGalleryForMobile(string fileName, Texture2D texture)
    {

        string albumName = "BAMBOO";

        NativeGallery.Permission permission = NativeGallery.CheckPermission(NativeGallery.PermissionType.Write, NativeGallery.MediaType.Image);
        if (permission == NativeGallery.Permission.Denied)
        {
            if (NativeGallery.CanOpenSettings())
            {
                NativeGallery.OpenSettings();
            }
        }

        NativeGallery.SaveImageToGallery(texture, albumName, fileName, (success, path) =>
        {
            Debug.Log(success);
            Debug.Log(path);
        });

        // cleanup
        UnityEngine.Object.Destroy(texture);

    }