Android godot 交互数据监听

在Android模板中定义插件

java 复制代码
package com.godot.game;

import android.util.Log;

import androidx.annotation.NonNull;

import org.godotengine.godot.plugin.GodotPlugin;
import org.godotengine.godot.Godot;
import org.godotengine.godot.plugin.SignalInfo;
import org.godotengine.godot.plugin.UsedByGodot;

import java.util.Collections;
import java.util.Set;


public class AndroidDataListenerPlugin extends GodotPlugin {
    public static final String TAG = "AndroidDataListenerPlugin";

    private final Godot mGodot;
    private int count = 0;

    public AndroidDataListenerPlugin(Godot godot) {
        super(godot);
        this.mGodot = godot;
    }

    @Override
    public String getPluginName() {
        return "AndroidDataListener";
    }

    @NonNull
    @Override
    public Set<SignalInfo> getPluginSignals() {
        return Collections.singleton(new SignalInfo("android_data", String.class));
    }


    @UsedByGodot
    public void startListening(String type) {
        new Thread(() -> {
            try {
                while (true) {
                    Thread.sleep(5000);
                    mGodot.runOnRenderThread(() -> {
                        emitSignal("android_data", (count++) + "收到你的参数,我确认了数据" + type);
                    });
                }
            } catch (InterruptedException e) {
                Log.e(TAG, "startListening: e=====" + e.getMessage());
            }
        }).start();
    }
}

注册插件

godot 使用Android方法并监听回调

python 复制代码
extends Label

@onready var android_plugin: Object = null

func _ready():
	text = "1===="
	if OS.get_name() == "Android":
		android_plugin = Engine.get_singleton("AndroidDataListener")
		text = "2===="
		if android_plugin:
			text = "3===="
			android_plugin.connect("android_data",Callable(self,"_on_android_data"))
			text = "4===="
			android_plugin.startListening("666")
			text = "5===="

func _on_android_data(percent: String):
	text = "change===="+percent

注意 :

1、插件名称:

python 复制代码
android_plugin = Engine.get_singleton("AndroidDataListener")   

来源于Android部分:

java 复制代码
    @Override
    public String getPluginName() {
        return "AndroidDataListener";
    }

2、监听方法 android_data:

python 复制代码
android_plugin.connect("android_data",Callable(self,"_on_android_data"))

来源于Android部分的定义 :

java 复制代码
 @NonNull
 @Override
 public Set<SignalInfo> getPluginSignals() {
     return Collections.singleton(new SignalInfo("android_data", String.class));
 }

和Android部分的发送 :

java 复制代码
emitSignal("android_data", (count++) + "收到你的参数,我确认了数据" + type);
相关推荐
一只大鸽子2 小时前
游戏开发笔记7-Godot RPG游戏基础-场景切换、全局脚本
笔记·游戏·godot
booboosui5 小时前
编号没变,消息还是来了:Mac 端通知读取漏第二条的原因
android
雨白21 小时前
单例设计模式:从 Java/Kotlin 实现到 Android 实战
android
hai_android1 天前
一行 asLiveData,背后是一座生命周期的桥
android·java·kotlin
阿杰m1 天前
Android源码第二十三章:Telephony、RIL 与 IMS——来电怎么到应用?
android
booboosui1 天前
没人认领 .md:安卓把报告交给了音乐播放器打开
android
hcgeng1 天前
原生与 H5 如何交互
交互·h5
用户60709759149061 天前
AOSP 14 OTA 升级指导(四):update_engine 内部机制——Action 流水线与写盘
android
恋猫de小郭1 天前
Jetpack Compose 1.13 发布,憋了这么久终有交代了
android·前端·flutter
JMchen1231 天前
【Android 性能优化实战 60 讲】12 异步 Inflate 实战与痛击:源码拆解原生瓶颈,手写增强版布局预加载框架
android·性能优化·实战·启动优化·异步inflate·x2c