在 Unity 中打电话?

2023-12-04

我在我的 C# 脚本中使用了

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

出现拨号器,但未拨打电话 如果是 Java,我可以说它的工作原理如下

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

但是我需要:

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

有没有和Java类似的ACTION_CALL in C#?
提前致谢


你可以将你的Java代码变成.jar or .aar插件从 C# 调用它。您还可以使用Unity的AndroidJavaClass and AndroidJavaObjectAPI 完全消除了对 Java 编译插件的需要。

随着AndroidJavaClass and AndroidJavaObjectAPI,相当于Java代码如下:

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

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

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

startActivity(call);

下面是该代码的等效项C#启动 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>许可,否则无法工作。看this有关如何将此 Android 权限添加到 Unity 的帖子。

For 安卓6.0以上。您必须使用运行时权限。ThisGithub 项目应该可以正常工作。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Unity 中打电话? 的相关文章

随机推荐