首页 > 其他分享 >Unity PIco事件封装

Unity PIco事件封装

时间:2023-12-06 13:44:06浏览次数:33  
标签:封装 picoKseyEvent PicoKeyEvent Unity PIco new using CommonUsages public

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.XR;
public interface IPicoKey<T>
{
    public void TryGetKey(PicoKeyEvent<T> picoKseyEvent, InputFeatureUsage<T> inputFeature);
}
public class PicoKeyEvent<T> 
{
    public UnityEvent<T> KeyDownEvent = new UnityEvent<T>();
    public UnityEvent<T> KeyPressingEvent = new UnityEvent<T>();
    public UnityEvent<bool> KeyUpEvent = new UnityEvent<bool>();

    public UnityEvent<bool> KeyAxisUpEvent = new UnityEvent<bool>();
    public UnityEvent<bool> KeyAxisDownEvent = new UnityEvent<bool>();
    public UnityEvent<bool> KeyAxisLeftEvent = new UnityEvent<bool>();
    public UnityEvent<bool> KeyAxisRightEvent = new UnityEvent<bool>();
    private XRNode m_node;
    private InputDevice m_InputDevide;

    private InputFeatureUsage<T> m_InputFeatureUsageValue;
    public  T m_Value;


    private bool Axis_2D = false;


    
    public bool isPressDown = false;
    public bool isPressing = false;


    public bool isUpPressDown = false;
    public bool isUpPressing = false;

    public bool isDownPressDown = false;
    public bool isDownPressing = false;
    public PicoKeyEvent(XRNode node, InputFeatureUsage<T> inputFeatureUsage)
    {
        m_node = node;
        m_InputDevide = InputDevices.GetDeviceAtXRNode(node);
        m_InputFeatureUsageValue = inputFeatureUsage;
        m_Value = default(T);
        Axis_2D = !m_Value.GetType().Equals(typeof(bool));
    }



    public void RegistButton()
    {

    }

    public InputFeatureUsage<T> GetFeatureUsage()
    {
        return m_InputFeatureUsageValue;
    }
    

    public XRNode GetXRNode()
    {
        return m_node;
    }

    public InputDevice GetInputDevide()
    {
        return m_InputDevide; 
    }

    public ref T GetRefValue()
    {
        return ref m_Value; 
    }

    public T GetValue()
    {
        return m_Value;
    }

以上是PIco事件基本的结构

 

 

using Pico.Platform.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Resources;
using TMPro;
using Unity.VisualScripting;
using Unity.XR.CoreUtils;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
using UnityEngine.XR;
using UnityEngine.XR.Interaction.Toolkit;
using static UnityEngine.XR.Interaction.Toolkit.XRRayInteractor;

[SerializeField]
public enum PicoSwapType
{
    North,
    South,
    West,
    East,
    None
}

public interface ISceneSetting
{
    public void LimitMoveAndTurnSnap(bool limitMove,bool limitSnapTurn);
}
public class GameManager : MonoBehaviour, IPicoKey<bool>, IPicoKey<Vector2>
{


    #region PicoEvent
    //=====================RIGHT HAND KEY EVENT=============
    public PicoKeyEvent<bool> R_GripBtn;
    public PicoKeyEvent<bool> R_TriggerBtn;
    public PicoKeyEvent<bool> R_PrimaryBtn;    public PicoKeyEvent<bool> R_SecondaryBtn;
    public PicoKeyEvent<Vector2> R_primary2DAxisBtn;
    public Action<PicoSwapType> R_OnSwapEvent;
    //=======================================================

    //=====================LEFT HAND KEY EVENT=============
    public PicoKeyEvent<bool> L_GripBtn;
    public PicoKeyEvent<bool> L_TriggerBtn;
    public PicoKeyEvent<bool> L_PrimaryBtn;
    public PicoKeyEvent<bool> L_SecondaryBtn;
    public PicoKeyEvent<Vector2> L_primary2DAxisBtn;
    public Action<PicoSwapType> L_OnSwapEvent;
    //=======================================================


