Skip to content

Commit 9d20997

Browse files
committed
Update InputActionReferences of project-wide actions on save
1 parent 0e8a81e commit 9d20997

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionReference.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public override string ToString()
160160
return base.ToString();
161161
}
162162

163-
private static string GetDisplayName(InputAction action)
163+
internal static string GetDisplayName(InputAction action)
164164
{
165165
return !string.IsNullOrEmpty(action?.actionMap?.name) ? $"{action.actionMap?.name}/{action.name}" : action?.name;
166166
}

Packages/com.unity.inputsystem/InputSystem/Editor/ProjectWideActions/ProjectWideActionsAsset.cs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,16 @@ private static InputActionAsset CreateNewActionAsset()
8686
}
8787
}
8888

89-
// Create sub-asset for each action. This is so that users can select individual input actions from the asset when they're
90-
// trying to assign to a field that accepts only one action.
89+
CreateInputActionReferences(asset);
90+
91+
AssetDatabase.SaveAssets();
92+
93+
return asset;
94+
}
95+
96+
private static void CreateInputActionReferences(InputActionAsset asset)
97+
{
98+
var maps = asset.actionMaps;
9199
foreach (var map in maps)
92100
{
93101
foreach (var action in map.actions)
@@ -97,10 +105,51 @@ private static InputActionAsset CreateNewActionAsset()
97105
AssetDatabase.AddObjectToAsset(actionReference, asset);
98106
}
99107
}
108+
}
100109

101-
AssetDatabase.SaveAssets();
110+
/// <summary>
111+
/// Updates the input action references in the asset by updating names, removing dangling references
112+
/// and adding new ones.
113+
/// </summary>
114+
/// <param name="asset"></param>
115+
internal static void UpdateInputActionReferences()
116+
{
117+
var asset = GetOrCreate();
118+
var existingReferences = InputActionImporter.LoadInputActionReferencesFromAsset(asset).ToList();
102119

103-
return asset;
120+
// Check if referenced input action exists in the asset and remove the reference if it doesn't.
121+
foreach (var actionReference in existingReferences)
122+
{
123+
var action = asset.FindAction(actionReference.action.id);
124+
if (action == null)
125+
{
126+
actionReference.Set(null);
127+
AssetDatabase.RemoveObjectFromAsset(actionReference);
128+
}
129+
}
130+
131+
// Check if all actions have a reference
132+
foreach (var action in asset)
133+
{
134+
var actionReference = existingReferences.FirstOrDefault(r => r.m_ActionId == action.id.ToString());
135+
// The input action doesn't have a reference, create a new one.
136+
if (actionReference == null)
137+
{
138+
var actionReferenceNew = ScriptableObject.CreateInstance<InputActionReference>();
139+
actionReferenceNew.Set(action);
140+
AssetDatabase.AddObjectToAsset(actionReferenceNew, asset);
141+
}
142+
else
143+
{
144+
// Update the name of the reference if it doesn't match the action name.
145+
if (actionReference.name != InputActionReference.GetDisplayName(action))
146+
{
147+
AssetDatabase.RemoveObjectFromAsset(actionReference);
148+
actionReference.name = InputActionReference.GetDisplayName(action);
149+
AssetDatabase.AddObjectToAsset(actionReference, asset);
150+
}
151+
}
152+
}
104153
}
105154
}
106155
}

Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindowUtils.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#if UNITY_EDITOR && UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
22
using System.IO;
3+
using System.Linq;
34
using UnityEditor;
45
using UnityEngine.UIElements;
56

@@ -14,10 +15,13 @@ internal class InputActionsEditorWindowUtils
1415
public static void SaveAsset(SerializedObject serializedAsset)
1516
{
1617
var asset = (InputActionAsset)serializedAsset.targetObject;
17-
// for the global actions asset: save differently (as it is a yaml file and not a json)
18+
// For project-wide actions asset save works differently. The asset is in YAML format, not JSON.
1819
if (asset.name == ProjectWideActionsAsset.kAssetName)
1920
{
21+
ProjectWideActionsAsset.UpdateInputActionReferences();
2022
AssetDatabase.SaveAssets();
23+
24+
var existingReferences = InputActionImporter.LoadInputActionReferencesFromAsset(ProjectWideActionsAsset.GetOrCreate()).ToList();
2125
return;
2226
}
2327
var assetPath = AssetDatabase.GetAssetPath(asset);

0 commit comments

Comments
 (0)