暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SerializedPropertyLinqExtensions.cs 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. namespace UnityEngine.InputSystem.Editor
  6. {
  7. internal static class SerializedPropertyLinqExtensions
  8. {
  9. public static IEnumerable<T> Select<T>(this SerializedProperty property, Func<SerializedProperty, T> selector)
  10. {
  11. if (property == null)
  12. throw new ArgumentNullException(nameof(property));
  13. if (selector == null)
  14. throw new ArgumentNullException(nameof(selector));
  15. if (property.isArray == false)
  16. yield break;
  17. for (var i = 0; i < property.arraySize; i++)
  18. {
  19. yield return selector(property.GetArrayElementAtIndex(i));
  20. }
  21. }
  22. public static IEnumerable<SerializedProperty> Where(this SerializedProperty property,
  23. Func<SerializedProperty, bool> predicate)
  24. {
  25. if (property == null)
  26. throw new ArgumentNullException(nameof(property));
  27. if (predicate == null)
  28. throw new ArgumentNullException(nameof(predicate));
  29. if (property.isArray == false)
  30. yield break;
  31. for (var i = 0; i < property.arraySize; i++)
  32. {
  33. var element = property.GetArrayElementAtIndex(i);
  34. if (predicate(element))
  35. yield return element;
  36. }
  37. }
  38. public static SerializedProperty FindLast(this SerializedProperty property, Func<SerializedProperty, bool> predicate)
  39. {
  40. Debug.Assert(predicate != null, "Missing predicate for FindLast function.");
  41. Debug.Assert(property != null, "SerializedProperty missing for FindLast function.");
  42. if (property.isArray == false)
  43. return null;
  44. for (int i = property.arraySize - 1; i >= 0; i--)
  45. {
  46. var element = property.GetArrayElementAtIndex(i);
  47. if (predicate(element))
  48. return element;
  49. }
  50. return null;
  51. }
  52. public static SerializedProperty FirstOrDefault(this SerializedProperty property)
  53. {
  54. if (property == null)
  55. throw new ArgumentNullException(nameof(property));
  56. if (property.isArray == false || property.arraySize == 0)
  57. return null;
  58. return property.GetArrayElementAtIndex(0);
  59. }
  60. public static SerializedProperty FirstOrDefault(this SerializedProperty property,
  61. Func<SerializedProperty, bool> predicate)
  62. {
  63. if (property == null)
  64. throw new ArgumentNullException(nameof(property));
  65. if (predicate == null)
  66. throw new ArgumentNullException(nameof(predicate));
  67. if (property.isArray == false)
  68. return null;
  69. for (var i = 0; i < property.arraySize; i++)
  70. {
  71. var arrayElementAtIndex = property.GetArrayElementAtIndex(i);
  72. if (predicate(arrayElementAtIndex) == false)
  73. continue;
  74. return arrayElementAtIndex;
  75. }
  76. return null;
  77. }
  78. public static IEnumerable<SerializedProperty> Skip(this SerializedProperty property, int count)
  79. {
  80. if (property == null)
  81. throw new ArgumentNullException(nameof(property));
  82. if (count < 0)
  83. throw new ArgumentOutOfRangeException(nameof(count));
  84. return SkipIterator(property, count);
  85. }
  86. public static IEnumerable<SerializedProperty> Take(this SerializedProperty property, int count)
  87. {
  88. if (property == null)
  89. throw new ArgumentNullException(nameof(property));
  90. if (count < 0 || count > property.arraySize)
  91. throw new ArgumentOutOfRangeException(nameof(count));
  92. return TakeIterator(property, count);
  93. }
  94. private static IEnumerable<SerializedProperty> SkipIterator(SerializedProperty source, int count)
  95. {
  96. var enumerator = source.GetEnumerator();
  97. while (count > 0 && enumerator.MoveNext()) count--;
  98. if (count <= 0)
  99. {
  100. while (enumerator.MoveNext())
  101. yield return (SerializedProperty)enumerator.Current;
  102. }
  103. }
  104. private static IEnumerable<SerializedProperty> TakeIterator(SerializedProperty source, int count)
  105. {
  106. if (count > 0)
  107. {
  108. foreach (SerializedProperty element in source)
  109. {
  110. yield return element;
  111. if (--count == 0) break;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. #endif