我正在制作一个简单的插件。现在我正在尝试添加振动属性。但是这段代码不起作用。我哪里错了?我的代码如下。你能帮我吗?
机器人:
import android.os.Vibrator;
public class Brid {
private Context context;
private static Brid instance;
public Brid()
{
this.instance = this;
}
public static Brid instance()
{
if(instance == null) {
instance = new Brid();
}
return instance;
}
public void setContext(Context context) {
this.context = context;
}
public void Vibrate()
{
Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(100);
}
}
Unity3D:
public static void Vibrate(AndroidJavaObject Brid, AndroidJavaObject context)
{
if (Brid == null)
{
using (AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
context = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
}
using (AndroidJavaClass pluginClass = new AndroidJavaClass("com.xxx.ultimatemobile.Brid"))
{
if (pluginClass != null)
{
Brid = pluginClass.CallStatic<AndroidJavaObject>("instance");
Brid.Call<AndroidJavaObject>("setContext", context);
Brid.Call<AndroidJavaObject>("Vibrate");
}
}
}
}
按钮事件:
public void vibrate()
{
#if UNITY_ANDROID
Bridge.Vibrate(null, null);
#endif
}
提前致谢...
答案 0 :(得分:1)
我看到你有一个解决方案。对于那些不想使用Java的人。您只能从C#执行此操作。
1.转到<UnityInstallationDirecory>\Editor\Data\PlaybackEngines\AndroidPlayer\Apk
,将AndroidManifest.xml
文件复制到<ProjectName>Assets\Plugins\Android
2.现在从<ProjectName>Assets\Plugins\Android
打开复制的清单文件并向其添加<uses-permission android:name="android.permission.VIBRATE"/>
。保存,构建和运行。如果这是一个权限问题,现在应该解决了。
AndroidManifest.xml
应该是什么样子(Unity 5.4):
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:theme="@style/UnityThemeSelector"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:debuggable="true">
<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>
你甚至不需要为简单的振动编写Java插件。这可以使用Unity的AndroidJavaClass和AndroidJavaObject类来完成。
没有Java的完整振动插件。
using UnityEngine;
using System.Collections;
public class Vibrate
{
public AndroidJavaClass unityPlayer;
public AndroidJavaObject currentActivity;
public AndroidJavaObject sysService;
public void Vibrate()
{
unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
sysService = currentActivity.Call<AndroidJavaObject>("getSystemService", "vibrator");
}
//Functions from https://developer.android.com/reference/android/os/Vibrator.html
public void vibrate()
{
sysService.Call("vibrate");
}
public void vibrate(long milliseconds)
{
sysService.Call("vibrate", milliseconds);
}
public void vibrate(long[] pattern, int repeat)
{
sysService.Call("vibrate", pattern, repeat);
}
public void cancel()
{
sysService.Call("cancel");
}
public bool hasVibrator()
{
return sysService.Call<bool>("hasVibrator");
}
}
<强> Usage 强>:
Vibrate vibrate = new Vibrate();
if (vibrate.hasVibrator())
{
//Vibrate
vibrate.vibrate();
//Vibrate for 500 milliseconds
vibrate.vibrate(500);
// Start without a delay
// Vibrate for 200 milliseconds
// Sleep for 2000 milliseconds
long[] pattern = { 0, 200, 2000 };
vibrate.vibrate(pattern, 0);
//Cancel Vibration
vibrate.cancel();
}
答案 1 :(得分:0)
我的解决方案如下。
<强>统一:强>
using (AndroidJavaClass pluginClass = new AndroidJavaClass("com.xx.ultimatemobile.Brid"))
{
if (pluginClass != null)
{
Brid = pluginClass.CallStatic<AndroidJavaObject>("instance");
Brid.Call("setContext", context);
context.Call("runOnUiThread", new AndroidJavaRunnable(() =>
{
Brid.Call("Vibrate");
}));
}
}
另外,我在1分钟之前找到了Manifest.xml文件示例。现在该插件正在运行。