Notice
Recent Posts
Archives
Today
Total
«   2024/06   »
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] 스크린샷 및 갤러리에 저장하기 본문

Unity

[Unity] 스크린샷 및 갤러리에 저장하기

devchop 2024. 5. 8. 11:20

 

Unity에서 제공하는 Screen Capture를 이용해 스크린샷을 찍고, Native Gallery 를 이용하여 갤러리에 저장한다.

 

순서는 다음과 같다.

1. 프레임 준비를 위해 Coroutine을 이용해 waitforEndofFrame()을 호출

2. 파일이름을 지정

3. 찰칵

4. 갤러리 저장을 위해 퍼미션 체크

5. 갤러리저장

 

 

1. 프레임 준비를 위해 Coroutine을 이용해 waitforEndofFrame()을 호출

    public void CaptureScreen()
    {
        StartCoroutine(MakeScreenShot());

    }

    private IEnumerator MakeScreenShot()
    {

        yield return new WaitForEndOfFrame();

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

#if UNITY_EDITOR
#elif UNITY_IPHONE || UNITY_ANDROID
        CaptureScreenForMobile(fileName);
#else
        CaptureScreenForPC(fileName);
#endif
    }

 

 

 

여기서 모바일과 PC 함수를 다르게 만들었는데, 저장경로 때문이다. PC 버전은 간단하게 Downloads 폴더에 저장, 모바일은 앨범명을 지정하고 그 안에 저장해야한다.

private void CaptureScreenForPC(string fileName)
    {
        ScreenCapture.CaptureScreenshot("~/Downloads/" + fileName);
    }

 

private void CaptureScreenForMobile(string fileName)
    {
        Texture2D texture = ScreenCapture.CaptureScreenshotAsTexture();
        string albumName = "BAMBOO";

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

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

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

    }

 

 

힐링방치 게임에서는 먼저 텍스처를 유저에게 보여주고, <저장하기>를 눌렀을 때 저장이 실행되므로 , 위 함수를 이용해 응용이 가능하당.

더보기
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class NativeCameraManager : MonoBehaviour
{
    public Image captureImage;
    public Texture2D captureTexture;

    int CaptureCounter = 0;


    public void CaptureScreen()
    {
        StartCoroutine(MakeScreenShot());

    }

    private IEnumerator MakeScreenShot()
    {

        yield return new WaitForEndOfFrame();

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

#if UNITY_EDITOR
#elif UNITY_IPHONE || UNITY_ANDROID
        CaptureScreenForMobile(fileName);
#else
        CaptureScreenForPC(fileName);
#endif
    }

    private void CaptureScreenForPC(string fileName)
    {
        ScreenCapture.CaptureScreenshot("~/Downloads/" + fileName);
    }

    private void CaptureScreenForMobile(string fileName)
    {
        Texture2D texture = ScreenCapture.CaptureScreenshotAsTexture();
        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);

    }


}