本文介绍了随着持续时间和模式振动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个简单的插件。现在我想添加一个振动属性。但是这段代码不起作用。我哪里错了?我的代码如下。你能帮我吗?

I am working on a simple plugin. Now I am trying to add a vibrate property. But this code is not working. Where am I going wrong? My code is as follows. Can you help me please.

Android:

import android.os。 Vibrator;

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
    }

提前致谢...

推荐答案

我的解决方案如下。

Unity:

        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文件示例分钟。现在该插件正在运行。

Also, I found Manifest.xml file example before 1 minute. Now the plugin is working.

这篇关于随着持续时间和模式振动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-10 06:10