在Unity的新输入系统中获取操作映射的GUID。

huangapple go评论79阅读模式
英文:

Getting GUID of Action Map in Unitys New Input System

问题

我开始深入研究在Unity的新输入系统中使用多个操作映射,以更好地分离不同部分游戏输入的方式。我目前正在创建在运行时更改活动操作映射的逻辑,作为其中的一部分,我希望获得对我在InputActionAsset中创建的操作映射的引用。

查看操作映射的文档,我看到这些操作映射可以使用与其名称匹配的字符串引用,或使用操作映射的GUID来引用。我倾向于尽量避免使用字符串引用,因为我希望能够在将来重命名这些操作映射而不破坏我的逻辑,但是我很难找到任何描述如何在操作映射之外获取这些GUID的资源。

我找到的一种获取这些GUID的方法是将PlayerInput组件的DefaultMap设置为所需的操作映射,然后使用以下代码在临时的MonoBehaviour中打印GUID:

[SerializeField] private PlayerInput _playerInput;

private void Awake()
{
    Debug.Log(_playerInput.currentActionMap.id);
}

但这感觉非常不正规,我需要为将来创建的每个操作映射重复这个过程。我假设有一种更好的直接获取这些GUID的方法,但从我迄今为止尝试的一切中,我都无法找到。有人知道如何做到这一点吗?

谢谢你的帮助!

英文:

I am starting to delve into using multiple action maps in Unity's new input system to better separate the input for different parts of my game. I am currently working on creating the logic for changing the active action map at runtime, and as part of this, I am hoping to get a reference to the action maps that I have created on my InputActionAsset.

Looking into the documentation for action maps, I saw that these action maps can be referred to using a string matching its name, or with a GUID for the action map. I tend to avoid using string references as much as possible as I want to ability to rename these action maps in the future without breaking my logic, but I am having a hard time finding any resources describing how to get the GUID of these action maps outside of a hacky workaround.

One way that I have found to get these GUIDs is to set the DefaultMap of the PlayerInput component to the desired action map, then have a temporary MonoBehaviour print the GUID using:

[SerializeField] private PlayerInput _playerInput;

private void Awake()
{
    Debug.Log(_playerInput.currentActionMap.id);
}

but this feels very hacky and I would need to repeat this for every action map I create in the future. I am assuming that there is a better way of getting these GUIDs directly but from everything I've tried so far I have been unable to find it. Does anybody know of a way of doing this?

Thank you for the help!

答案1

得分: 0

对于将来找到这个问题的任何人,答案比我想象的更简单,但我仍然觉得奇怪的是在Unity文档中没有提到这一点。

要找到动作映射(Action Maps)和输入动作(Input Actions)的GUID,您首先必须在项目视图中选择InputActionAsset,然后在检查器中确保选中了“Generate C# Class”框。

然后,您只需打开为此InputActionAsset创建的C#类,并在该文件中将会包含动作映射的GUID。

感谢@BugFinder指导我朝着正确的方向前进,现在我拥有了我所希望的功能!

英文:

For anyone finding this in the future, the answer was more straightforward than I thought, but I still find it strange that there was no mention of this in the Unity documentation.

To find the GUID's for Action Maps, as well as Input Actions, you must first select the InputActionAsset in the project view, and then in the inspector make sure that the "Generate C# Class" box is checked.

Generate C# Class option for InputActionAsset

You then simply need to open the C# class that was created for this InputActionAsset, and within that file it will have the GUIDs for the action maps.

Thank you to @BugFinder for pointing me in the right direction, I now have the functionality that I was hoping for!

答案2

得分: 0

我终于找到了我希望的理想实现,这要感谢@derHugo的建议。根据他们的建议,如果你为InputActionAsset生成一个C#类,那么在运行时你只需要创建这个C#类的一个实例,然后使用该引用来访问有关动作映射的任何数据。

private void Awake()
{
    InGameInputAction inGameInputAction = new InGameInputAction();
    InputActionMap characterInputActionMap = inGameInputAction.CharacterInputActionMap;
}

这正是我所期望的功能,完全消除了访问GUID的需要。

再次感谢@derHugo向我指出这一点,它节省了大量不必要的GUID手动缓存,现在代码更加清晰!

我希望这些信息能够更容易地在Unity的新输入系统文档中找到,因为这对我来说并不是很明显,尽管我花了相当多的时间搜索这个功能,但最终我以为使用GUID是实现这一目标的唯一方法。

英文:

I finally found the ideal implementation I was hoping for thanks to advice from @derHugo. As per their recommendation, if you generate a C# class for the InputActionAsset, all you need to do is create an instance of this C# class during runtime and then use that reference for accessing any data about action maps.

private void Awake()
{
	InGameInputAction inGameInputAction = new InGameInputAction();
	InputActionMap characterInputActionMap = inGameInputAction.CharacterInputActionMap;
}

This is exactly the functionality that I was hoping for and completely removes the need to access GUIDs at all.

Thank you again @derHugo for pointing this out to me, it has saved a lot of unnecessary manual caching of GUIDs and things are much cleaner now!

I would love for this information to be available somewhere more easily accessible in Unity's documentation of the new input system, as this was not very obvious to me, and though I spent a decent amount of time searching for this functionality I ended up assuming that using GUIDs was the only way of accomplishing this.

huangapple
  • 本文由 发表于 2023年7月14日 05:19:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定