在Unity中打电话? [英] Make phone call in Unity?

查看:331
本文介绍了在Unity中打电话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#脚本中使用

Application.OpenURL("tel:+79011111115");

拨号器出现,但未拨打电话 如果是Java,我可以说它像

Dialer appeared, but phone call did not occur If it were Java, I could say that it worked like

Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:+79011111115"));

但是我需要:

Intent call = new Intent(Intent.ACTION_CALL, Uri.parse("tel:+79011111115"));

在C#中是否有Java的ACTION_CALL类比?
预先感谢

Is there any analogy of Java's ACTION_CALL in C#?
Thanks in advance

推荐答案

您可以将Java代码放入 .jar .aar 插件中,并通过C#对其进行调用.您还可以使用Unity的AndroidJavaClassAndroidJavaObject API,完全不需要Java编译插件.

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.

使用AndroidJavaClassAndroidJavaObject API,与下面的 Java 代码等效:

With the AndroidJavaClass and AndroidJavaObject API, the equivalent of the Java code below:

Intent call = new Intent(Intent.ACTION_CALL, Uri.parse("tel:+79011111115"));

C#中的

如下:

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);

请记住,您必须在Intent上启动Activity才能完成它,下面是 Java 中的样子:

Remember you must start the Activity on the Intent to finish it and below is what it looks like in Java:

startActivity(call);

下面的代码等同于 C#代码中用于启动活动的代码:

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);
}

最后,就像在Java中一样,您还必须添加<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>权限,否则它将不起作用.请参阅这篇帖子,了解如何将此Android权限添加到Unity

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.

对于 Android 6.0 及更高版本.您必须使用运行时权限. 这个 Github项目应该可以很好地解决这个问题.

For Android 6.0 and above. You have to use run-time permission. This Github project should be work fine just for this.

这篇关于在Unity中打电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