diff --git a/Editor/BetterExtensions.Editor.asmdef b/Editor/BetterExtensions.Editor.asmdef index 741a00a..f7ea45d 100644 --- a/Editor/BetterExtensions.Editor.asmdef +++ b/Editor/BetterExtensions.Editor.asmdef @@ -2,6 +2,7 @@ "name": "BetterExtensions.Editor", "rootNamespace": "Better.Extensions.EditorAddons", "references": [ + "GUID:6adbd2a12d24f5d429f7ea1b1086e835", "GUID:01df13aca8d01e24a911bcc3e8277031", "GUID:28da8d3b12e3efa47928e0c9070f853d" ], diff --git a/Runtime/Extensions/ListExtensions.cs b/Runtime/Extensions/ListExtensions.cs new file mode 100644 index 0000000..ccd7de4 --- /dev/null +++ b/Runtime/Extensions/ListExtensions.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; + +namespace Better.Extensions.Runtime +{ + public static class ListExtensions + { + public static bool RemoveRange(this List self, IEnumerable range) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return false; + } + + if (range == null) + { + DebugUtility.LogException(nameof(range)); + return false; + } + + var anyRemoved = false; + foreach (var item in range) + { + if (self.Remove(item)) + { + anyRemoved = true; + } + } + + return anyRemoved; + } + + public static void AddRangeDistinct(this List self, IEnumerable range) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return; + } + + if (range == null) + { + DebugUtility.LogException(nameof(range)); + return; + } + + foreach (var item in range) + { + if (self.Contains(item)) continue; + self.Add(item); + } + } + + public static bool TrimToCount(this List self, int count) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return false; + } + + count = Math.Max(count, 0); + + if (self.Count > count) + { + var difference = self.Count - count; + self.RemoveRange(count, difference); + return true; + } + + return false; + } + } +} \ No newline at end of file diff --git a/Runtime/Extensions/ListExtensions.cs.meta b/Runtime/Extensions/ListExtensions.cs.meta new file mode 100644 index 0000000..4d71aa1 --- /dev/null +++ b/Runtime/Extensions/ListExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: eddeabac5dee447e8ae5525f96d3baa3 +timeCreated: 1709927050 \ No newline at end of file diff --git a/package.json b/package.json index 9d832e9..83970e0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.uurha.betterextensions", "displayName": "Better Extensions", - "version": "1.5.95", + "version": "1.5.96", "unity": "2021.3", "description": "Unity extensions, serialize extension, async extension, string extension and UI extensions", "dependencies": {