我正在开发一个简单的插件。现在,我正在尝试添加振动属性。但是此代码不起作用。我要去哪里错了?我的代码如下。你能帮我吗。
Android:
导入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 }
提前致谢…
我的解决方法如下。
统一:
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文件示例。现在,该插件正在运行。