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