|
| 1 | +package com.xraph.gameframework.unity |
| 2 | + |
| 3 | +import android.util.Log |
| 4 | + |
| 5 | +/** |
| 6 | + * Registry for the Flutter bridge controller. |
| 7 | + * |
| 8 | + * This singleton class is accessible from Unity C# via JNI (AndroidJavaClass). |
| 9 | + * It provides a reliable way for Unity to find the active UnityEngineController |
| 10 | + * without relying on Activity methods that may not exist. |
| 11 | + * |
| 12 | + * This is the Android equivalent of iOS's FlutterBridgeRegistry.swift. |
| 13 | + * |
| 14 | + * Usage from Unity C#: |
| 15 | + * ```csharp |
| 16 | + * using (var registry = new AndroidJavaClass("com.xraph.gameframework.unity.FlutterBridgeRegistry")) |
| 17 | + * { |
| 18 | + * var controller = registry.CallStatic<AndroidJavaObject>("getSharedController"); |
| 19 | + * if (controller != null) |
| 20 | + * { |
| 21 | + * controller.Call("onUnityMessage", target, method, data); |
| 22 | + * } |
| 23 | + * } |
| 24 | + * ``` |
| 25 | + */ |
| 26 | +object FlutterBridgeRegistry { |
| 27 | + |
| 28 | + private const val TAG = "FlutterBridgeRegistry" |
| 29 | + |
| 30 | + init { |
| 31 | + Log.d(TAG, "FlutterBridgeRegistry initialized (static init)") |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * The currently active UnityEngineController. |
| 36 | + * Set when a controller registers, cleared when it unregisters. |
| 37 | + */ |
| 38 | + @Volatile |
| 39 | + private var _sharedController: UnityEngineController? = null |
| 40 | + |
| 41 | + /** |
| 42 | + * The UnityPlayer instance (for utility functions). |
| 43 | + */ |
| 44 | + @Volatile |
| 45 | + private var _sharedUnityPlayer: Any? = null |
| 46 | + |
| 47 | + /** |
| 48 | + * Get the shared controller instance. |
| 49 | + * Called from Unity C# via JNI. |
| 50 | + * |
| 51 | + * @return The active UnityEngineController, or null if none registered. |
| 52 | + */ |
| 53 | + @JvmStatic |
| 54 | + fun getSharedController(): UnityEngineController? { |
| 55 | + return _sharedController |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Get the shared UnityPlayer instance. |
| 60 | + * Called from Unity C# via JNI. |
| 61 | + * |
| 62 | + * @return The UnityPlayer instance, or null if not set. |
| 63 | + */ |
| 64 | + @JvmStatic |
| 65 | + fun getSharedUnityPlayer(): Any? { |
| 66 | + return _sharedUnityPlayer |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Check if a controller is registered. |
| 71 | + * Called from Unity C# via JNI. |
| 72 | + * |
| 73 | + * @return true if a controller is registered, false otherwise. |
| 74 | + */ |
| 75 | + @JvmStatic |
| 76 | + fun isReady(): Boolean { |
| 77 | + return _sharedController != null |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Register a controller with the registry. |
| 82 | + * Called by UnityEngineController when it becomes active. |
| 83 | + * |
| 84 | + * @param controller The controller to register. |
| 85 | + */ |
| 86 | + @JvmStatic |
| 87 | + fun register(controller: UnityEngineController) { |
| 88 | + _sharedController = controller |
| 89 | + Log.d(TAG, "Controller registered") |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Register the UnityPlayer instance. |
| 94 | + * Called by UnityEngineController when Unity is initialized. |
| 95 | + * |
| 96 | + * @param unityPlayer The UnityPlayer instance. |
| 97 | + */ |
| 98 | + @JvmStatic |
| 99 | + fun registerUnityPlayer(unityPlayer: Any?) { |
| 100 | + _sharedUnityPlayer = unityPlayer |
| 101 | + if (unityPlayer != null) { |
| 102 | + Log.d(TAG, "UnityPlayer registered") |
| 103 | + } else { |
| 104 | + Log.d(TAG, "UnityPlayer unregistered") |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Unregister the current controller. |
| 110 | + * Called by UnityEngineController when it's destroyed. |
| 111 | + */ |
| 112 | + @JvmStatic |
| 113 | + fun unregisterAll() { |
| 114 | + _sharedController = null |
| 115 | + _sharedUnityPlayer = null |
| 116 | + Log.d(TAG, "All references cleared") |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Send a message from Unity to Flutter. |
| 121 | + * This is the main entry point for Unity→Flutter communication. |
| 122 | + * Called from Unity C# via JNI. |
| 123 | + * |
| 124 | + * @param target The target component name (e.g., "GameFrameworkDemo") |
| 125 | + * @param method The method name (e.g., "onCurrentSpeed") |
| 126 | + * @param data The JSON data string |
| 127 | + * @return true if the message was sent, false if no controller is registered |
| 128 | + */ |
| 129 | + @JvmStatic |
| 130 | + fun sendMessageToFlutter(target: String, method: String, data: String): Boolean { |
| 131 | + Log.d(TAG, ">>> sendMessageToFlutter called from Unity!") |
| 132 | + Log.d(TAG, " Target: $target, Method: $method") |
| 133 | + Log.d(TAG, " Data length: ${data.length}") |
| 134 | + |
| 135 | + val controller = _sharedController |
| 136 | + if (controller == null) { |
| 137 | + Log.e(TAG, "Cannot send message - no controller registered!") |
| 138 | + Log.e(TAG, " Target: $target, Method: $method") |
| 139 | + Log.e(TAG, " Fix: Ensure UnityEngineController.register() is called before Unity sends messages") |
| 140 | + return false |
| 141 | + } |
| 142 | + |
| 143 | + Log.d(TAG, "Controller found, forwarding to onUnityMessage") |
| 144 | + controller.onUnityMessage(target, method, data) |
| 145 | + Log.d(TAG, "<<< Message forwarded successfully") |
| 146 | + return true |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Send a simple message from Unity to Flutter. |
| 151 | + * Wraps the message as Unity:onMessage. |
| 152 | + * Called from Unity C# via JNI. |
| 153 | + * |
| 154 | + * @param message The message string |
| 155 | + * @return true if the message was sent, false if no controller is registered |
| 156 | + */ |
| 157 | + @JvmStatic |
| 158 | + fun sendSimpleMessage(message: String): Boolean { |
| 159 | + return sendMessageToFlutter("Unity", "onMessage", message) |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Notify Flutter that Unity is ready. |
| 164 | + * Called from Unity C# via JNI. |
| 165 | + * |
| 166 | + * @return true if the notification was sent, false if no controller is registered |
| 167 | + */ |
| 168 | + @JvmStatic |
| 169 | + fun notifyUnityReady(): Boolean { |
| 170 | + return sendMessageToFlutter("Unity", "onReady", "true") |
| 171 | + } |
| 172 | +} |
0 commit comments