Skip to content

Commit 89a370c

Browse files
committed
Revert "CHANGE: Performance improvement - removed project-wide asset migration code (#2025)"
This reverts commit ba9677b.
1 parent 94c260e commit 89a370c

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,25 @@ internal static class ProjectWideActionsAsset
1717

1818
internal static class ProjectSettingsProjectWideActionsAssetConverter
1919
{
20+
private const string kAssetPathInputManager = "ProjectSettings/InputManager.asset";
21+
private const string kAssetNameProjectWideInputActions = "ProjectWideInputActions";
22+
2023
class ProjectSettingsPostprocessor : AssetPostprocessor
2124
{
25+
private static bool migratedInputActionAssets = false;
26+
2227
#if UNITY_2021_2_OR_NEWER
2328
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload)
2429
#else
2530
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
2631
#endif
2732
{
33+
if (!migratedInputActionAssets)
34+
{
35+
MoveInputManagerAssetActionsToProjectWideInputActionAsset();
36+
migratedInputActionAssets = true;
37+
}
38+
2839
if (!Application.isPlaying)
2940
{
3041
// If the Library folder is deleted, InputSystem will fail to retrieve the assigned Project-wide Asset because this look-up occurs
@@ -35,6 +46,69 @@ private static void OnPostprocessAllAssets(string[] importedAssets, string[] del
3546
}
3647
}
3748
}
49+
50+
private static void MoveInputManagerAssetActionsToProjectWideInputActionAsset()
51+
{
52+
var objects = AssetDatabase.LoadAllAssetsAtPath(EditorHelpers.GetPhysicalPath(kAssetPathInputManager));
53+
if (objects == null)
54+
return;
55+
56+
var inputActionsAsset = objects.FirstOrDefault(o => o != null && o.name == kAssetNameProjectWideInputActions) as InputActionAsset;
57+
if (inputActionsAsset != default)
58+
{
59+
// Found some actions in the InputManager.asset file
60+
//
61+
string path = ProjectWideActionsAsset.kDefaultAssetPath;
62+
63+
if (File.Exists(EditorHelpers.GetPhysicalPath(path)))
64+
{
65+
// We already have a path containing inputactions, find a new unique filename
66+
//
67+
// eg Assets/InputSystem_Actions.inputactions ->
68+
// Assets/InputSystem_Actions (1).inputactions ->
69+
// Assets/InputSystem_Actions (2).inputactions ...
70+
//
71+
string[] files = Directory.GetFiles("Assets", "*.inputactions");
72+
List<string> names = new List<string>();
73+
for (int i = 0; i < files.Length; i++)
74+
{
75+
names.Add(System.IO.Path.GetFileNameWithoutExtension(files[i]));
76+
}
77+
string unique = ObjectNames.GetUniqueName(names.ToArray(), kDefaultAssetName);
78+
path = "Assets/" + unique + ".inputactions";
79+
}
80+
81+
var json = inputActionsAsset.ToJson();
82+
InputActionAssetManager.SaveAsset(EditorHelpers.GetPhysicalPath(path), json);
83+
84+
Debug.Log($"Migrated Project-wide Input Actions from '{kAssetPathInputManager}' to '{path}' asset");
85+
86+
// Update current project-wide settings if needed (don't replace if already set to something else)
87+
//
88+
if (InputSystem.actions == null || InputSystem.actions.name == kAssetNameProjectWideInputActions)
89+
{
90+
InputSystem.actions = (InputActionAsset)AssetDatabase.LoadAssetAtPath(path, typeof(InputActionAsset));
91+
Debug.Log($"Loaded Project-wide Input Actions from '{path}' asset");
92+
}
93+
}
94+
95+
// Handle deleting all InputActionAssets as older 1.8.0 pre release could create more than one project wide input asset in the file
96+
foreach (var obj in objects)
97+
{
98+
if (obj is InputActionReference)
99+
{
100+
var actionReference = obj as InputActionReference;
101+
AssetDatabase.RemoveObjectFromAsset(obj);
102+
Object.DestroyImmediate(actionReference);
103+
}
104+
else if (obj is InputActionAsset)
105+
{
106+
AssetDatabase.RemoveObjectFromAsset(obj);
107+
}
108+
}
109+
110+
AssetDatabase.SaveAssets();
111+
}
38112
}
39113

40114
// Returns the default asset path for where to create project-wide actions asset.

0 commit comments

Comments
 (0)