Version caution
Note created for Bevy in version 0.16 in mind.
I cannot guarantee that it will work in all versions.
Contents
Update
Simple fix- use updated android activity.
To do so, specify the android_activity dependency in your Cargo.toml file:
[]
= { = "https://github.com/damus-io/android-activity", = "a8948332c7c551303d32eb26a59d0abd676e47a5" }
In app/build.gradle update the android-activity dependency from:
implementation libs.games.activity
to:
implementation "androidx.games:games-activity:4.0.0"
That's it! All of the stuff below is my previous fix that is not required now.
Content
My game didn't received any touch input on Android, even though it was working just fine on desktop and iOS. But I was able to write a plugin that detects touch input in GameActivity and sends it to Bevy.
Using in Bevy
Add plugin:
...
app.add_plugins;
...
Also, it will require adding android depencency and entry binding that will be used by GameActivity.
Android only dependency:
[]
= "0.21"
Function that will be used by java GameActivity:
pub extern "system"
Notice name of the function, it should follow the pattern: Java_ + ID of the application with _ instead of dots + _ + activity class + _ + method name. So, since I have org.mycompany.idlerpg id with MainActivity class and setTouchState method, it should be named Java_org_mycompany_idlerpg_MainActivity_setTouchState.
In the java MainActivity class, you need to add the following code:
Code
In this example there are external dependencies besides Bevy, here is the Cargo.toml file of the plugin:
[]
= "bevy_android_input"
= "0.1.0"
= "2021"
= ["Piotr Siuszko <siuszko@zoho.com>"]
= "MIT OR Apache-2.0"
= ["bevy", "android", "plugin"]
[]
= "1"
= "0.8"
= { = "0.16", = false, = [
"bevy_ui","bevy_picking","bevy_window"]}
= "0.16"
Plugin src/lib.rs
//! Android input handling plugin for Bevy applications.
//!
//! This module provides functionality to bridge Android touch input events
//! with Bevy's input system, handling coordinate transformations and camera
//! viewport adjustments.
use ;
use SegQueue;
use Lazy;
/// Thread-safe queue for storing Android touch input events.
/// This allows the Android native layer to push events that will be
/// processed by the Bevy systems.
static INPUT_QUEUE: = new;
/// Bevy plugin that integrates Android touch input handling.
///
/// This plugin sets up the necessary systems to:
/// - Track camera information for coordinate transformations
/// - Process queued Android touch events
/// - Convert them to Bevy input events
;
/// Raw touch data received from the Android platform.
///
/// This struct represents a single touch event with its basic properties
/// before being converted to Bevy's touch input format.
/// Resource containing camera information needed for input coordinate transformation.
///
/// This resource tracks the camera's viewport offset and scaling factor
/// to properly convert Android screen coordinates to Bevy world coordinates.
pub
/// Entry point for Android native code to send touch input events.
///
/// This function is called from the Android native layer (typically JNI)
/// to queue touch events for processing by the Bevy input system.
///
/// # Arguments
/// * `id` - Unique identifier for the touch point
/// * `x` - X coordinate in screen space
/// * `y` - Y coordinate in screen space
/// * `state` - Touch state (0=Started, 1=Moved, 2=Ended, other=Canceled)
This work is licensed under CC BY-NC-SA 4.0.