A game about forced loneliness, made by TACStudios
1// Note: If not UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS we do not use a custom property drawer and
2// picker for InputActionReferences but rather rely on default (classic) object picker.
3#if UNITY_EDITOR && UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
4using UnityEditor;
5using UnityEditor.Search;
6
7namespace UnityEngine.InputSystem.Editor
8{
9 /// <summary>
10 /// Custom property drawer in order to use the "Advanced Picker" from UnityEditor.Search.
11 /// </summary>
12 [CustomPropertyDrawer(typeof(InputActionReference))]
13 internal sealed class InputActionReferencePropertyDrawer : PropertyDrawer
14 {
15 private readonly SearchContext m_Context = UnityEditor.Search.SearchService.CreateContext(new[]
16 {
17 InputActionReferenceSearchProviders.CreateInputActionReferenceSearchProviderForAssets(),
18 InputActionReferenceSearchProviders.CreateInputActionReferenceSearchProviderForProjectWideActions(),
19 }, string.Empty, SearchConstants.PickerSearchFlags);
20
21
22 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
23 {
24 // Sets the property to null if the action is not found in the asset.
25 ValidatePropertyWithDanglingInputActionReferences(property);
26
27 ObjectField.DoObjectField(position, property, typeof(InputActionReference), label,
28 m_Context, SearchConstants.PickerViewFlags);
29 }
30
31 static void ValidatePropertyWithDanglingInputActionReferences(SerializedProperty property)
32 {
33 if (property?.objectReferenceValue is InputActionReference reference)
34 {
35 // Check only if the reference is a project-wide action.
36 if (reference?.asset == InputSystem.actions)
37 {
38 var action = reference?.asset?.FindAction(reference.action.id);
39 if (action is null)
40 {
41 property.objectReferenceValue = null;
42 property.serializedObject.ApplyModifiedProperties();
43 }
44 }
45 }
46 }
47 }
48}
49
50#endif