You can make your Java code into .jar or .aar plugin the call it from C#. You can also use Unity's AndroidJavaClass
and AndroidJavaObject
API which totally eliminates the need for a Java compiled plugin.
With the AndroidJavaClass
and AndroidJavaObject
API, the equivalent of the Java code below:
Intent call = new Intent(Intent.ACTION_CALL, Uri.parse("tel:+79011111115"));
in C# is as below:
string phoneNum = "tel: +79011111115";
//For accessing static strings(ACTION_CALL) from android.content.Intent
AndroidJavaClass intentStaticClass = new AndroidJavaClass("android.content.Intent");
string actionCall = intentStaticClass.GetStatic<string>("ACTION_CALL");
//Create Uri
AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse", phoneNum);
//Pass ACTION_CALL and Uri.parse to the intent
AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", actionCall, uriObject);
Remember you must start the Activity on the Intent to finish it and below is what it looks like in Java:
startActivity(call);
Below is the equivalent of that code in C# code to start the Activity:
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
try
{
//Start Activity
unityActivity.Call("startActivity", intent);
}
catch (Exception e)
{
Debug.LogWarning("Failed to Dial number: " + e.Message);
}
Finally, just like in Java, you must also add <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
permission otherwise it won't work. See this post for how to add this Android permission to Unity.
For Android 6.0 and above. You have to use run-time permission. This Github project should be work fine just for this.