10.InputActions文件生成C#代码

10.输入配置文件生成CSharp代码


10.1 知识点

课前准备

  • 创建配置文件 配置文件中创建两套行为

根据配置文件生成C#代码

选择InputActions文件 勾选GenerateC#Class

在Inspector窗口设置生成路径,类名,命名空间

应用后生成代码

生成的代码中是根据InputAction配置文件解析Json 生成了类 提供一些方法

使用C#代码进行监听

创建一个Lesson10_输入配置文件生成CSharp代码Input配置文件对象用于监听输入

Lesson10_输入配置文件生成CSharp代码Input配置文件 input;

Start()中创建生成的代码对象,激活配置文件的输入,添加事件监听。Update中监听移动输入。

void Start()
{
    //1.创建生成的代码对象
    input = new Lesson10_输入配置文件生成CSharp代码Input配置文件(); // 实例化一个Lesson10_输入配置文件生成CSharp代码Input配置文件对象

    //2.激活输入
    input.Enable(); // 启用输入

    //3.事件监听
    input.Action1.Fire.performed += (context) =>
    {
        print("鼠标点击 开火");
    };

    input.Action2.Space.performed += (context) =>
    {
        print("空格按下 跳跃");
    };

}

void Update()
{
    print("wasd移动" + input.Action1.Move.ReadValue<Vector2>()); // 监听输入并打印
}

挂载代码后 输入查结果



10.2 知识点代码

Lesson10_输入配置文件生成CSharp代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lesson10_输入配置文件生成CSharp代码 : MonoBehaviour
{

    #region 知识点二 使用C#代码进行监听

    Lesson10_输入配置文件生成CSharp代码Input配置文件 input; // 创建一个Lesson10_输入配置文件生成CSharp代码Input配置文件对象用于监听输入

    #endregion

    void Start()
    {
        #region 知识点一 根据配置文件生成C#代码
        //1.选择InputActions文件
        //2.在Inspector窗口设置生成路径,类名,命名空间
        //3.应用后生成代码
        #endregion

        #region 知识点二 使用C#代码进行监听

        //1.创建生成的代码对象
        input = new Lesson10_输入配置文件生成CSharp代码Input配置文件(); // 实例化一个Lesson10_输入配置文件生成CSharp代码Input配置文件对象

        //2.激活输入
        input.Enable(); // 启用输入

        //3.事件监听
        input.Action1.Fire.performed += (context) =>
        {
            print("鼠标点击 开火");
        };

        input.Action2.Space.performed += (context) =>
        {
            print("空格按下 跳跃");
        };

        #endregion
    }

    #region 知识点二 使用C#代码进行监听

    void Update()
    {
        print("wasd移动" + input.Action1.Move.ReadValue<Vector2>()); // 监听输入并打印
    }

    #endregion
}

Lesson10_输入配置文件生成CSharp代码Input配置文件

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was auto-generated by com.unity.inputsystem:InputActionCodeGenerator
//     version 1.5.1
//     from Assets/Unity进阶InputSystem教程/Lesson10_输入配置文件生成CSharp代码/Lesson10_输入配置文件生成CSharp代码Input配置文件.inputactions
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Utilities;

