コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】入力を簡単にバインディングできる「InputBinder」紹介

はじめに

「InputBinder」を Unity プロジェクトに導入することで
入力を簡単にバインディングできるようになります

使い方

f:id:baba_s:20171121170752p:plain

入力をバインディングしたいオブジェクトに
「Input Binder」をアタッチします

そして、下記のようなスクリプトを作成します

using RyanNielson.InputBinder;
using UnityEngine;

public class Example : MonoBehaviour
{
    public InputBinder m_binder;

    private void Start()
    {
        m_binder = GetComponent<InputBinder>();

        m_binder.BindAxis( "Horizontal", Horizontal );
        m_binder.BindAxis( "Vertical"  , Vertical   );

        m_binder.BindButton( "Jump", InputEvent.Pressed , JumpPressed  );
        m_binder.BindButton( "Jump", InputEvent.Released, JumpReleased );
        m_binder.BindButton( "Jump", InputEvent.Held    , JumpHeld     );
    }

    private void Horizontal( float value )
    {
        Debug.Log( "Horizontal " + value );
    }

    private void Vertical( float value )
    {
        Debug.Log( "Vertical " + value );
    }

    private void JumpPressed()
    {
        Debug.Log( "JumpPressed" );
    }

    private void JumpReleased()
    {
        Debug.Log( "JumpReleased" );
    }

    private void JumpHeld()
    {
        Debug.Log( "JumpHeld" );
    }
}

あとは、このスクリプトをゲームオブジェクトにアタッチして
Inspector で「Input Binder」を設定すると、
矢印キーやスペースキーを押した時にイベントが呼び出されることが確認できます

f:id:baba_s:20171121171328p:plain

Inspector で各種イベントを設定することも可能です

関連記事