Hello I’m trying it include an android library in my unity project. My android code is as follows:
package com.androidtester;
public class ATests {
public String teststr() {
return "testing...";
}
public boolean testbool() {
return true;
}
public int testint() {
return 123;
}
}
My c# code is as follows:
public class GameControl : MonoBehaviour {
void Start ()
{
try {
AndroidJavaClass atests = new AndroidJavaClass ("com.androidtester.ATests");
Debug.Log ("Test Str:" + atests.Call<string> ("teststr"));
Debug.Log ("Test Int:" + atests.Call<int> ("testint"));
Debug.Log ("Test Bool:" + atests.Call<bool> ("testbool"));
} catch (Exception e) {
Debug.Log ("ATests Error..." + e.ToString ());
}
}
}
This is within a blank unity 2D project with an empty game object containing GameControl. My folder structure is as so:
When I run my unity application on an android device I see this in adb’s logcat:
I/Unity (29910): GameControl:Start() (at ./Assets/GameControl.cs:10)
I/Unity (29910):
I/Unity (29910): (Filename: ./Assets/GameControl.cs Line: 10)
I/Unity (29910):
I/Unity (29910): Test Str:
I/Unity (29910): UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
I/Unity (29910): UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
I/Unity (29910): UnityEngine.Logger:Log(LogType, Object)
I/Unity (29910): UnityEngine.Debug:Log(Object)
I/Unity (29910): GameControl:Start() (at ./Assets/GameControl.cs:13)
I/Unity (29910):
I/Unity (29910): (Filename: ./Assets/GameControl.cs Line: 13)
I/Unity (29910):
I/Unity (29910): Test Int:0
I/Unity (29910): UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
I/Unity (29910): UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
I/Unity (29910): UnityEngine.Logger:Log(LogType, Object)
I/Unity (29910): UnityEngine.Debug:Log(Object)
I/Unity (29910): GameControl:Start() (at ./Assets/GameControl.cs:14)
I/Unity (29910):
I/Unity (29910): (Filename: ./Assets/GameControl.cs Line: 14)
I/Unity (29910):
I/Unity (29910): Test Bool:False
I/Unity (29910): UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
I/Unity (29910): UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
I/Unity (29910): UnityEngine.Logger:Log(LogType, Object)
I/Unity (29910): UnityEngine.Debug:Log(Object)
I/Unity (29910): GameControl:Start() (at ./Assets/GameControl.cs:15)
I/Unity (29910):
I/Unity (29910): (Filename: ./Assets/GameControl.cs Line: 15)
I/Unity (29910):
For some reason the return values from android are all wrong. If I enter a different function name or return type they generate exceptions so I know the signatures are matching up. I should mention that if I switch the android function to ‘public static’ then use 'AndroidJavaClass.CallStatic() in c# everything works fine. It’s the plain AndroidJavaClass.Call() that gives incorrect return values.