如何知道手机设备当前是否在通话中,是否使用iOS本机代码或Unity C#?
我正在尝试构建一个Unity插件,可以检测手机是否在通话中。我想在打电话时阻止游戏录像机 我统一编写了Objective-C代码和相应的C#类,如下所示: 目标-C插件如何知道手机设备当前是否在通话中,是否使用iOS本机代码或Unity C#?,c#,ios,objective-c,unity3d,C#,Ios,Objective C,Unity3d,我正在尝试构建一个Unity插件,可以检测手机是否在通话中。我想在打电话时阻止游戏录像机 我统一编写了Objective-C代码和相应的C#类,如下所示: 目标-C插件 电话 #import <Foundation/Foundation.h> #import <CallKit/CallKit.h> @interface PhoneCallDelegate : NSObject { } - (BOOL) isOnPhoneCall; @end Unity C#Class
电话
#import <Foundation/Foundation.h>
#import <CallKit/CallKit.h>
@interface PhoneCallDelegate : NSObject
{
}
- (BOOL) isOnPhoneCall;
@end
Unity C#Class PhoneCallController.cs
使用系统集合
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public class PhonCallPlugin
{
/* Interface to native implementation */
[DllImport("__Internal")]
private static extern bool _InPhoneCall();
public static bool IsDuringPhoneCall()
{
return _InPhoneCall();
}
}
然后我尝试调用此脚本引发的IsDuringPhoneCall()
方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PhoneCallController : MonoBehaviour
{
void Update()
{
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
Debug.Log(" During a Phone Call : " + PhonCallPlugin.IsDuringPhoneCall());
}
}
}
我尝试调用该设备,然后启动应用程序,但该方法总是返回False
我深入研究了代码并返回了调用计数,结果发现它总是返回0
为什么会发生这种情况?我如何知道电话是否在Unity的通话中?
提前感谢您在回答中尝试使用as了吗?当然,但它在IOS 10及以上版本中已被弃用。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PhoneCallController : MonoBehaviour
{
void Update()
{
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
Debug.Log(" During a Phone Call : " + PhonCallPlugin.IsDuringPhoneCall());
}
}
}