说明
Unity 有自己的 接口 Handheld.Vibrate() 来实现手机的震动,但是不能控制震动时长。
现在来介绍Unity 调用 Android 系统的震动接口实现手机震动效果。基本思路:将Android的震动功能封装到一个工具类中,将此工具类打打包成一个.jar文件供Unity使用。这样就不用每建立一个新的Unity工程再去写一个Android的插件了。只要Unity工程引用了这个.jar插件,再在代码里调用.jar的震动接口就可以了。
Android 工程的实现
1.创建一个新的空的工程。
2.在这个工程中创建一个Android Library。
(这里用来实现震动,并将此Module打包成.jar)。
3.配置AndoridMainifest.xml,必须要有权限
<uses-permission android:name="android.permission.VIBRATE" />
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.idadi.commonlibrary" >
<uses-permission android:name="android.permission.VIBRATE" />
</manifest>
4.在此Module引用Unity的Class.jar库.
如何引用参考:https://blog.csdn.net/SGamble/article/details/101376652
5.在Module中实现震动功能
VibratorTool.java脚本
package com.tools.common;
import android.app.Activity;
import android.app.Service;
import android.os.Vibrator;
import android.widget.Toast;
import com.unity3d.player.UnityPlayer;
public class VibratorTool {
public static Vibrator s_vibrator;
public static Activity s_curActivity;
/**
* 设置震动参数,这个函数在写Android测试工程时使用,Unity项目不用调用此函数。
*/
public static void SetVibrateParam(Activity curActivity) {
s_curActivity = curActivity;
if (curActivity != null) {
s_vibrator = (Vibrator) curActivity.getSystemService(Service.VIBRATOR_SERVICE);
}
ToastTool.SetToastParam(curActivity);
}
protected static void GetVibrateParam() {
if (s_curActivity == null) {
s_curActivity = UnityPlayer.currentActivity;
}
if (s_vibrator == null) {
s_vibrator = (Vibrator) s_curActivity.getSystemService(Service.VIBRATOR_SERVICE);
}
}
/**
* 手机震动
*/
public static void CVibrate(int milliseconds) {
ToastTool.ShowToast("手机震动");
GetVibrateParam();
s_vibrator.vibrate(milliseconds);
}
/**
* 手机震动 ----短震动
*/
public static void CVibrateShort() {
ToastTool.ShowToast("手机震动 ----短震动");
GetVibrateParam();
s_vibrator.vibrate(new long[]{100, 10, 100, 1000}, -1);
}
/**
* 手机震动 ----长震动
*/
public static void CVibrateLong() {
ToastTool.ShowToast("手机震动 ----长震动");
GetVibrateParam();
s_vibrator.vibrate(new long[]{100, 100, 100, 1000}, 0);
}
/**
* 震动取消
*/
public static void CCancelVibrate() {
ToastTool.ShowToast("手机震动 ----震动取消");
GetVibrateParam();
s_vibrator.cancel();
}
}
辅助的脚本 ToastTool.java
package com.tools.common;
import android.app.Activity;
//import android.app.Service;
//import android.os.Vibrator;
import android.widget.Toast;
import com.unity3d.player.UnityPlayer;
public class ToastTool {
public static Activity s_curActivity;
public static boolean s_showToast = true;
public static void SetShowToast(boolean show) {
s_showToast = show;
}
/**
* 设置震动参数
*/
public static void SetToastParam(Activity curActivity) {
s_curActivity = curActivity;
if (s_curActivity == null) {
s_curActivity = UnityPlayer.currentActivity;
}
}
protected static void GetToastParam() {
if (s_curActivity == null) {
s_curActivity = UnityPlayer.currentActivity;
}
}
protected static void ShowToast(CharSequence text) {
GetToastParam();
if (s_showToast == false) {
return;
}
if (s_curActivity != null) {
Toast.makeText(s_curActivity, text, Toast.LENGTH_SHORT).show();
} else {
}
}
}
5.将Module打包成.jar
(具体打包请自行百度合适的打包方法)。我用的 Android Studio 3.5.2
在Module的Build.gradle中添加
//生成jar包
task makeJar(type: Copy) {
delete 'build/outputs/network.jar'
from('build/intermediates/packaged-classes/release/') //jar文件来源
into('build/libs/') //生成路径
include('classes.jar')
rename('classes.jar', 'commontools.jar') //命名为network.jar
}
makeJar.dependsOn(build)
整个文件如下:
apply plugin: 'com.android.library'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
//生成jar包
task makeJar(type: Copy) {
delete 'build/outputs/network.jar'
from('build/intermediates/packaged-classes/release/') //jar文件来源
into('build/libs/') //生成路径
include('classes.jar')
rename('classes.jar', 'commontools.jar') //命名为network.jar
}
makeJar.dependsOn(build)
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation files('libs/classes.jar')
}
生成.jar
Unity中的实现
1.新建Unity项目
将.jar文件拷贝到项目 Plugins\Android 文件夹中。
2.在Plugins\Android 文件夹中新建 AndroidManifest.xml 文件。
并配置权限
<uses-permission android:name="android.permission.VIBRATE" />
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
xmlns:tools="http://schemas.android.com/tools"
android:installLocation="preferExternal">
<uses-permission android:name="android.permission.VIBRATE" />
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>
<application
android:theme="@style/UnityThemeSelector"
android:icon="@mipmap/app_icon"
android:label="@string/app_name">
<activity android:name="com.unity3d.player.UnityPlayerActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
</application>
</manifest>
3.C#代码调用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestJave : MonoBehaviour
{
AndroidJavaClass VibratorTool = null;
// Start is called before the first frame update
void Start()
{
VibratorTool = new AndroidJavaClass("com.tools.common.VibratorTool");
//smsDialog.CallStatic<AndroidJavaObject>("getInstance").Call("init", getContext());
}
public void CVibrate(int milliseconds)
{
Debug.LogError("---- 调用震动");
VibratorTool.CallStatic("CVibrate", milliseconds);
}
public void CVibrateShort()
{
Debug.LogError("---- 调用震动 -- 短");
VibratorTool.CallStatic("CVibrateShort");
}
public void CVibrateLong()
{
Debug.LogError("---- 调用震动 -- 长");
VibratorTool.CallStatic("CVibrateLong");
}
public void CCannelVibrate()
{
Debug.LogError("---- 调用震动 -- 取消");
VibratorTool.CallStatic("CCancelVibrate");
}
}
4.打包测试
源码工程
包含了Android工程与Unity工程
链接地址:https://download.csdn.net/download/ZFSR05255134/15137850