Android主应用向Unity AAR插件传递整数参数的实现方法问询
Hey there! Since you’ve already got your Unity project exported as an AAR and integrated into Android Studio, let’s cut straight to the working solution for passing an integer from your Android host app to Unity, and getting Unity to trigger object activation based on that value.
Step 1: Configure Unity to Receive the Integer
First, we need a C# script in Unity that can listen for the integer from Android and handle object activation. Here’s how to set it up:
- Create a new C# script (name it
AndroidCommHandler.cs) and attach it to a persistent GameObject in your Unity scene—like aGameManagerobject you mark withDontDestroyOnLoadso it stays active across scenes. - Add this code to the script:
using UnityEngine; public class AndroidCommHandler : MonoBehaviour { private void Awake() { // Ensure this object stays alive when switching scenes DontDestroyOnLoad(gameObject); } // This method will be called directly from Android public static void ReceiveIntFromAndroid(int receivedValue) { Debug.Log($"Unity got integer from Android: {receivedValue}"); // Logic to activate different objects based on the value switch(receivedValue) { case 1: ToggleObjectActive("PlayerCharacter", true); break; case 2: ToggleObjectActive("EnemySpawner", true); break; case 3: ToggleObjectActive("UI_MainMenu", true); break; default: Debug.LogWarning($"Unknown value: {receivedValue}"); break; } } private static void ToggleObjectActive(string objectName, bool shouldActivate) { GameObject target = GameObject.Find(objectName); if(target != null) { target.SetActive(shouldActivate); // Optional: Deactivate other objects here if needed } else { Debug.LogError($"Couldn't find object: {objectName}"); } } }
- Double-check that this script is included in your Unity build (it will be by default if the GameObject is in your active scene) before re-exporting the AAR (if you made changes after your initial export).
Step 2: Send the Integer from Android to Unity
In your Android Studio project, you’ll use Unity’s built-in UnitySendMessage method to call the C# function we just created. Here’s how to implement it in both Kotlin and Java:
Kotlin Implementation
import com.unity3d.player.UnityPlayer // Call this method when you need to send the integer to Unity fun sendIntToUnity(value: Int) { // Parameters: // 1. Name of the Unity GameObject with our script attached // 2. Name of the C# method to invoke // 3. The value (converted to string, since UnitySendMessage uses string params) UnityPlayer.UnitySendMessage( "GameManager", "ReceiveIntFromAndroid", value.toString() ) }
Java Implementation
import com.unity3d.player.UnityPlayer; public void sendIntToUnity(int value) { UnityPlayer.UnitySendMessage( "GameManager", "ReceiveIntFromAndroid", String.valueOf(value) ); }
Critical Notes:
- Matching Names: The GameObject name (
GameManager) and method name (ReceiveIntFromAndroid) must exactly match what’s in your Unity project—case sensitivity matters! - Parameter Conversion:
UnitySendMessageonly accepts string arguments, but Unity will automatically parse the string back to an integer for our C# method (since it expects anintparameter). No extra parsing needed on the Unity side. - Timing: Make sure you call this method after the Unity module has fully loaded. If you send the integer too early (before the
GameManageris active), Unity won’t receive it. You can listen for Unity’sonUnityPlayerReadycallback to trigger the send.
Step 3: Troubleshooting Common Issues
- Unity doesn’t receive the value: Check for typos in GameObject/method names. Ensure the
GameManageris active in the Unity scene when the message is sent. - Objects don’t activate: Verify the object names in
ToggleObjectActivematch exactly with your Unity scene objects. Also, make sure those objects are set to inactive by default. - UnityPlayer class not found: Confirm your Unity AAR module is properly added as a dependency in your Android Studio project (check
build.gradlefiles).
内容的提问来源于stack exchange,提问作者Borut Kolar
相关产品推荐
相关产品推荐

