Unreal Engine UPL(Unreal Plugin Language) : 모바일 네이티브 코드 사용법과 멀티 플랫폼 개발에 좋은 구조
[velog에서 블로그 이전하며 가져온 글입니다]
작성 일자 : 2023년 5월 4일
언리얼 엔진으로 모바일 앱을 개발하다보면 OS에 종속적인 기능을 만들어야 할 때가 있다. 예를 들어 사진첩에서 사진을 가져오는 것이 있다. 안드로이드에서는 `Intent`와 `StartActivity`를 사용해서 다른 어플리케이션을 실행하고 데이터를 주고 받으며, 아래와 같은 코드를 통해 갤러리 앱에서 사진 데이터를 가져올 수 있다.
```Java
갤러리 앱 실행
//Start Gallery Activity
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1/*a random integer id for current Activity*/);
```
```Java
갤러리 앱에서 선택된 사진 데이터 반환
//In your Activity Class
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode != Activity.RESULT_OK) {
return;
}
if(requestCode != 11/*the id from Start Gallery Activity*/) {
return;
}
Bitmap bitmap;
try {
InputStream in = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e) {
return;
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
imageBase64Encoded = Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray());
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
}
```
이 코드는 갤러리에서 사진 하나를 선택한 다음, 선택한 사진을 이미지 뷰에 띄우는 코드이다. IOS도 개발할 때 마찬가지로 Objective-c, Switf로 동일한 코드를 만들 수 있을 것이다.
( IOS 관련 글은 나중에 IOS 개발을 시작하면 작성 예정 )