Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Assets/Tests/InputSystem/CoreTests_Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4939,6 +4939,7 @@ public void Actions_CanLookUpActionInMap()

var action1 = map.AddAction("action1");
var action2 = map.AddAction("action2");
var yawPitch = map.AddAction("Yaw/Pitch");

Assert.That(map.FindAction("action1"), Is.SameAs(action1));
Assert.That(map.FindAction("action2"), Is.SameAs(action2));
Expand All @@ -4947,6 +4948,10 @@ public void Actions_CanLookUpActionInMap()
// Lookup is case-insensitive.
Assert.That(map.FindAction("Action1"), Is.SameAs(action1));
Assert.That(map.FindAction("Action2"), Is.SameAs(action2));

// Lookup allows '/' within action name (https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1306)
// yawPitchAction = _actions.FindAction("Yaw/Pitch");
Assert.That(map.FindAction("Yaw/Pitch"), Is.SameAs(yawPitch));
}

[Test]
Expand Down Expand Up @@ -8106,6 +8111,7 @@ public void Actions_CanLookUpActionInAssetByName()
var action1 = map1.AddAction("action1");
var action2 = map1.AddAction("action2");
var action3 = map2.AddAction("action3");
var action4 = map2.AddAction("Yaw/Pitch");

Assert.That(asset.FindAction("action1"), Is.SameAs(action1));
Assert.That(asset.FindAction("action2"), Is.SameAs(action2));
Expand All @@ -8119,6 +8125,10 @@ public void Actions_CanLookUpActionInAssetByName()
Assert.That(asset.FindAction($"{{{action2.id.ToString()}}}"), Is.SameAs(action2));
Assert.That(asset.FindAction($"{{{action3.id.ToString()}}}"), Is.SameAs(action3));

// Lookup allows '/' within action name (https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1306)
// yawPitchAction = _actions.FindAction("Yaw/Pitch");
Assert.That(asset.FindAction("Yaw/Pitch"), Is.SameAs(action4));

// Shouldn't allocate.
var map1action1 = "map1/action1";
Assert.That(() =>
Expand All @@ -8127,6 +8137,26 @@ public void Actions_CanLookUpActionInAssetByName()
}, Is.Not.AllocatingGCMemory());
}

// https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1306
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, links could be put into a Description attribute:

[Description("See https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1306")]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fair change to this test since specific to reported use-case

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in b78188b

[Test]
[Category("Actions")]
public void Actions_CanLookUpActionInAssetByNameIfHavingActionAndMapActionWithSameName()
{
var asset = ScriptableObject.CreateInstance<InputActionAsset>();

var map1 = new InputActionMap("map1");
var map2 = new InputActionMap("Yaw");

asset.AddActionMap(map1);
asset.AddActionMap(map2);

var action1 = map1.AddAction("Yaw/Pitch");
var action2 = map2.AddAction("Pitch");

// map/action should be selected first
Assert.That(asset.FindAction("Yaw/Pitch"), Is.SameAs(action2));
}

// Since we allow looking up by action name without any map qualification, ambiguities result when several
// actions are named the same. We choose to not do anything special other than generally preferring an
// enabled action over a disabled one. Other than that, we just return the first hit.
Expand Down
1 change: 1 addition & 0 deletions Packages/com.unity.inputsystem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ however, it has to be formatted properly to pass verification tests.
- Fixed an issue on macOS which didn't detect up-left DPAD presses for Xbox controllers. [ISXB-810](https://issuetracker.unity3d.com/issues/macos-d-pad-upper-left-corner-is-not-logged-with-the-xbox-controller)
- Fixed Input Actions code generation overwriting user files when the names happened to match. [ISXB-1257](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1257)
- Fixed an issue when providing JoinPlayer with a specific split screen index. [ISXB-897](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-897)
- Fixed an issue where an action with a name containing a slash "/" could not be found via `InputActionAsset.FindAction(string,bool)`. [ISXB-1306](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1306).

## [1.14.0] - 2025-03-20

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public static InputActionAsset FromJson(string json)
/// in the asset.
/// </summary>
/// <param name="actionNameOrId">Name of the action as either a "map/action" combination (e.g. "gameplay/fire") or
/// a simple name. In the former case, the name is split at the '/' slash and the first part is used to find
/// a simple name e.g. "fire". In the former case, the name is split at the '/' slash and the first part is used to find
/// a map with that name and the second part is used to find an action with that name inside the map. In the
/// latter case, all maps are searched in order and the first action that has the given name in any of the maps
/// is returned. Note that name comparisons are case-insensitive.
Expand All @@ -491,6 +491,10 @@ public static InputActionAsset FromJson(string json)
/// An exception is if, of the multiple actions with the same name, some are enabled and some are disabled. In
/// this case, the first action that is enabled is returned.
///
/// If an action name contains a slash "/", e.g. "yaw/pitch" and there is also a map called "yaw" which
/// contains an action "pitch", the action "pitch" within the map "jump" will be returned instead of the
/// action named "yaw/pitch".
///
/// <example>
/// <code>
/// var asset = ScriptableObject.CreateInstance&lt;InputActionAsset&gt;();
Expand Down Expand Up @@ -533,28 +537,11 @@ public InputAction FindAction(string actionNameOrId, bool throwIfNotFound = fals

if (m_ActionMaps != null)
{
// Check if we have a "map/action" path.
// Check if we have a "map/action" path. If we do we either has a "map/action" path or a simple
// action name containing a slash. We first attempt matching it to a "map/action" and only if that
// fails do we attempt to search for a "some/action" name.
var indexOfSlash = actionNameOrId.IndexOf('/');
if (indexOfSlash == -1)
{
// No slash so it's just a simple action name. Return either first enabled action or, if
// none are enabled, first action with the given name.
InputAction firstActionFound = null;
for (var i = 0; i < m_ActionMaps.Length; ++i)
{
var action = m_ActionMaps[i].FindAction(actionNameOrId);
if (action != null)
{
if (action.enabled || action.m_Id == actionNameOrId) // Match by ID is always exact.
return action;
if (firstActionFound == null)
firstActionFound = action;
}
}
if (firstActionFound != null)
return firstActionFound;
}
else
if (indexOfSlash >= 0)
{
// Have a path. First search for the map, then for the action.
var mapName = new Substring(actionNameOrId, 0, indexOfSlash);
Expand Down Expand Up @@ -583,6 +570,23 @@ public InputAction FindAction(string actionNameOrId, bool throwIfNotFound = fals
break;
}
}

// It's just a simple action name. Return either first enabled action or, if
// none are enabled, first action with the given name.
InputAction firstActionFound = null;
for (var i = 0; i < m_ActionMaps.Length; ++i)
{
var action = m_ActionMaps[i].FindAction(actionNameOrId);
if (action != null)
{
if (action.enabled || action.m_Id == actionNameOrId) // Match by ID is always exact.
return action;
if (firstActionFound == null)
firstActionFound = action;
}
}
if (firstActionFound != null)
return firstActionFound;
}

if (throwIfNotFound)
Expand Down