본문 바로가기

Unity

[Unity] 갤러리에서 이미지 불러오기

[유니티] 갤러리에서 이미지 불러와 화면에 표시하는 방법

 

 

1. UnityNativeGallery 유니티 패키지 다운로드 후 설치

https://github.com/yasirkula/UnityNativeGallery

 

GitHub - yasirkula/UnityNativeGallery: A native Unity plugin to interact with Gallery/Photos on Android & iOS (save and/or load

A native Unity plugin to interact with Gallery/Photos on Android & iOS (save and/or load images/videos) - GitHub - yasirkula/UnityNativeGallery: A native Unity plugin to interact with Gallery/P...

github.com

 

 

2. 코드 작성

RawImage rawImage; // 불러온 이미지를 보여줄 RawImage

public void getImage {
	NativeGallery.GetImageFromGallery((image) => 
            {
                FileInfo selectedImage = new FileInfo(image);
            
                if (!string.IsNullOrEmpty(image))
                    StartCoroutine(LoadImage(image));

            });
}

//이미지 로드 코루틴            
IEnumerator LoadImage(string imagePath)
    {
        byte[] imageData = File.ReadAllBytes(imagePath);
        string imageName = Path.GetFileName(imagePath).Split('.')[0];
        string saveImagePath = Application.persistentDataPath + "/Image";

        File.WriteAllBytes(saveImagePath + imageName + ".jpg", imageData);

        var tempImage = File.ReadAllBytes(imagePath);

        Texture2D texture = new Texture2D(1080, 1440);
        texture.LoadImage(tempImage);

        rawImage.texture = texture;
        
        yield return null;
    }

 

간혹 이미지가 회전된 상태로 보일 때가 있다. 그럼 그냥 rawImage 회전을 시켜주자^^

LIST