Author :JerryYang
Create by 2020.12.07
環(huán)境:
Unity:2019.4.2f1
Xcode:12.2
Android Studio:4.1.1
在用unity進(jìn)行移動端開發(fā)時,有時會需要和原生進(jìn)行通訊。接下來以unity調(diào)用原生震動為例
unity自己有震動的接口:
Handheld.Vibrate();
這個接口調(diào)用一次震動時長為0.5s,不能修改,并且手機(jī)關(guān)閉震動的情況下無效。
那么我們就需要在IOS和安卓端分別寫好接口給unity調(diào)用。
一、unity和iOS通訊
- 首先打開Xcode,新建一個工程Vibrator,然后新建一個類VibratorTool繼承NSObject,具體代碼如下:
VibratorTool.h文件:
//
// VibratorTool.h
// Vibrator
//
// Created by 飛楊 on 02.12.20.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface VibratorTool : NSObject
@end
NS_ASSUME_NONNULL_END
VibratorTool.m文件:
//
// VibratorTool.m
// Vibrator
//
// Created by JerryYang on 02.12.20.
//
#import "VibratorTool.h"
#import <UIKit/UIImpactFeedbackGenerator.h>
@implementation VibratorTool
#if defined (__cplusplus)
extern "C"
{
#endif
void setVibrator(float Intensity){
UIImpactFeedbackGenerator * generator;
if (@available(iOS 13.0, *)) {
generator = [[UIImpactFeedbackGenerator alloc]init];
[generator impactOccurredWithIntensity:Intensity];
} else {
if (Intensity< 0.33) {
generator = [[UIImpactFeedbackGenerator alloc]initWithStyle:UIImpactFeedbackStyleLight];
}else if(Intensity >=0.33 && Intensity < 0.66){
generator = [[UIImpactFeedbackGenerator alloc]initWithStyle:UIImpactFeedbackStyleMedium];
}else{
generator = [[UIImpactFeedbackGenerator alloc]initWithStyle:UIImpactFeedbackStyleHeavy];
}
[generator impactOccurred];
}
}
#if defined (__cplusplus)
}
#endif
@end
-
在unity隨便個文件夾,把這兩個文件拖進(jìn)去,勾選iOS平臺
勾選對應(yīng)平臺.png 在unity引入命名空間:
using System.Runtime.InteropServices;
新建類 Visrator.cs 具體的代碼如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
/// <summary>
/// 震動反饋
/// Author JerryYang
/// Create by 2020.12.03
/// </summary>
public class Visrator
{
#if UNITY_IOS && !UNITY_EDITOR
[DllImport("__Internal")]
private static extern void setVibrator(float Intensity);
#endif
public void SetVibrator(float Intensity = 1)
{
#if UNITY_IOS && !UNITY_EDITOR
setVibrator(Intensity);
#endif
}
}
二、unity和android通訊
- 首先打開Android,新建一個工程Vibrator,然后新建一個類VibratorTool,具體代碼如下:
package com.example.vibrate;
import android.content.Context;
import android.os.Vibrator;
public class VibratorTool {
public void setVibrator(Context act, long t){
Vibrator vibrator = (Vibrator) act.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(t);
}
}
unity默認(rèn)是加了震動權(quán)限,如果沒有權(quán)限,請?jiān)?AndroidManifest.xml 文件中添加:
<uses-permission android:name="android.permission.VIBRATE" />
-
在unity隨便個文件夾,把這個文件拖進(jìn)去,勾選Android平臺
勾選對應(yīng)平臺.png - 在 Visrator.cs 文件中修改SetVibrator方法為:
public void SetVibrator(float Intensity = 1)
{
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidJavaObject jo = new AndroidJavaObject("com.example.vibrate.VibratorTool");
AndroidJavaClass act = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
var actObj = act.GetStatic<AndroidJavaObject>("currentActivity");
long l = (long)(Intensity * 10);
jo.Call("setVibrator", actObj,l);
#elif UNITY_IOS && !UNITY_EDITOR
setVibrator(Intensity);
#endif
}
這樣簡單的通訊功能就完成了,同時用到了OC,C#,Java,C++。
注意:只有真機(jī)才能測試哦!

