使 Android 手机振动 100 毫秒 C# [英] Making an Android phone vibrate for 100ms C#

查看:28
本文介绍了使 Android 手机振动 100 毫秒 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上周我一直在我的手机上尝试了一些游戏,发现有些游戏使用振动作为玩家的反馈"发生过,比如:

I've been trying out a few games on my phone in the last week, and saw that some used vibration as a "feedback" for the player when something happened, like:

  • 玩家撞墙
  • 播放器完美启动
  • 玩家获得了大奖...

等等...

我真的很喜欢给手机一点点震动或轻微震动,基本上可以告诉玩家重要的事情(对于游戏)发生了,通过输出这不仅在屏幕上,也来自扬声器.

I really love how giving a little vibration or small shake to the phone, can basically tell the player that something important (for the game) happened, through an output that isn't only on the screen or comes from the speaker.

我正在使用 Unity 开发我的游戏.

I'm developing my game with Unity.

在我的游戏中,我会让手机在当玩家崩溃以及当他获得大奖时振动,就像我见过的其他漂亮的游戏一样.

In my game, I would make the phone vibrate when the player crashes, and when he receives a big prize, like I've seen other beautiful games doing.

我已经尝试过使用默认振动:Handheld.Vibrate(),效果很好,但它持续了太多时间以满足我的需要,我不会'无法改变任何东西.

I've already tried using the default vibration: Handheld.Vibrate(), that worked, but it was lasting for too much time for my need and I wouldn't have been able to change anything in it.

我读过,要做到这一点,我需要编写一个单独的插件,或者使用已经存在的插件.我没有找到任何可行的,并且不知道如何写

I've read that, to do this, I would need to write a separate plugin, or use one that already exists. I have not found any working ones, and have no idea how to write one!

我只需要调用诸如 vibrateFor(100) 之类的东西,其中 100毫秒.

I would only need to call something like vibrateFor(100) where 100 are the milliseconds.

某些游戏,例如Perfect Slices" 好像经常用这个.

Some games, like "Perfect Slices" seem to use this a lot.

那么,谁能为我制作一个这样的插件或指出一个适用于 Android 的现有插件吗?

提前感谢您的帮助!

编辑:否则,谁能向我详细解释如何使用这个 插件似乎对其他人有用?

Edit: Otherwise, could anyone explain to me in detail how to use this plugin that seems to have worked for other people?

Unity 版本是 2019.2.8f1.

Unity version is 2019.2.8f1.

推荐答案

2018.3 版本起,Unity 允许您将 .java 文件添加到您的 Unity 项目中并进行编译当您构建 Android 播放器时.

Since version 2018.3 Unity allows you to add .java files into your Unity Project and they are compiled when you build an Android player.

1) 将输出的 UnityAndroidVibrator.javaAndroidManifest.xml 文件复制到 Assets/Plugins/Android> Unity 项目中的目录.

1) Copy the output UnityAndroidVibrator.java and AndroidManifest.xml files into the Assets/Plugins/Android directory in your Unity project.

2) 转到:[文件] > [构建设置] > [播放器设置] > [播放器] 并复制包名称.

2) Go to: [File] > [build Settings] > [Player Settings] > [Player] and copy package name.

3) 在这两个类中查找并替换 "uav.uav.uav" 包名称,并使用您在步骤 2) 中复制的包名称.

3) In both classes find and replace "uav.uav.uav" package name with that you copied in step 2).

UnityAndroidVibrator.cs 类:

using UnityEngine;
public class UnityAndroidVibrator : MonoBehaviour 
{
    #if UNITY_ANDROID || UNITY_EDITOR 
    private static AndroidJavaObject plugin = null;
    #endif


    // Use this for initialization
    void Awake () 
    {
        #if UNITY_ANDROID && !UNITY_EDITOR 
        plugin = new AndroidJavaClass("uav.uav.uav.UnityAndroidVibrator").CallStatic<AndroidJavaObject>("instance");
        #endif
    }


    /// <summary>
    /// <para>Vibrates For Given Amount Of Time.</para>
    /// <para>1 sec = 1000 Millseconds :)</para>
    /// </summary>
    /// <param name="DurationInMilliseconds">Duration in milliseconds.</param>
    public void VibrateForGivenDuration(int DurationInMilliseconds)
    {
        plugin.Call("VibrateForGivenDuration", DurationInMilliseconds);

    }

    /// <summary>
    /// Stoping All Vibrate.
    /// </summary>
    public void StopVibrate()
    {
        plugin.Call("StopVibrate");
    }


    /// <summary>
    /// <para>Customs Vibrate or Vibration with Pattern.</para>
    /// <para>Start without a delay</para>
    /// <para>Each element then alternates between vibrate, sleep, vibrate, sleep...</para>
    /// <para>long[] Pattern = {0, 100, 100, 300};</para>
    /// </summary>
    /// <param name="Pattern">Pattern.</param>
    public void CustomVibrate(long[] Pattern)
    {
        plugin.Call("CustomVibrate", Pattern);
    }


}

UnityAndroidVibrator.java 类:

package uav.uav.uav;

import android.os.Vibrator;
import com.unity3d.player.UnityPlayer;

public class UnityAndroidVibrator 
{
    private static UnityAndroidVibrator _instance;

    public UnityAndroidVibrator() {
    }

    public void VibrateForGivenDuration(int duration) {
        Vibrator vibs = (Vibrator)UnityPlayer.currentActivity.getApplicationContext().getSystemService("vibrator");
        vibs.vibrate((long)duration);
    }

    public void StopVibrate() {
        Vibrator vibs = (Vibrator)UnityPlayer.currentActivity.getApplicationContext().getSystemService("vibrator");
        vibs.cancel();
    }

    public void CustomVibrate(long[] Pattern) {
        Vibrator vibs = (Vibrator)UnityPlayer.currentActivity.getApplicationContext().getSystemService("vibrator");
        vibs.vibrate(Pattern, -1);
    }

    public static UnityAndroidVibrator instance() 
    {
        if (_instance == null) 
        {
            _instance = new UnityAndroidVibrator();
        }

        return _instance;
    }
}

AndroidManifest.xml 文件:

<?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">
  <application>
    <activity android:name="com.unity3d.player.UnityPlayerActivity" android:theme="@style/UnityThemeSelector" android:screenOrientation="fullSensor" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density" android:hardwareAccelerated="false">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>
  <uses-permission android:name="android.permission.VIBRATE" />
</manifest>

<小时>

注意:例如,如果您的包名称为com.my.app",则结果名称为:


Note: for example if your package name is "com.my.app" the resulting name would be:

UnityAndroidVibrator.cs"类:

plugin = new AndroidJavaClass("com.my.app.UnityAndroidVibrator").CallStatic<AndroidJavaObject>("instance");

UnityAndroidVibrator.java"类:

package com.my.app;

import android.os.Vibrator;
import com.unity3d.player.UnityPlayer;

这篇关于使 Android 手机振动 100 毫秒 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