Unity Input System

Unity InputSystemでキーの割り当てを変更する方法です。今回はPlayer InputのCreate Actionsで生成されるInput ActionsのAction Maps -> Player -> Actions -> Fire に Space [Keyboard]を追加してそれを実行後にスクリプトから変更します。

パッケージマネージャーからInput Systemをインストールします。

オブジェクト(GameObject)に Player Input コンポーネントを追加します。Create ActionsボタンをクリックしてInput Actionsを作成します。inputactionsファイルを選択して Edit asset ボタンをクリックします。

Action Maps -> Player -> Actions -> Fire を選択します。All Control SchemesをKeyboardMouseに変更します。Fireの+ボタンをクリックしてAdd Bindingを選択します。PropertiesのBinding – Path をクリックします。Keyboard -> By Location of Key (Using US Layout)のSpaceを選択します。Use in Control Scheme の Keyboard&Mouseをチェックします。

スクリプトを追加、変更します。以下のように変更します。クラスの宣言は省略しています。

    
    public PlayerInput playerInput;
    private InputAction fireInputAction;
    private InputBinding keyboardInputBinding;

 // Start is called before the first frame update
    void Start()
    {
        playerInput = GetComponent<PlayerInput>();

        fireInputAction= playerInput.actions["Fire"];
        // keyboardInputBinding= fireInputAction.bindings[5];
        fireInputAction.ApplyBindingOverride(new InputBinding() { path = "<Keyboard>/space", overridePath = "<Keyboard>/q" });
        // fireInputAction.ApplyBindingOverride(5, "<Keyboard>/q");
        // keyboardInputBinding = fireInputAction.bindings[5];

    }

    // Update is called once per frame
    void Update()
    {

    }

    public void OnMove(InputValue value)
    {
        var wasd = value.Get<Vector2>();
    }

    public void OnLook(InputValue value)
    {

    }

    public void OnFire()
    {
        // Spaceキーの代わりにQキーを押すと以下のコートが実行されます
    }

keyboardInputBinding.path が “<Keyboard>/space” から”<Keyboard>/q” に変更されます。qは大文字、小文字どちらでも構わないようです。keyboardInputBinding.groupsはKeyboard&Mouseとなります。

ApplyBindingOverrideはInputBindingクラスを作成する方法とindexを指定する方法とあります。

ActionsのFireという名前がそのままOnFireというメソッドの名前になります。

運用としてはUIにCanvasを作成してインタラクションコンポーネントを利用して変更することになります。ほかのゲームでは左右に矢印ボタンを配置してクリックして選択したり、キー入力を受け付けたりして変更しています。

Unityの新しい入力システムInputSystemを使ってみる

https://gametukurikata.com/basic/inputsystem

こちらのサイトが参考になります。

Unity Scriptable Render Pipeline

スクリプタブルレンダーパイプラインの概要

https://blogs.unity3d.com/jp/2018/01/31/srp-overview/

こちらのブログについて Unity 2019.1 以降で CullingResults, DrawRendererSettings の個所などでエラーが発生して動作しなくなりました。

これはこのクラスがExperimentalに指定されて、Unity 2019.1 以降のバージョンでは利用できなくなりました。次のように書き換えると動作しました。

BasicAssetPipe.cs

using UnityEngine;
using UnityEngine.Rendering;
// using UnityEngine.Experimental.Rendering;


[ExecuteInEditMode]
public class BasicAssetPipe : RenderPipelineAsset
{
    public Color clearColor = Color.green;

#if UNITY_EDITOR
    [UnityEditor.MenuItem("SRP-Demo/01 - Create Basic Asset Pipeline")]
    static void CreateBasicAssetPipeline()
    {
        var instance = ScriptableObject.CreateInstance<BasicAssetPipe>();
        UnityEditor.AssetDatabase.CreateAsset(instance, "Assets/SRP-Demo/1-BasicAssetPipe/BasicAssetPipe.asset");
    }

#endif

    protected override RenderPipeline CreatePipeline()
    {
        return new BasicPipeInstance(clearColor);
    }
}

public class BasicPipeInstance : RenderPipeline
{
    private Color m_ClearColor = Color.black;

    public BasicPipeInstance(Color clearColor)
    {
        m_ClearColor = clearColor;
    }

    protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        // does not so much yet :()
        // base.Render(context, cameras);

        // clear buffers to the configured color
        var cmd = new CommandBuffer();
        cmd.ClearRenderTarget(true, true, m_ClearColor);
        context.ExecuteCommandBuffer(cmd);
        cmd.Release();
        context.Submit();
    }
}

OpaqueAssetPipe.cs

using System;
using UnityEngine;
using UnityEngine.Rendering;
// using UnityEngine.Experimental.Rendering;

