gpt4 book ai didi

c# - 将 EventSystem 用于按键事件

转载 作者:太空狗 更新时间:2023-10-29 21:31:59 25 4
gpt4 key购买 nike

上下文:假设您正在检查键盘上是否按下了“W”,最常见的检查方法是通过以下代码:

void Update(){
if (Input.GetKeyDown("W"))
DoSomething();
}

有没有办法使用 Unity 的 EventSystem 执行以下操作?换句话说,是否有像 IPointerClickHandler 这样的接口(interface)的实现,例如,检查按钮是否被按下,而不是在 Update() 函数中这样做?

最佳答案

Is there a way to do the following, using Unity's EventSystem? In other words, is there an implementation of an interface like IPointerClickHandler,

没有。 EventSystem 主要用于光线转换和调度事件。这不用于检测键盘事件。 EventSystem 中唯一可以检测键盘事件的组件是 InputField 组件。仅此而已,不能用于任何其他用途。

Check whether a button is pressed, without doing so in an Update() function?

是的,Event.KeyboardEvent 有一种方法,这需要 OnGUI功能。

void OnGUI()
{
if (Event.current.Equals(Event.KeyboardEvent("W")))
{
print("W pressed!");
}
}

这比将 Input.GetKeyDown 函数与 Update 函数一起使用更糟糕。我鼓励您坚持使用 Input.GetKeyDown。没有任何问题。


如果您正在寻找事件类型 InputSystem没有 Input.GetKeyDown 然后使用 Unity 的新输入 API 并订阅 InputSystem.onEvent 事件。

如果您正在寻找类似于 IPointerClickHandler 接口(interface)的功能,您可以在 Input.GetKeyDown 之上实现它。

1。首先,使用 System.Enum.GetValues(typeof(KeyCode)); 获取所有 KeyCode 枚举并将其存储在一个数组。

2.创建一个接口(interface)“IKeyboardEvent”,并在IPointerClickHandler中添加OnKeyDown等函数,就像OnPointerClick > 界面。

3.循环遍历 #1KeyCode 并检查数组中的每个键是否被按下、释放或按住。

4.获取场景中的所有组件,检查它们是否实现了IKeyboardEvent接口(interface)。如果是,则根据 #3 中的键状态在界面中调用适当的函数。

这是一个仍然可以扩展或改进的功能示例:

附加到一个空的游戏对象。

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

public class KeyboardEventSystem : MonoBehaviour
{
Array allKeyCodes;

private static List<Transform> allTransforms = new List<Transform>();
private static List<GameObject> rootGameObjects = new List<GameObject>();

void Awake()
{
allKeyCodes = System.Enum.GetValues(typeof(KeyCode));
}

void Update()
{
//Loop over all the keycodes
foreach (KeyCode tempKey in allKeyCodes)
{
//Send event to key down
if (Input.GetKeyDown(tempKey))
senEvent(tempKey, KeybrdEventType.keyDown);

//Send event to key up
if (Input.GetKeyUp(tempKey))
senEvent(tempKey, KeybrdEventType.KeyUp);

//Send event to while key is held down
if (Input.GetKey(tempKey))
senEvent(tempKey, KeybrdEventType.down);

}
}


void senEvent(KeyCode keycode, KeybrdEventType evType)
{
GetAllRootObject();
GetAllComponents();

//Loop over all the interfaces and callthe appropriate function
for (int i = 0; i < allTransforms.Count; i++)
{
GameObject obj = allTransforms[i].gameObject;

//Invoke the appropriate interface function if not null
IKeyboardEvent itfc = obj.GetComponent<IKeyboardEvent>();
if (itfc != null)
{
if (evType == KeybrdEventType.keyDown)
itfc.OnKeyDown(keycode);
if (evType == KeybrdEventType.KeyUp)
itfc.OnKeyUP(keycode);
if (evType == KeybrdEventType.down)
itfc.OnKey(keycode);
}
}
}

private static void GetAllRootObject()
{
rootGameObjects.Clear();

Scene activeScene = SceneManager.GetActiveScene();
activeScene.GetRootGameObjects(rootGameObjects);
}


private static void GetAllComponents()
{
allTransforms.Clear();

for (int i = 0; i < rootGameObjects.Count; ++i)
{
GameObject obj = rootGameObjects[i];

//Get all child Transforms attached to this GameObject
obj.GetComponentsInChildren<Transform>(true, allTransforms);
}
}

}

public enum KeybrdEventType
{
keyDown,
KeyUp,
down
}

public interface IKeyboardEvent
{
void OnKeyDown(KeyCode keycode);
void OnKeyUP(KeyCode keycode);
void OnKey(KeyCode keycode);
}

用法:

像使用 IPointerClickHandler 一样,在脚本中实现 IKeyboardEvent 接口(interface)及其函数。

public class test : MonoBehaviour, IKeyboardEvent
{
public void OnKey(KeyCode keycode)
{
Debug.Log("Key held down: " + keycode);
}

public void OnKeyDown(KeyCode keycode)
{
Debug.Log("Key pressed: " + keycode);
}

public void OnKeyUP(KeyCode keycode)
{
Debug.Log("Key released: " + keycode);
}
}

关于c# - 将 EventSystem 用于按键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50471129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com