Vibrate with duration and pattern

后端 未结 2 847
盖世英雄少女心
盖世英雄少女心 2020-12-06 23:12

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 plea

相关标签:
2条回答
  • 2020-12-06 23:50

    My solution as follows.

    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");
                    }));
                }
            }
    

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

    0 讨论(0)
  • 2020-12-06 23:59

    I see you have a solution. For those who don't want to use Java. You can do this from C# only.

    1.Go to <UnityInstallationDirecory>\Editor\Data\PlaybackEngines\AndroidPlayer\Apk, Copy the AndroidManifest.xml file to your <ProjectName>Assets\Plugins\Android

    2.Now open the copied Manifest file from <ProjectName>Assets\Plugins\Android and add <uses-permission android:name="android.permission.VIBRATE"/> to it. Save, Build and Run. If this is a permission problem, that should now be solved.

    What your AndroidManifest.xml should look like(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>
    

    You don't even need to write Java plugin for simple vibration. This can be done with Unity's AndroidJavaClass and AndroidJavaObject classes.

    Complete Vibration Plugin without 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();
    }
    
    0 讨论(0)
提交回复
热议问题