This was working a few weeks ago, but now I've noticed my OnReward message is no longer called from my custom plugin.
In my rewardcenter.cs class I call the plugin class to set the listener to the gameObject that this script is attached to (in this example case it's GameObject):
public class rewardcenter : MonoBehaviour { int counter = 0; void Start () { OnReward("85"); } // Update is called once per frame void Update () { if (counter == 20) { #if UNITY_ANDROID PluginClass.SetRewardListener(gameObject.name); Debug.Log("Adding Android Reward Listener: " + gameObject.name); #endif } counter++; } void OnReward(string quantity) { Debug.Log("OnReward: " + quantity); } } In the PluginClass.cs file you can see how I call the setUnityObjectName call on the java plugin class to set the unity object to the passed in GameObject's name string GameObject:
private static AndroidJavaObject pluginClassInstance = null; private static AndroidJavaObject activityContext = null; // pass in the GameObject that implements the OnReward method public static void SetRewardListener(string gameObjName) { if (activityContext == null) { using (AndroidJavaClass activityClass = new AndroidJavaClass ("com.unity3d.player.UnityPlayer")) { activityContext = activityClass.GetStatic<AndroidJavaObject> ("currentActivity"); } } using (AndroidJavaClass pluginClass = new AndroidJavaClass ("pluginclass.com.PluginClass")) { if (pluginClass != null) { pluginClassInstance = pluginClass.CallStatic<AndroidJavaObject> ("getInstance"); activityContext.Call ("runOnUiThread", new AndroidJavaRunnable (() => { pluginClassInstance.Call("setUnityObjectName", gameObjName); })); } } } In the java PluginClass itself later on we attempt to call OnReward using UnitySendMessage on our game object:
public void unityEarnedReward(String quantity) { Log.d(TAG, "unityEarnedReward: " + quantity); if (PluginClass.getInstance()._unityObjectName != null) { Log.d(TAG, "calling OnReward(" + quantity + ") on unityObject with name: " + PluginClass.getInstance()._unityObjectName); com.unity3d.player.UnityPlayer.UnitySendMessage(PluginClass.getInstance()._unityObjectName, "OnReward", quantity); } } I end up with a log output in Android Studio of:
calling OnReward(202) on unityObject with name: GameObject But it never seems to actually call OnReward within the game.
Just for kicks I tried calling OnReward when the script starts, it ends up with an output of:
OnReward: 85 So I know it works and prints to the console when it is executed.
Any idea where I'm going wrong? The weird thing is this worked a few weeks ago, but now the OnReward method isn't executed.
EDIT:
I also tried running it without the specific class declaration like this with no luck:
UnityPlayer.UnitySendMessage(PluginClass.getInstance()._unityObjectName, "OnReward", quantity); I also renamed my gameObject to be MyCustomPlugin but while the android code triggered it doesn't seem the OnReward method was ever triggered on the script linked to MyCustomPlugin.
When I run this code:
void Update () { var unityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer"); unityPlayer.CallStatic ("UnitySendMessage", gameObject.name, "OnReward", "185"); OnReward ("86"); } I end up with two Log messages:
04-14 10:47:47.085 10341-10354/? I/Unity: OnReward: 86 (Filename: ./artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51) 04-14 10:47:47.095 10341-10354/? I/Unity: OnReward: 185 (Filename: ./artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51) 
0 comments:
Post a Comment