๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

TIL (Today I Learned)/Unity

[Unity] iOS ์•จ๋ฒ”์— ์ด๋ฏธ์ง€ ์ €์žฅํ•˜๊ธฐ

728x90

[unity] iOS ์•จ๋ฒ”์— ์ด๋ฏธ์ง€ ์ €์žฅํ•˜๊ธฐ

1. Asset Store

Native Gallery for Android & iOS import ํ•˜๊ธฐ

์Šคํฌ๋ฆฐ์ƒท 2021-08-28 ์˜คํ›„ 5 50 36
์Šคํฌ๋ฆฐ์ƒท 2021-08-28 ์˜คํ›„ 5 51 04
์Šคํฌ๋ฆฐ์ƒท 2021-08-28 ์˜คํ›„ 5 50 50

์ฐธ์กฐ UnityNativeGallery

2. ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ

using System.IO;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class CameraCapture : MonoBehaviour
{
    [SerializeField]
    private Button TakePhotoButton;
    public int fileCounter;

    private Camera Camera
    {
        get
        {
            if (!_camera)
            {
                // _camera = Camera.main;
                _camera = Camera.main;
            }
            return _camera;
        }
    }

    [SerializeField]
    private Camera _camera;
    void Awake()
    {
        TakePhotoButton.onClick.AddListener(TakePhoto);
    }

    private void TakePhoto()
    {
        Debug.Log("TakePhoto()" + fileCounter);
        Capture();
    }

    public void Capture()
    {
        RenderTexture activeRenderTexture = RenderTexture.active;
        RenderTexture.active = Camera.targetTexture;

        Camera.Render();

        Texture2D image = new Texture2D(Camera.targetTexture.width, Camera.targetTexture.height);
        image.ReadPixels(new Rect(0, 0, Camera.targetTexture.width, Camera.targetTexture.height), 0, 0);
        image.Apply();
        RenderTexture.active = activeRenderTexture;

        byte[] bytes = image.EncodeToPNG();
        Destroy(image);

        // Mac path : 
        // cf) https://docs.unity3d.com/ScriptReference/Application-dataPath.html?_ga=2.85471643.307249663.1629898360-1836162968.1629898360&_gl=1*1kwusal*_ga*MTgzNjE2Mjk2OC4xNjI5ODk4MzYw*_ga_1S78EFL1W5*MTYyOTkwMDcyMi4yLjAuMTYyOTkwMDcyMi42MA..
        // File.WriteAllBytes(Application.dataPath + "/Image/" + fileCounter + ".png", bytes);

        // cf2) https://devparklibrary.tistory.com/32
        // Debug.Log(Application.persistentDataPath);
        // File.WriteAllBytes(Application.persistentDataPath +"/Image" +fileCounter + ".png", bytes);

        // iOS Mobile
        string name = "AR Camera" + System.DateTime.Now.ToString("yyyy-mm-dd_HH-mm-ss") +fileCounter + ".png";
        NativeGallery.SaveImageToGallery(bytes, "AR Camera Assistant picutres", name);
        fileCounter++;
    }
}

NativeGallery.SaveImageToGallery(bytes, "AR Camera Assistant picutres", name)๋กœ ios14 ์ด์ƒ์˜ ๋ฒ„์ „์—์„œ ์•จ๋ฒ”์— ์ ‘๊ทผํ•ด์„œ ์ด๋ฏธ์ง€๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

'TIL (Today I Learned) > Unity' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Unity, MacOS] LeanTween  (0) 2021.09.17
[Unity]iOS14 ์•จ๋ฒ”์— ์‚ฌ์ง„ ์ €์žฅ  (0) 2021.08.29
[Unity] iOS build path issue  (0) 2021.08.27