微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c#-在Unity中打电话?

我在C#脚本中使用了@H_502_1@

@H_502_1@

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

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

@H_502_1@

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

但是我需要:@H_502_1@

@H_502_1@

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

在C#中是否有Java的ACTION_CALL的类比?
提前致谢@H_502_1@

解决方法:@H_502_1@

您可以将Java代码放入.jar或.aar插件中,并从C#中调用它.您还可以使用Unity的AndroidJavaClass和AndroidJavaObject API,从而完全不需要Java编译插件.@H_502_1@

使用AndroidJavaClass和AndroidJavaObject API,等效于以下Java代码:@H_502_1@

@H_502_1@

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

在C#中如下所示:@H_502_1@

@H_502_1@

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中的样子:@H_502_1@

@H_502_1@

startActivity(call);

下面是等效于C#代码中用于启动Activity的代码:@H_502_1@

@H_502_1@

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的信息,请参见this.@H_502_1@

适用于Android 6.0及更高版本.您必须使用运行时权限.为此,This Github项目应该可以正常工作.@H_502_1@

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