2

I have embedded a Unity project into a separate Android app-- How can I debug an AndroidJavaObject call in C# if its embedded (see SpinningCube.cs below)?

Perhaps adding logging statements in the C# portion or attaching to some process?

--

SpinningCube.cs

public class SpinningCube : MonoBehaviour 
{
    ...

    //Sends dummy data to the receiveResult function in Java
    void SendResultToJava(float value)
    {
        using (AndroidJavaClass javaPlugin = new AndroidJavaClass("com.example.my.button.MyPlugin"))
        {
            AndroidJavaObject pluginInstance = javaPlugin.CallStatic<AndroidJavaObject>("instance");
            pluginInstance.Call("receiveResult", value.ToString());
        }

    }

    //Called from Java
    void TestFunction(string dummy_key)
    {
        float value = (float)0.9999; 
        SendResultToJava(value); //Send the value to Java
    }
}

--

I suspect my Android plugin com.example.my.button.MyPlugin is not setup or working properly b/c receiveResult() never seems to be called by Unity and no errors are popping in Android Studio.

MainActivity.java (Cube is the object name)

    ...
    MyPlugin.testInstance = MyPlugin.instance();
    String unity_str = MyPlugin.testInstance.UnitySendMessageExtension("Cube", "testFunction","dummyKey");

MyPlugin.java

    package com.example.my.button;

    import com.unity3d.player.UnityPlayer;

    public final class MyPlugin
    {
        //Make class static variable so that the callback function is sent to one instance of this class.
        public static MyPlugin testInstance;

        public static MyPlugin instance()
        {
            if(testInstance == null)
            {
                testInstance = new MyPlugin();
            }
            return testInstance;
        }

        String result = "";


        public String UnitySendMessageExtension(String gameObject, String functionName, String funcParam)
        {
            UnityPlayer.UnitySendMessage(gameObject, functionName, funcParam);
            String tempResult = result;
            return tempResult;
        }

        //Receives result from C# and saves it to result  variable
        void receiveResult(String value)
        {
            result = "";//Clear old data
            result = value; //Get new one
        }
    }   

code adapted from Programmer's solution

Thanks!

  • `Debug.Log` should work fine – Programmer Jul 05 '18 at 01:19
  • @Programmer Including `Debug.Log` statements to SpinningCube.cs (Unity code) did not output anything to Android Studio's logcat -- where would you expect these statements to log to? – boogie_bullfrog Jul 05 '18 at 07:11
  • You have to filter it with Unity tag. See [this](https://stackoverflow.com/questions/44690357/how-to-create-and-read-log-on-android-devices/44690501#44690501) even for many other options you have – Programmer Jul 05 '18 at 11:06
  • @Programmer helpful link. The Unity logs were not showing in Logcat because I was exporting Unity as a Release Build. They showed up once i switched to a Development Build. – boogie_bullfrog Jul 05 '18 at 16:12
  • Great. Glad you fixed it. – Programmer Jul 05 '18 at 16:13

0 Answers0