[ExecuteInEditMode]
public class OpaqueAssetPipe : RenderPipelineAsset
{
#if UNITY_EDITOR
    [UnityEditor.MenuItem("SRP-Demo/02 - Create Opaque Asset Pipeline")]
    static void CreateBasicAssetPipeline()
    {
        var instance = ScriptableObject.CreateInstance<OpaqueAssetPipe>();
        UnityEditor.AssetDatabase.CreateAsset(instance, "Assets/SRP-Demo/2-OpaqueAssetPipe/OpaqueAssetPipe.asset");
    }
#endif

    protected override RenderPipeline CreatePipeline()
    {
        return new OpaqueAssetPipeInstance();
    }
}

public class OpaqueAssetPipeInstance : RenderPipeline
{
    protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        // base.Render(context, cameras);

        foreach (var camera in cameras)
        {
            // Culling
            ScriptableCullingParameters cullingParams;

            if (!(camera.TryGetCullingParameters(out cullingParams)))
                 continue;

            var cull = context.Cull(ref cullingParams);

            // Setup camera for rendering (sets render target, view/projection matrices and other
            // per-camera built-in shader variables).
            context.SetupCameraProperties(camera);

            // clear depth buffer
            var cmd = new CommandBuffer();
            cmd.ClearRenderTarget(true, false, Color.black);
            context.ExecuteCommandBuffer(cmd);
            cmd.Release();

            // Draw opaque objects using BasicPass shader pass
            var settings = new DrawingSettings(new ShaderTagId("BasicPass"), new SortingSettings(camera));
            settings.sortingSettings = new SortingSettings() { criteria = SortingCriteria.CommonOpaque };
            var filterSettings = new FilteringSettings(renderQueueRange: RenderQueueRange.opaque);
            context.DrawRenderers(cull, ref settings ,ref filterSettings);

            // Draw skybox
            context.DrawSkybox(camera);

            context.Submit();
        }
    }
}

TransparentAssetPipe.cs

using System;
using UnityEngine;
using UnityEngine.Rendering;
// using UnityEngine.Experimental.Rendering;

[ExecuteInEditMode]
public class TransparentAssetPipe : RenderPipelineAsset
{
#if UNITY_EDITOR
    [UnityEditor.MenuItem("SRP-Demo/03 - Create Transparent Asset Pipeline")]
    static void CreateBasicAssetPipeline()
    {
        var instance = ScriptableObject.CreateInstance<TransparentAssetPipe>();
        UnityEditor.AssetDatabase.CreateAsset(instance, "Assets/SRP-Demo/3-TransparentAssetPipe/TransparentAssetPipe.asset");
    }
#endif

    protected override RenderPipeline CreatePipeline()
    {
        return new TransparentAssetPipeInstance();
    }
}

public class TransparentAssetPipeInstance : RenderPipeline
{
    protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        // base.Render(context, cameras);

        foreach (var camera in cameras)
        {
            // Culling
            ScriptableCullingParameters cullingParams;

            if (!(camera.TryGetCullingParameters(out cullingParams)))
                continue;

            var cull = context.Cull(ref cullingParams);

            // Setup camera for rendering (sets render target, view/projection matrices and other
            // per-camera built-in shader variables).
            context.SetupCameraProperties(camera);

            // clear depth buffer
            var cmd = new CommandBuffer();
            cmd.ClearRenderTarget(true, false, Color.black);
            context.ExecuteCommandBuffer(cmd);
            cmd.Release();

            // Draw opaque objects using BasicPass shader pass
            var settings = new DrawingSettings(new ShaderTagId("BasicPass"), new SortingSettings(camera));
            settings.sortingSettings = new SortingSettings() { criteria = SortingCriteria.CommonOpaque };
            var filterSettings = new FilteringSettings(renderQueueRange: RenderQueueRange.opaque);
            context.DrawRenderers(cull, ref settings, ref filterSettings);


            // Draw skybox
            context.DrawSkybox(camera);

            settings.sortingSettings = new SortingSettings() { criteria = SortingCriteria.CommonTransparent };
            filterSettings = new FilteringSettings(renderQueueRange: RenderQueueRange.transparent);
            context.DrawRenderers(cull, ref settings, ref filterSettings);

            context.Submit();
        }
    }
}

こちらのサイトが参考になりました。

LWRP 4-preview を2019.1で動かすように改造した話

https://connect.unity.com/p/lwrp-4-preview-wo2019-1dedong-kasuyounigai-zao-shitahua

Unity Scriptable Render Pipeline(SRP)はパッケージマネージャーからUniversal Render Pipeline(URP, 2019より前はLightweight Render Pipeline(LWRP))、HD レンダーパイプライン(HDRP)をチェックしなくても利用できます。