public partial class @Lesson10_输入配置文件生成CSharp代码Input配置文件: IInputActionCollection2, IDisposable
{
    public InputActionAsset asset { get; }
    public @Lesson10_输入配置文件生成CSharp代码Input配置文件()
    {
        asset = InputActionAsset.FromJson(@"{
    ""name"": ""Lesson10_输入配置文件生成CSharp代码Input配置文件"",
    ""maps"": [
        {
            ""name"": ""Action1"",
            ""id"": ""02ff5afe-ecd2-4b12-bc80-00290183aba9"",
            ""actions"": [
                {
                    ""name"": ""Fire"",
                    ""type"": ""Button"",
                    ""id"": ""16e4382b-eb33-4487-94d0-e460bef52828"",
                    ""expectedControlType"": ""Button"",
                    ""processors"": """",
                    ""interactions"": """",
                    ""initialStateCheck"": false
                },
                {
                    ""name"": ""Move"",
                    ""type"": ""Value"",
                    ""id"": ""3ce58f88-c3ed-4854-87a6-810f984acde3"",
                    ""expectedControlType"": """",
                    ""processors"": """",
                    ""interactions"": """",
                    ""initialStateCheck"": true
                }
            ],
            ""bindings"": [
                {
                    ""name"": """",
                    ""id"": ""d37ccabe-a801-4ef9-aef8-f6815debb4f6"",
                    ""path"": ""<Mouse>/leftButton"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Fire"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": false
                },
                {
                    ""name"": ""2D Vector"",
                    ""id"": ""3edd82ad-da7a-4f5b-8d99-e356c0624c2d"",
                    ""path"": ""2DVector"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Move"",
                    ""isComposite"": true,
                    ""isPartOfComposite"": false
                },
                {
                    ""name"": ""up"",
                    ""id"": ""0dd83068-282d-43e4-a19c-f66d80067d91"",
                    ""path"": ""<Keyboard>/w"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Move"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": true
                },
                {
                    ""name"": ""down"",
                    ""id"": ""0d308dae-235c-434f-a6ad-4f1af0d07f18"",
                    ""path"": ""<Keyboard>/s"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Move"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": true
                },
                {
                    ""name"": ""left"",
                    ""id"": ""df909922-9eda-44ae-b664-c9aed5b3b577"",
                    ""path"": ""<Keyboard>/a"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Move"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": true
                },
                {
                    ""name"": ""right"",
                    ""id"": ""78d0478d-9a87-4050-bbc0-b220110757cf"",
                    ""path"": ""<Keyboard>/d"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Move"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": true
                }
            ]
        },
        {
            ""name"": ""Action2"",
            ""id"": ""fff7f140-da64-40b4-ba23-bb96ca49eb23"",
            ""actions"": [
                {
                    ""name"": ""Space"",
                    ""type"": ""Button"",
                    ""id"": ""df3376c5-5676-4e22-b3d1-0ce6182a6bb1"",
                    ""expectedControlType"": ""Button"",
                    ""processors"": """",
                    ""interactions"": """",
                    ""initialStateCheck"": false
                }
            ],
            ""bindings"": [
                {
                    ""name"": """",
                    ""id"": ""d61c8e3e-2e8f-48f3-b1a5-553ba453aa77"",
                    ""path"": ""<Keyboard>/space"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Space"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": false
                }
            ]
        }
    ],
    ""controlSchemes"": []
}");
        // Action1
        m_Action1 = asset.FindActionMap("Action1", throwIfNotFound: true);
        m_Action1_Fire = m_Action1.FindAction("Fire", throwIfNotFound: true);
        m_Action1_Move = m_Action1.FindAction("Move", throwIfNotFound: true);
        // Action2
        m_Action2 = asset.FindActionMap("Action2", throwIfNotFound: true);
        m_Action2_Space = m_Action2.FindAction("Space", throwIfNotFound: true);
    }

    public void Dispose()
    {
        UnityEngine.Object.Destroy(asset);
    }

    public InputBinding? bindingMask
    {
        get => asset.bindingMask;
        set => asset.bindingMask = value;
    }

    public ReadOnlyArray<InputDevice>? devices
    {
        get => asset.devices;
        set => asset.devices = value;
    }

    public ReadOnlyArray<InputControlScheme> controlSchemes => asset.controlSchemes;

    public bool Contains(InputAction action)
    {
        return asset.Contains(action);
    }

    public IEnumerator<InputAction> GetEnumerator()
    {
        return asset.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public void Enable()
    {
        asset.Enable();
    }

    public void Disable()
    {
        asset.Disable();
    }

    public IEnumerable<InputBinding> bindings => asset.bindings;

    public InputAction FindAction(string actionNameOrId, bool throwIfNotFound = false)
    {
        return asset.FindAction(actionNameOrId, throwIfNotFound);
    }

    public int FindBinding(InputBinding bindingMask, out InputAction action)
    {
        return asset.FindBinding(bindingMask, out action);
    }

    // Action1
    private readonly InputActionMap m_Action1;
    private List<IAction1Actions> m_Action1ActionsCallbackInterfaces = new List<IAction1Actions>();
    private readonly InputAction m_Action1_Fire;
    private readonly InputAction m_Action1_Move;
    public struct Action1Actions
    {
        private @Lesson10_输入配置文件生成CSharp代码Input配置文件 m_Wrapper;
        public Action1Actions(@Lesson10_输入配置文件生成CSharp代码Input配置文件 wrapper) { m_Wrapper = wrapper; }
        public InputAction @Fire => m_Wrapper.m_Action1_Fire;
        public InputAction @Move => m_Wrapper.m_Action1_Move;
        public InputActionMap Get() { return m_Wrapper.m_Action1; }
        public void Enable() { Get().Enable(); }
        public void Disable() { Get().Disable(); }
        public bool enabled => Get().enabled;
        public static implicit operator InputActionMap(Action1Actions set) { return set.Get(); }
        public void AddCallbacks(IAction1Actions instance)
        {
            if (instance == null || m_Wrapper.m_Action1ActionsCallbackInterfaces.Contains(instance)) return;
            m_Wrapper.m_Action1ActionsCallbackInterfaces.Add(instance);
            @Fire.started += instance.OnFire;
            @Fire.performed += instance.OnFire;
            @Fire.canceled += instance.OnFire;
            @Move.started += instance.OnMove;
            @Move.performed += instance.OnMove;
            @Move.canceled += instance.OnMove;
        }

        private void UnregisterCallbacks(IAction1Actions instance)
        {
            @Fire.started -= instance.OnFire;
            @Fire.performed -= instance.OnFire;
            @Fire.canceled -= instance.OnFire;
            @Move.started -= instance.OnMove;
            @Move.performed -= instance.OnMove;
            @Move.canceled -= instance.OnMove;
        }

        public void RemoveCallbacks(IAction1Actions instance)
        {
            if (m_Wrapper.m_Action1ActionsCallbackInterfaces.Remove(instance))
                UnregisterCallbacks(instance);
        }

        public void SetCallbacks(IAction1Actions instance)
        {
            foreach (var item in m_Wrapper.m_Action1ActionsCallbackInterfaces)
                UnregisterCallbacks(item);
            m_Wrapper.m_Action1ActionsCallbackInterfaces.Clear();
            AddCallbacks(instance);
        }
    }
    public Action1Actions @Action1 => new Action1Actions(this);

    // Action2
    private readonly InputActionMap m_Action2;
    private List<IAction2Actions> m_Action2ActionsCallbackInterfaces = new List<IAction2Actions>();
    private readonly InputAction m_Action2_Space;
    public struct Action2Actions
    {
        private @Lesson10_输入配置文件生成CSharp代码Input配置文件 m_Wrapper;
        public Action2Actions(@Lesson10_输入配置文件生成CSharp代码Input配置文件 wrapper) { m_Wrapper = wrapper; }
        public InputAction @Space => m_Wrapper.m_Action2_Space;
        public InputActionMap Get() { return m_Wrapper.m_Action2; }
        public void Enable() { Get().Enable(); }
        public void Disable() { Get().Disable(); }
        public bool enabled => Get().enabled;
        public static implicit operator InputActionMap(Action2Actions set) { return set.Get(); }
        public void AddCallbacks(IAction2Actions instance)
        {
            if (instance == null || m_Wrapper.m_Action2ActionsCallbackInterfaces.Contains(instance)) return;
            m_Wrapper.m_Action2ActionsCallbackInterfaces.Add(instance);
            @Space.started += instance.OnSpace;
            @Space.performed += instance.OnSpace;
            @Space.canceled += instance.OnSpace;
        }

        private void UnregisterCallbacks(IAction2Actions instance)
        {
            @Space.started -= instance.OnSpace;
            @Space.performed -= instance.OnSpace;
            @Space.canceled -= instance.OnSpace;
        }

        public void RemoveCallbacks(IAction2Actions instance)
        {
            if (m_Wrapper.m_Action2ActionsCallbackInterfaces.Remove(instance))
                UnregisterCallbacks(instance);
        }

        public void SetCallbacks(IAction2Actions instance)
        {
            foreach (var item in m_Wrapper.m_Action2ActionsCallbackInterfaces)
                UnregisterCallbacks(item);
            m_Wrapper.m_Action2ActionsCallbackInterfaces.Clear();
            AddCallbacks(instance);
        }
    }
    public Action2Actions @Action2 => new Action2Actions(this);
    public interface IAction1Actions
    {
        void OnFire(InputAction.CallbackContext context);
        void OnMove(InputAction.CallbackContext context);
    }
    public interface IAction2Actions
    {
        void OnSpace(InputAction.CallbackContext context);
    }
}

10.3 练习题

请使用上一道小球的练习题的基础上,制作的输入配置文件,为一个球体处理移动、旋转、跳跃、开火的逻辑

配置好配置文件,生成配置文件脚本

新建脚本 把小球练习的脚本内容大致拷贝过来 注释InputAction相关 挂载到场景中

创建配置文件对象 new出来并激活

private Lesson10_练习题Input配置文件 input;
void Start()
{
    input = new Lesson10_练习题Input配置文件();
    input.Enable();
}

给配置文件对象的移动、跳跃、开火添加监听,逻辑和之前一样就行

input.Player.Jump.performed += (context) =>
{
    // 跳跃逻辑,给物体施加向上的力(利用物理系统刚体加力)
    body.AddForce(Vector3.up * 200);
};

input.Player.Fire.performed += (context) =>
{
    // 开火逻辑,通过鼠标位置的射线检测确定子弹飞出去的方向
    RaycastHit info;
    if (Physics.Raycast(Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue()), out info))
    {
        // 得到子弹飞出去的向量
        Vector3 point = info.point;
        //让y轴和小球y相等 否则可能会在地下发射
        point.y = this.transform.position.y;
        //得到子弹向量
        Vector3 dir = point - this.transform.position;
        // 创建子弹实例并飞出去
        Instantiate(bullet, this.transform.position, Quaternion.LookRotation(dir));
    }
};

dir = input.Player.Move.ReadValue<Vector2>();
dir.z = dir.y; // 将移动向量的y值赋给z值,因为我们只需要在水平方向上移动
dir.y = 0; // 将移动向量的y值设为0,保证物体不会上下移动
body.AddForce(dir); // 通过刚体组件给物体施加力,实现移动

10.4 练习题代码

Lesson10_练习题

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Lesson10_练习题 : MonoBehaviour
{
    //请使用上一道小球的练习题的基础上,制作的输入配置文件,为一个球体处理移动、旋转、跳跃、开火的逻辑




    // 声明一个公共游戏对象bullet,用于存储子弹的预制体
    public GameObject bullet;

    #region 之前InputAction的实现方式
    //[Header("移动控制")]
    //public InputAction move;
    //[Header("跳跃控制")]
    //public InputAction jump;
    //[Header("开火控制")]
    //public InputAction fire;
    #endregion



    private Rigidbody body; // 刚体组件,用于实现物体的运动
    private Vector3 dir; // 存储移动方向的向量


    private Lesson10_练习题Input配置文件 input;

    void Start()
    {
        body = this.GetComponent<Rigidbody>(); // 获取当前游戏对象的刚体组件

        #region 之前InputAction的实现方式
        //move.Enable(); // 启用移动控制输入

        //jump.Enable(); // 启用跳跃控制输入

        //fire.Enable(); // 启用开火控制输入

        //jump.performed += (context) =>
        //{
        //    // 跳跃逻辑,给物体施加向上的力(利用物理系统刚体加力)
        //    body.AddForce(Vector3.up * 200);
        //};

        //fire.performed += (context) =>
        //{
        //    // 开火逻辑,通过鼠标位置的射线检测确定子弹飞出去的方向
        //    RaycastHit info;
        //    if (Physics.Raycast(Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue()), out info))
        //    {
        //        // 得到子弹飞出去的向量
        //        Vector3 point = info.point;
        //        //让y轴和小球y相等 否则可能会在地下发射
        //        point.y = this.transform.position.y;
        //        //得到子弹向量
        //        Vector3 dir = point - this.transform.position;
        //        // 创建子弹实例并飞出去
        //        Instantiate(bullet, this.transform.position, Quaternion.LookRotation(dir));
        //    }
        //};
        #endregion


        input = new Lesson10_练习题Input配置文件();
        input.Enable();

        input.Player.Jump.performed += (context) =>
        {
            //    // 跳跃逻辑,给物体施加向上的力(利用物理系统刚体加力)
            body.AddForce(Vector3.up * 200);
        };

        input.Player.Fire.performed += (context) =>
        {
            // 开火逻辑,通过鼠标位置的射线检测确定子弹飞出去的方向
            RaycastHit info;
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue()), out info))
            {
                // 得到子弹飞出去的向量
                Vector3 point = info.point;
                //让y轴和小球y相等 否则可能会在地下发射
                point.y = this.transform.position.y;
                //得到子弹向量
                Vector3 dir = point - this.transform.position;
                // 创建子弹实例并飞出去
                Instantiate(bullet, this.transform.position, Quaternion.LookRotation(dir));
            }
        };
    }

    void Update()
    {
        #region 之前InputAction的实现方式
        //dir = move.ReadValue<Vector2>(); // 读取移动输入的值,即鼠标或键盘的输入
        //dir.z = dir.y; // 将移动向量的y值赋给z值,因为我们只需要在水平方向上移动
        //dir.y = 0; // 将移动向量的y值设为0,保证物体不会上下移动
        //body.AddForce(dir); // 通过刚体组件给物体施加力,实现移动
        #endregion

        dir = input.Player.Move.ReadValue<Vector2>();
        dir.z = dir.y; // 将移动向量的y值赋给z值,因为我们只需要在水平方向上移动
        dir.y = 0; // 将移动向量的y值设为0,保证物体不会上下移动
        body.AddForce(dir); // 通过刚体组件给物体施加力,实现移动
    }
}

Lesson10_练习题Input配置文件

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was auto-generated by com.unity.inputsystem:InputActionCodeGenerator
//     version 1.5.1
//     from Assets/Unity进阶InputSystem教程/Lesson10_练习题/Lesson10_练习题Input配置文件.inputactions
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Utilities;

public partial class @Lesson10_练习题Input配置文件: IInputActionCollection2, IDisposable
{
    public InputActionAsset asset { get; }
    public @Lesson10_练习题Input配置文件()
    {
        asset = InputActionAsset.FromJson(@"{
    ""name"": ""Lesson10_练习题Input配置文件"",
    ""maps"": [
        {
            ""name"": ""Player"",
            ""id"": ""c7a15913-f5a5-4931-ab41-6147ccd7fbd3"",
            ""actions"": [
                {
                    ""name"": ""Jump"",
                    ""type"": ""Button"",
                    ""id"": ""d5175f0a-98c2-4410-adbe-6ae633caae92"",
                    ""expectedControlType"": ""Button"",
                    ""processors"": """",
                    ""interactions"": """",
                    ""initialStateCheck"": false
                },
                {
                    ""name"": ""Move"",
                    ""type"": ""Value"",
                    ""id"": ""b55a3221-61ab-4509-a254-4d1b99a45cc5"",
                    ""expectedControlType"": """",
                    ""processors"": """",
                    ""interactions"": """",
                    ""initialStateCheck"": true
                },
                {
                    ""name"": ""Fire"",
                    ""type"": ""Button"",
                    ""id"": ""f340af02-52d8-4034-b210-023b41017e11"",
                    ""expectedControlType"": ""Button"",
                    ""processors"": """",
                    ""interactions"": """",
                    ""initialStateCheck"": false
                }
            ],
            ""bindings"": [
                {
                    ""name"": """",
                    ""id"": ""4c405465-57a8-4b66-afc6-0bf2d58e55d6"",
                    ""path"": ""<Keyboard>/space"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Jump"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": false
                },
                {
                    ""name"": ""Move"",
                    ""id"": ""60cbd141-90b6-4d2e-be8d-a75f982c4ae9"",
                    ""path"": ""2DVector"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Move"",
                    ""isComposite"": true,
                    ""isPartOfComposite"": false
                },
                {
                    ""name"": ""up"",
                    ""id"": ""b8985200-99df-4898-8063-b5ca4e4a2dab"",
                    ""path"": ""<Keyboard>/w"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Move"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": true
                },
                {
                    ""name"": ""down"",
                    ""id"": ""1aeb0711-2302-4358-905b-2877e09a42d2"",
                    ""path"": ""<Keyboard>/s"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Move"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": true
                },
                {
                    ""name"": ""left"",
                    ""id"": ""0594f6ca-fe46-4c0b-befa-8c2ea3c4869f"",
                    ""path"": ""<Keyboard>/a"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Move"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": true
                },
                {
                    ""name"": ""right"",
                    ""id"": ""ba2a3e96-6f2c-4dff-bcc4-e77c43d5d41a"",
                    ""path"": ""<Keyboard>/d"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Move"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": true
                },
                {
                    ""name"": """",
                    ""id"": ""f8554161-165c-424c-9ebc-ba15198791cc"",
                    ""path"": ""<Mouse>/rightButton"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Fire"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": false
                }
            ]
        }
    ],
    ""controlSchemes"": []
}");
        // Player
        m_Player = asset.FindActionMap("Player", throwIfNotFound: true);
        m_Player_Jump = m_Player.FindAction("Jump", throwIfNotFound: true);
        m_Player_Move = m_Player.FindAction("Move", throwIfNotFound: true);
        m_Player_Fire = m_Player.FindAction("Fire", throwIfNotFound: true);
    }