    #endregion

    
    private void Awake()
    {
        InitPicoKeyEvent();
}
    void Update()
{
          UpdateCheckPicoKeyEvent();
}
    /// <summary>
    /// 更新事件状态
    /// </summary>
    public void UpdateCheckPicoKeyEvent()
    {

        TryGetKey(R_SecondaryBtn, CommonUsages.secondaryButton);
        TryGetKey(R_PrimaryBtn, CommonUsages.primaryButton);
        TryGetKey(R_GripBtn, CommonUsages.gripButton);
        TryGetKey(R_TriggerBtn, CommonUsages.triggerButton);
        TryGetKey(R_primary2DAxisBtn, CommonUsages.primary2DAxis);

        TryGetKey(L_SecondaryBtn, CommonUsages.secondaryButton);
        TryGetKey(L_PrimaryBtn, CommonUsages.primaryButton);
        TryGetKey(L_GripBtn, CommonUsages.gripButton);
        TryGetKey(L_TriggerBtn, CommonUsages.triggerButton);
        TryGetKey(L_primary2DAxisBtn, CommonUsages.primary2DAxis);
    }

    public void TryGetKey(PicoKeyEvent<bool> picoKseyEvent, InputFeatureUsage<bool> inputFeature)
    {
        if (picoKseyEvent.GetInputDevide().TryGetFeatureValue(inputFeature, out picoKseyEvent.GetRefValue()))
        {
            if (picoKseyEvent.GetValue())
            {
                if (!picoKseyEvent.isPressDown)
                {
                    picoKseyEvent.isPressDown = true;
                    picoKseyEvent.KeyDownEvent?.Invoke(true);
                }
                else
                {
                    picoKseyEvent.isPressing = true;
                    picoKseyEvent.KeyPressingEvent?.Invoke(true);
                }
            }
            else
            {
                picoKseyEvent.KeyUpEvent?.Invoke(true);
                picoKseyEvent.isPressDown = false;
                picoKseyEvent.isPressing = false;
            }
        }
        else if (!picoKseyEvent.GetValue() && (picoKseyEvent.isPressDown || picoKseyEvent.isPressing))
        {
            picoKseyEvent.KeyUpEvent?.Invoke(true);
        }
    }

    public void TryGetKey(PicoKeyEvent<Vector2> picoKseyEvent, InputFeatureUsage<Vector2> inputFeature)
    {
        if (picoKseyEvent.GetInputDevide().TryGetFeatureValue(inputFeature, out picoKseyEvent.GetRefValue()))
        {
            if (picoKseyEvent.GetValue() != Vector2.zero)
            {
                if (!picoKseyEvent.isPressDown)
                {
                    picoKseyEvent.isPressDown = true;
                    picoKseyEvent.KeyDownEvent?.Invoke(picoKseyEvent.GetValue());
                }
                else
                {
                    picoKseyEvent.isPressing = true;
                    picoKseyEvent.KeyPressingEvent?.Invoke(picoKseyEvent.GetValue());
                }
            }
            else
            {
                picoKseyEvent.KeyUpEvent?.Invoke(true);
                picoKseyEvent.isPressDown = false;
                picoKseyEvent.isPressing = false;
            }
        }
        else if (picoKseyEvent.GetValue() == Vector2.zero && (picoKseyEvent.isPressDown || picoKseyEvent.isPressing))
        {
            picoKseyEvent.KeyUpEvent?.Invoke(true);
        }
    }

    /// <summary>
    /// 初始化pico的按钮事件
    /// </summary>
    private void InitPicoKeyEvent()
    {
        R_primary2DAxisBtn = new PicoKeyEvent<Vector2>(XRNode.RightHand, CommonUsages.primary2DAxis);
        R_SecondaryBtn = new PicoKeyEvent<bool>(XRNode.RightHand, CommonUsages.secondaryButton);
        R_PrimaryBtn = new PicoKeyEvent<bool>(XRNode.RightHand, CommonUsages.primaryButton);
        R_GripBtn = new PicoKeyEvent<bool>(XRNode.RightHand, CommonUsages.gripButton);
        R_TriggerBtn = new PicoKeyEvent<bool>(XRNode.RightHand, CommonUsages.triggerButton);

        L_primary2DAxisBtn = new PicoKeyEvent<Vector2>(XRNode.LeftHand, CommonUsages.primary2DAxis);
        L_SecondaryBtn = new PicoKeyEvent<bool>(XRNode.LeftHand, CommonUsages.secondaryButton);
        L_PrimaryBtn = new PicoKeyEvent<bool>(XRNode.LeftHand, CommonUsages.primaryButton);
        L_GripBtn = new PicoKeyEvent<bool>(XRNode.LeftHand, CommonUsages.gripButton);
        L_TriggerBtn = new PicoKeyEvent<bool>(XRNode.LeftHand, CommonUsages.triggerButton);
    }

  

标签:封装,picoKseyEvent,PicoKeyEvent,Unity,PIco,new,using,CommonUsages,public
From: https://www.cnblogs.com/WantPeach/p/17879318.html

相关文章

