Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
쿼터니언(quaternions) 으로 보간하기
Android 플랫폼에는 푸시 알림, 분석, 인증, 광고 등과 같은 광범위하고 다양한 기능을 갖춘 풍부한 타사 라이브러리 생태계와 수많은 API가 있습니다.
이는 Godot 코어 자체에서는 의미가 없으므로 Godot는 오랫동안 :ref:`Android 플러그인 시스템 <doc_android_plugin>`을 제공했습니다. :ref:`Android 플러그인 시스템 <doc_android_plugin>`을 사용하면 개발자는 Java 또는 Kotlin 코드를 사용하여 Godot Android 플러그인을 생성할 수 있습니다. 이는 GDScript, C# 또는 GDExtension의 Godot 프로젝트에 있는 Android API 또는 타사 라이브러리에 액세스하고 사용할 수 있는 인터페이스를 제공합니다.
class MyAndroidSingleton(godot: Godot?) : GodotPlugin(godot) {
@UsedByGodot
fun doSomething(value: String) {
// ...
}
}
그러나 Android 플러그인을 작성하려면 대부분의 Godot 개발자에게는 없는 Java 또는 Kotlin 코드에 대한 지식이 필요합니다. 따라서 개발자가 인터페이스할 수 있는 Godot 플러그인이 없는 Android API와 타사 라이브러리가 많이 있습니다. 사실, 이것이 개발자들이 다른 게임 엔진에서 Godot로 전환할 수 없는 주요 이유 중 하나입니다.
이 문제를 해결하기 위해 우리는 개발자가 Android API 및 타사 라이브러리에 액세스하는 프로세스를 단순화하기 위해 **Godot 4.4**에 몇 가지 도구를 도입했습니다.
JavaClassWrapper(Godot 싱글톤)
JavaClassWrapper is a Godot singleton which allows
creating instances of Java / Kotlin classes, implementing Java / Kotlin interfaces, and calling methods on them using only GDScript, C# or GDExtension.
var LocalDateTime = JavaClassWrapper.wrap("java.time.LocalDateTime")
var DateTimeFormatter = JavaClassWrapper.wrap("java.time.format.DateTimeFormatter")
var datetime = LocalDateTime.now()
var formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss")
print(datetime.format(formatter))
위의 코드 조각에서 JavaClassWrapper``는 GDScript에서 Java ``LocalDateTime 및 DateTimeFormatter 클래스에 액세스하는 데 사용됩니다. ``JavaClassWrapper``를 통해 마치 GDScript 메소드인 것처럼 GDScript에서 직접 Java 클래스 메소드를 호출할 수 있습니다.
class PrintProxy:
func println(content: String) -> void:
print(content)
var print_proxy = PrintProxy.new()
var printer_object = JavaClassWrapper.create_proxy(print_proxy, ["android.util.Printer"])
printer_object.println("Hello Godot World!")
In the code snippet above, JavaClassWrapper is used to implement the Java android.util.Printer interface from GDScript using a Object as the implementation.
The instantiated proxy can then be passed to Java methods that accept a android.util.Printer parameter.
Check out the JavaClassWrapper documentation to learn more about its API.
Android
JavaClassWrapper``는 훌륭하지만 Android에서 많은 작업을 수행하려면 다양한 Android 수명 주기/런타임 개체에 액세스해야 합니다. ``AndroidRuntime 플러그인은 이 작업을 수행할 수 있게 해주는 `내장 Godot Android 플러그인 <https://javadoc.io/doc/org.godotengine/godot/latest/org/godotengine/godot/plugin/AndroidRuntimePlugin.html>`_입니다.
JavaClassWrapper 및 AndroidRuntime 플러그인을 결합하면 개발자는 GDScript에서 전환하거나 Godot 자체 이외의 도구를 사용하지 않고도 Android API에 액세스하고 사용할 수 있습니다. Android 개발에 Godot을 채택한 것은 엄청난 일입니다:
간단한 작업을 수행해야 하거나 타사 라이브러리의 일부만 사용해야 하는 경우 플러그인을 만들 필요가 없습니다.
이를 통해 개발자는 Android 기능을 신속하게 통합할 수 있습니다.
개발자는 GDScript 및 ``JavaClassWrapper``(Java 또는 Kotlin 필요 없음)만 사용하여 Godot 애드온을 만들 수 있습니다.
참고
gradle``를 사용하여 내보내기의 경우 Godot는 프로젝트 ``addons 디렉토리에서 찾은 .jar 또는 .aar 파일을 자동으로 포함합니다. 따라서 타사 라이브러리를 사용하려면 .jar 또는 .aar 파일을 addons 디렉터리에 놓고 ``JavaClassWrapper``를 사용하여 GDScript에서 직접 메서드를 호출하면 됩니다.
예: Android 토스트 표시
# Retrieve the AndroidRuntime singleton.
var android_runtime = Engine.get_singleton("AndroidRuntime")
if android_runtime:
# Retrieve the Android Activity instance.
var activity = android_runtime.getActivity()
# Create a Godot Callable to wrap the toast display logic.
var toast_callable = func():
# Use JavaClassWrapper to retrieve the android.widget.Toast class, then make and show a toast using the class APIs.
var ToastClass = JavaClassWrapper.wrap("android.widget.Toast")
ToastClass.makeText(activity, "This is a test", ToastClass.LENGTH_LONG).show()
# Wrap the Callable in a Java Runnable and run it on the Android UI thread to show the toast.
activity.runOnUiThread(android_runtime.createRunnableFromGodotCallable(toast_callable))
예: 기기 진동
# Retrieve the AndroidRuntime singleton.
var android_runtime = Engine.get_singleton("AndroidRuntime")
if android_runtime:
# Retrieve the Android Vibrator system service and check if the device supports it.
var vibrator_service = android_runtime.getApplicationContext().getSystemService("vibrator")
if vibrator_service and vibrator_service.hasVibrator():
# Configure and run a VibrationEffect.
var VibrationEffect = JavaClassWrapper.wrap("android.os.VibrationEffect")
var effect = VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE)
vibrator_service.vibrate(effect)
예: 내부 클래스에 액세스
Java 내부 클래스는 $ 기호를 사용하여 액세스할 수 있습니다.
# Accessing 'VERSION' class, which is an inner class from the 'android.os.Build' class.
var version = JavaClassWrapper.wrap("android.os.Build$VERSION")
var sdk_int = version.SDK_INT
if sdk_int == 30:
# Do something specific on android 11 devices.
else:
# All other devices
예시: 생성자 호출하기
생성자는 클래스와 동일한 이름을 가진 메서드를 호출하여 호출됩니다.
이 예에서는 텍스트를 보내려는 인텐트를 생성합니다.
# Retrieve the AndroidRuntime singleton.
var android_runtime = Engine.get_singleton("AndroidRuntime")
if android_runtime:
var Intent = JavaClassWrapper.wrap("android.content.Intent")
var activity = android_runtime.getActivity()
var intent = Intent.Intent() # Call the constructor.
intent.setAction(Intent.ACTION_SEND)
intent.putExtra(Intent.EXTRA_TEXT, "This is a test message.")
intent.setType("text/plain")
activity.startActivity(intent)
예: Android 갤러리에 이미지 저장
# Retrieve the AndroidRuntime singleton.
var android_runtime = Engine.get_singleton("AndroidRuntime")
if android_runtime:
var Intent = JavaClassWrapper.wrap("android.content.Intent")
var activity = android_runtime.getActivity()
var intent = Intent.Intent()
# Create the File and Uri.
var Uri = JavaClassWrapper.wrap("android.net.Uri")
var File = JavaClassWrapper.wrap("java.io.File")
var file = File.File(file_path_to_image_here)
var uri = Uri.fromFile(file)
# Set Action and Data of Intent.
intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
intent.setData(uri)
# Broadcast it.
activity.sendBroadcast(intent)