Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InputActionReferencePropertyDrawer.cs 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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
  4. using UnityEditor;
  5. using UnityEditor.Search;
  6. namespace UnityEngine.InputSystem.Editor
  7. {
  8. /// <summary>
  9. /// Custom property drawer in order to use the "Advanced Picker" from UnityEditor.Search.
  10. /// </summary>
  11. [CustomPropertyDrawer(typeof(InputActionReference))]
  12. internal sealed class InputActionReferencePropertyDrawer : PropertyDrawer
  13. {
  14. private readonly SearchContext m_Context = UnityEditor.Search.SearchService.CreateContext(new[]
  15. {
  16. InputActionReferenceSearchProviders.CreateInputActionReferenceSearchProviderForAssets(),
  17. InputActionReferenceSearchProviders.CreateInputActionReferenceSearchProviderForProjectWideActions(),
  18. }, string.Empty, SearchConstants.PickerSearchFlags);
  19. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  20. {
  21. // Sets the property to null if the action is not found in the asset.
  22. ValidatePropertyWithDanglingInputActionReferences(property);
  23. ObjectField.DoObjectField(position, property, typeof(InputActionReference), label,
  24. m_Context, SearchConstants.PickerViewFlags);
  25. }
  26. static void ValidatePropertyWithDanglingInputActionReferences(SerializedProperty property)
  27. {
  28. if (property?.objectReferenceValue is InputActionReference reference)
  29. {
  30. // Check only if the reference is a project-wide action.
  31. if (reference?.asset == InputSystem.actions)
  32. {
  33. var action = reference?.asset?.FindAction(reference.action.id);
  34. if (action is null)
  35. {
  36. property.objectReferenceValue = null;
  37. property.serializedObject.ApplyModifiedProperties();
  38. }
  39. }
  40. }
  41. }
  42. }
  43. }
  44. #endif