    public void Dispose()
    {
        UnityEngine.Object.Destroy(asset);
    }

    public InputBinding? bindingMask
    {
        get => asset.bindingMask;
        set => asset.bindingMask = value;
    }

    public ReadOnlyArray<InputDevice>? devices
    {
        get => asset.devices;
        set => asset.devices = value;
    }

    public ReadOnlyArray<InputControlScheme> controlSchemes => asset.controlSchemes;

    public bool Contains(InputAction action)
    {
        return asset.Contains(action);
    }

    public IEnumerator<InputAction> GetEnumerator()
    {
        return asset.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public void Enable()
    {
        asset.Enable();
    }

    public void Disable()
    {
        asset.Disable();
    }

    public IEnumerable<InputBinding> bindings => asset.bindings;

    public InputAction FindAction(string actionNameOrId, bool throwIfNotFound = false)
    {
        return asset.FindAction(actionNameOrId, throwIfNotFound);
    }

    public int FindBinding(InputBinding bindingMask, out InputAction action)
    {
        return asset.FindBinding(bindingMask, out action);
    }

    // Player
    private readonly InputActionMap m_Player;
    private List<IPlayerActions> m_PlayerActionsCallbackInterfaces = new List<IPlayerActions>();
    private readonly InputAction m_Player_Jump;
    private readonly InputAction m_Player_Move;
    private readonly InputAction m_Player_Fire;
    public struct PlayerActions
    {
        private @Lesson10_练习题Input配置文件 m_Wrapper;
        public PlayerActions(@Lesson10_练习题Input配置文件 wrapper) { m_Wrapper = wrapper; }
        public InputAction @Jump => m_Wrapper.m_Player_Jump;
        public InputAction @Move => m_Wrapper.m_Player_Move;
        public InputAction @Fire => m_Wrapper.m_Player_Fire;
        public InputActionMap Get() { return m_Wrapper.m_Player; }
        public void Enable() { Get().Enable(); }
        public void Disable() { Get().Disable(); }
        public bool enabled => Get().enabled;
        public static implicit operator InputActionMap(PlayerActions set) { return set.Get(); }
        public void AddCallbacks(IPlayerActions instance)
        {
            if (instance == null || m_Wrapper.m_PlayerActionsCallbackInterfaces.Contains(instance)) return;
            m_Wrapper.m_PlayerActionsCallbackInterfaces.Add(instance);
            @Jump.started += instance.OnJump;
            @Jump.performed += instance.OnJump;
            @Jump.canceled += instance.OnJump;
            @Move.started += instance.OnMove;
            @Move.performed += instance.OnMove;
            @Move.canceled += instance.OnMove;
            @Fire.started += instance.OnFire;
            @Fire.performed += instance.OnFire;
            @Fire.canceled += instance.OnFire;
        }

        private void UnregisterCallbacks(IPlayerActions instance)
        {
            @Jump.started -= instance.OnJump;
            @Jump.performed -= instance.OnJump;
            @Jump.canceled -= instance.OnJump;
            @Move.started -= instance.OnMove;
            @Move.performed -= instance.OnMove;
            @Move.canceled -= instance.OnMove;
            @Fire.started -= instance.OnFire;
            @Fire.performed -= instance.OnFire;
            @Fire.canceled -= instance.OnFire;
        }

        public void RemoveCallbacks(IPlayerActions instance)
        {
            if (m_Wrapper.m_PlayerActionsCallbackInterfaces.Remove(instance))
                UnregisterCallbacks(instance);
        }

        public void SetCallbacks(IPlayerActions instance)
        {
            foreach (var item in m_Wrapper.m_PlayerActionsCallbackInterfaces)
                UnregisterCallbacks(item);
            m_Wrapper.m_PlayerActionsCallbackInterfaces.Clear();
            AddCallbacks(instance);
        }
    }
    public PlayerActions @Player => new PlayerActions(this);
    public interface IPlayerActions
    {
        void OnJump(InputAction.CallbackContext context);
        void OnMove(InputAction.CallbackContext context);
        void OnFire(InputAction.CallbackContext context);
    }
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com

×

喜欢就点赞,疼爱就打赏