  • ICEE-将SiC/GaS功率MOSFET与应用电路集成封装的IC系列
    BM2SCQ124T-LBZ@ROHM内置1700VSiC-MOSFET的准谐振AC/DC转换器BM2SCQ124T-LBZ是一款准谐振AC/DC转换器,为所有带插座的设备提供很好的电源系统。采用准谐振工作方式,实现软开关,有助于降低EMI。内置1700V/4ASiCMOSFET,有助于设计简化。通过外部连接电流检测电阻,可以实现高度灵活......
  • vue3 之 封装hooks
    注意:使用Hooks来做的话,需要封装一个以use开头的函数,自定义Hooks有一个潜规则,就是要use开头一、相关链接①已经封装好可直接使用的:https://vueuse.org/core/useMounted/② 为什么要在Vue3中多使用Hooks?好处是啥?: https://zhua......
  • Unity Transform接口的几个常用方法解析_unity基础开发教程
    UnityTransform接口常用方法解析1.Transform.position2.Transform.right、Transform.forward、Transform.up3.Transform.Rotate4.Transform.Translate在Unity中,Transform类是游戏对象位置、旋转和缩放的表示。在日常开发中我们回经常用到Transform接口的几个常用方法,这些方......
  • 封装与模块化
    数据封装是面向对象编程的基本准则:使用封装隐藏对象内部的状态。所有的通信都通过对象的方法来实现。访问修饰符:1.private(私有):仅限对象本身内部访问常用于修饰属性2.public(公开):可以任意访问常用于修饰方法classPerson{privateStringname;......
  • quickjs模块封装、类封装
    一、模块封装、类封装通过quickjs,封装JS模块,然后可以通过import导入对应模块,还可以导入具体的类。全部代码如下。1#include"quickjs-libc.h"2#include<stdio.h>3#include<inttypes.h>4#include<string.h>5#include"cutils.h"67staticJSC......
  • Unity底层是如何处理C#的
    在面试中,我们经常会被问到Unity的底层是如何处理C#,本节给通过一下3个点来给大家详细的分析这个问题:(1)C#的发展历史;(2)Unity为什么用C#;(3)il2cpp解决了什么问题;对惹,这里有一个游戏开发交流小组,希望大家可以点击进来一起交流一下开发经验呀C#的发展历史C#没有出来之前......
  • Unity DOTS系列之托管/非托管Component的区别与性能分析
    最近DOTS发布了正式的版本,我们来分享一下DOTS里面托管与非托管Component的区别与性能分析,方便大家上手学习掌握UnityDOTS开发。托管与非托管的区别在于是不是基于自动垃圾回收的。托管是由垃圾回收器来负责自动回收,非托管需要我们手动来做相关内存管理,不被垃圾回收系统来处理。......
  • Unity DOTS系列之System中如何使用SystemAPI.Query迭代数据
    最近DOTS发布了正式的版本,我们来分享一下System中如何基于SystemAPI.Query来迭代World中的数据,方便大家上手学习掌握UnityDOTS开发。SystemAPI.Query的使用System有两种,一种是Unmanaged的ISystem,一种是managed的SystemBase,这两种System都可以通过SystemAPI.Query来迭代与......
  • 微信小程序 wx.request Typescript 封装统一请求
    话不多说直接上代码,想懂的终究会懂,哈哈哈哈文件名:request.ts 1/**2*HttpMethod类型api处要用3*/4exportenumHttpMethod{5Get="GET",6Post="POST",7Options="OPTIONS",8Put="PUT",9Delete=......
  • stack和queue的底层容器封装 以及提供随机存储的容器
    在C++中,std::stack和std::queue是容器适配器,它们提供了特定的接口,依赖于某个容器类(如std::deque或std::list)来处理元素1。std::stack:std::stack默认使用std::deque作为其底层容器2。但是,你也可以在创建std::stack对象时指定其他的底层容器,只要这个容器支持......