直接上代码
csharp
#if UNITY_IOS
[DllImport("__Internal")]
private static extern void EX_C_CallVibrateE(int eID);
#endif
public static void CallVibrate(int eID)
{
#if UNITY_EDITOR
#elif UNITY_ANDROID
long miSec = 30;
if(eID == 1520)
{
miSec = 60;
}
//通过报名获取java class
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
//获取当前的activity
var activity = jc.GetStatic<AndroidJavaObject>("currentActivity");
var service = new AndroidJavaClass("android.app.Service");
var s = service.GetStatic<string>("VIBRATOR_SERVICE");
var vib = activity.Call<AndroidJavaObject>("getSystemService", s);
vib.Call("vibrate", miSec);
#elif UNITY_IOS
EX_C_CallVibrateE(eID);
#endif
}
这是我的方法 对应到ios平台和安卓平台 其中eID = 1520表示长震动,为1519为短震动
在打包发布安卓平台时,需要申请一下安卓的震动权限,其他不需设置
申请方法:在Android的打包文件的AndroidManifest中添加
<uses-permission android:name="android.permission.VIBRATE" />
即可
在打包发布Ios平台时,需要在Assets\Plugins\IOS目录下添加一个.m文件,供c#代码调用
代码如下,可根据自身需求删除不必要的方法
csharp
#import <Foundation/Foundation.h>
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AudioToolbox/AudioToolbox.h>
#include <sys/param.h>
#include <sys/mount.h>
#ifdef __cplusplus
extern "C"
{
#endif
bool CanOpenURL(char* name)
{
NSURL *appURL = [NSURL URLWithString:[NSString stringWithUTF8String:name]];
if ([[UIApplication sharedApplication] canOpenURL:appURL])
{
return YES;
}
else
{
return NO;
}
}
void GotoAppSetting()
{
NSURL *settingURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if([[UIApplication sharedApplication] canOpenURL:settingURL])
{
[[UIApplication sharedApplication] openURL:settingURL];
}
}
long getFreeSpace() {
NSDictionary *systemAttributes = [[NSFileManager defaultManager] fileSystemAttributesAtPath:NSHomeDirectory()];
NSString *diskTotalSize = [systemAttributes objectForKey:@"NSFileSystemSize"];
NSLog(@"totalspace:%@ B", diskTotalSize);
NSLog(@"totalspace:%.2f GB", [diskTotalSize floatValue]/1024/1024/1024);
NSString *diskFreeSize = [systemAttributes objectForKey:@"NSFileSystemFreeSize"];
NSLog(@"freespace:%@ B", diskFreeSize);
NSLog(@"freespace:%.2f GB", [diskFreeSize floatValue]/1024/1024/1024);
return [diskFreeSize longLongValue]/(1024 *1024);
}
bool canAttPopup()
{
if (@available(iOS 14, *))
{
return [ATTrackingManager trackingAuthorizationStatus] == ATTrackingManagerAuthorizationStatusNotDetermined;
}
return false;
}
void EX_C_ATTAuthorize()
{
if (@available(iOS 14.0, *)){
[ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
NSLog(@"ATTrackingManagerStatus: %d", (int)status);
if(ATTrackingManagerAuthorizationStatusAuthorized == status)
{
//[FBAdSettings setAdvertiserTrackingEnabled:YES];
}
UnitySendMessage("GameManager", "OnATTAuthStatus", [[NSString stringWithFormat:@"%d", (int)status] UTF8String]);
}];
}
}
int EX_C_UserInterfaceStyle()
{
if (@available(iOS 14.0, *))
{
return (int)[[[UIScreen mainScreen] traitCollection] userInterfaceStyle];
}
return 0;
}
void EX_C_CallVibrateE(int eID)
{
AudioServicesPlaySystemSound(eID);
}
#ifdef __cplusplus
}
#endif