Unity使用新输入系统InputSystem制作飞机大战Demo

请添加图片描述

@作者 : SYFStrive

@博客首页 : HomePage

📌:个人社区(欢迎大佬们加入) 👉:社区链接🔗

📌:觉得文章不错可以点点关注 👉:专栏连接🔗

💃:程序员每天坚持锻炼💪

请添加图片描述

相关专栏

👉
飞机大战专栏(🔥)

目录

  • UI背景制作
  • 添加Quad
  • 实现背景移动
  • 创建材质球添加对应的背景图片
  • 利用材质球的偏移量使背景动起来
  • 效果
  • 新输入系统使用InputSystem
  • InputActions与角色串联起来(关键代码PlayerInput)
  •    PlayerInput
  • 使玩家移动
  • Player
  • 玩家与新输入系统绑定
  • 最后
  • UI背景制作

    添加Quad

    使用Unity创建3D Object 👉 Quad

    如 👇

    实现背景移动

    注意❗:背景图片的WrapMode模式改为 Repeat

    如 👇

    创建材质球添加对应的背景图片

    利用材质球的偏移量使背景动起来

    代码如 👇

    //private显示
    [SerializeField] Vector2 bgVecMove;
    
    //获取背景材质组件
    private Material material;
    
    private void Awake()
    {
        //获取material组件
        material = GetComponent<MeshRenderer>().material;
    }
    
    private void Update()
    {
        //利用材质球的偏移量使背景移动
        material.mainTextureOffset+=bgVecMove * Time.deltaTime;   
    }
    

    效果

    新输入系统使用InputSystem

    InputSystem详细使用可以至我的博客👉 链接 学习

    下载步骤:Window 👉 Package Manager 👉 输入InputSystem

    创建步骤:右键 👉 Create 👉 InputActions

    如 👇


    添加键的说明如 👇

    InputActions与角色串联起来(关键代码PlayerInput)

       PlayerInput

    实现共能: 添加系统事件 👉 使系统事件与新输入系统绑定

    脚本如 👇

    using UnityEngine;
    using UnityEngine.Events;
    using UnityEngine.InputSystem;
    
    [CreateAssetMenu(menuName ="Player Input")]
    public class PlayerInput : ScriptableObject, InputActions.IGameplayActions
    {
    
        //使用系统事件
        private event UnityAction<Vector2> onMove; //= delegate { }判空的1种方法(一开始初始化)
        private event UnityAction onStopMove;
    
        //初始化实例InputAction
        private InputActions inputActions;
    
    
        private void Awake()
        {
            //实例InputAction
            inputActions=new InputActions();    
            //登记动作表的回调函数 如我现在只有一个动作表就登记一个
            inputActions.Gameplay.SetCallbacks(this);   
        }
    
        //Play时执行
        private void OnEnable()
        {
            
        }
    
        //推出游戏执行
        private void OnDisable()
        {
            EntranceSceneInhibitoryInput();
        }
    
    
        /// <summary>
        /// 进入场景时禁止输入设备一系列操作
        /// </summary>
        private void EntranceSceneInhibitoryInput()
        {
            //禁止输入
            inputActions.Gameplay.Disable();
        }
    
    
        /// <summary>
        /// 限制鼠标与输入设备输入
        /// </summary>
        private void AstrictImport()
        {
            //理解:操作玩家 👉 启动动作表
            inputActions.Gameplay.Enable();
    
            //确定硬件指针是否可见。
            Cursor.visible = false;
    
            //Cursor.lockState对应的参数如 👇三种
            //1、不加锁定
            //CursorLockMode.None;
            //2、锁定光标
            //CursorLockMode.Locked;
            //3、将光标限制在屏幕内
            //CursorLockMode.Confined;
            Cursor.lockState = CursorLockMode.Locked;
        }
    
        public void OnMove(InputAction.CallbackContext context)
        {
            //按下及按住时执行
            if (context.phase == InputActionPhase.Performed)
            {
                //执行事件
                //判断空的第二种方法
                //if(onMove!=null) 
    
                //判断空的第三种方法
                //传入输入动作读取到的二维向量的值 👉 将其作为参数 
                //当按下按键的时候 调用onMove方法 👉 同时将读取到的二维向量值作为参数
                onMove?.Invoke(context.ReadValue<Vector2>()); 
            }
    
            //当松开按键时执行
            if(context.phase== InputActionPhase.Canceled)
            {
                //执行事件
                onStopMove?.Invoke();
            }
        }
    }
    

    使玩家移动

    Player

    实现共能:初始化时绑定事件 与 游戏停止时移除事件(控制玩家的移动)

    代码如 👇

    using UnityEngine;
    
    //运行时自动添加Rigidbody组件
    [RequireComponent(typeof(Rigidbody2D))]
    public class Player : MonoBehaviour
    {
        //private显示
        [SerializeField]PlayerInput playerInput;
    
        //获取刚体
        private new Rigidbody2D rigidbody;
    
        //移动的速度
        [SerializeField] float moveSpeed=10;
    
        private void Awake()
        {
            //获取刚体组件
            rigidbody=GetComponent<Rigidbody2D>();    
        }
        private void Start()
        {
            //初始化重力为0
            rigidbody.gravityScale = 0f;
            //初始化激活动作表
            playerInput.AstrictImport();
        }
    
        private void OnEnable()
        {
            //订阅事件
            playerInput.onMove += Move;
            playerInput.onStopMove += StopMove;
        }
    
        private void OnDisable()
        {
            //移除事件
            playerInput.onMove -= Move;
            playerInput.onStopMove -= StopMove;
        }
    
        /// <summary>
        /// 移动
        /// </summary>
        private void Move(Vector2 moveInput)
        {
            //好理解先接受移动的值
            var moveSpeedS = moveInput * moveSpeed;
            //刚体移动
            rigidbody.velocity= moveSpeedS;
        }
    
        /// <summary>
        /// 停止移动
        /// </summary>
        private void StopMove()
        {
            //刚体移动
            rigidbody.velocity = Vector3.zero;
        }
    }
    

    玩家与新输入系统绑定

    绑定如 👇

    实现效果如 👇

    最后


    本文到这里就结束了,大佬们的支持是我持续更新的最大动力,希望这篇文章能帮到大家💪

     

                     相关专栏连接🔗

    下篇文章再见ヾ( ̄▽ ̄)ByeBye

    物联沃分享整理
    物联沃-IOTWORD物联网 » Unity使用新输入系统InputSystem制作飞机大战Demo

    发表评论