2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

UnityでiOSのHaptic Feedback(触覚フィードバック)を実装してみた

Last updated at Posted at 2025-08-27

はじめに

ハイパーカジュアルゲームを触っていると、ボタンを押した時に「ぶるっ」と震える演出をよく見かけます。

これがあるだけでUIの体験が一気にリッチになり、タップの気持ちよさも増すんですよね。

「Unityでこれできないかな?」と思ったのですが、調べてみると 標準機能には存在しない ことが分かりました。

そこで今回は、iOS向けのネイティブプラグインを自作して実装してみたので、その手順を紹介します。

実現方法

UnityでiOSの機能を使う場合は以下の2択になります。

  1. アセットを導入する(Asset Storeで有料/無料のものがある)
  2. 自作プラグインを作る

せっかくなので今回は自作してみましょう。

やることはシンプルで、Unity(C#) → iOS(Objective-C++) に橋渡しするだけです。

実装コード

Unity側(C#)

まずはUnityで呼び出すクラスを作成します。

HapticManager.cs

using System.Runtime.InteropServices;
using UnityEngine;

public class HapticManager
{
    [DllImport("__Internal")]
    private static extern void PlayHaptic();

    public static void TriggerHapticFeedback()
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer) PlayHaptic();
    }
}

ポイントは[DllImport("__Internal")]です。

これでiOS側の関数を呼び出せるようになります。

iOS側(Objective-C++)

次にiOSのネイティブ処理を書きます。

HapticFeedback.mm

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

extern "C" {
    void PlayHaptic() {
        UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];
        [generator prepare];
        [generator impactOccurred];
    }
}

ここではApple純正のUIImpactFeedbackGeneratorを利用して、タップ時に「Medium」強度の振動を発生させています。

配置場所について

作成した .mm ファイルは以下の場所に置きます:

Assets/Plugins/iOS/HapticFeedback.mm

この場所に置くことで、iOSビルド時のみ組み込まれるようになります。

動かしてみた

実際にiOS端末にビルドしてボタンを押すと……

ぶるっと震えました!🙌

まとめ

  • Unityには標準でHaptic Feedbackはない
  • iOSのネイティブ機能を呼び出すにはプラグインを作る
  • C#とObjective-C++を橋渡しするだけで実現可能
  • 実装するとUI体験が一気に向上

Unityエンジニアとして、「プラグインで足りない機能を自作できる」 のは大きな武器になります。

ぜひ皆さんも試してみてください!

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?