unity-atoms/Source/Extensions/ListExtensions.cs

177 lines
6.1 KiB
C#
Raw Normal View History

Squashed commit of the following: commit 847eff037204d841546c0da772d2f341f9cf1e25 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sun Mar 17 22:09:38 2019 +0100 #17 - Serializable not needed for ScriptableObject commit 593b275e6394b9d589de8a7a20375145dfc4aa84 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sun Mar 17 21:59:33 2019 +0100 18 - IGameEvent<T1, T2> RegisterListener commit 40443ce9bd4b1c339aaf19cfcb119f2336608dae Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:35:04 2019 +0100 Remove some more warnings commit bd453110ac51a6ebe3d54368fcb039bdbe52e278 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:33:30 2019 +0100 Update README commit 90977b853a047c84efb5311768a09f4e8a1165b2 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:31:45 2019 +0100 Initialize to null to get rid off warnings commit 38b7f5c4ede195aa7198f567801c9cbeedc9b6f6 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:15:53 2019 +0100 More fixes to enable local unity project commit 404e1cbf88ed4431c61d3ece074e838e74ac5141 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:12:37 2019 +0100 Remove duplicated asmdef commit 5734300684e8a16553f213157cad1b4722b7cb7f Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:09:46 2019 +0100 Change files to include commit d1e42b119a6bc1577b1792459fa298e063652337 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 16:05:56 2019 +0100 Added root package json commit 1709a0347147d74460f653182bbaf8d15eb6154e Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 15:30:44 2019 +0100 #16 - Add test and examples Unity project commit a3ea1a133bf6727e011ba85c64569db45302e487 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 13:12:02 2019 +0100 #13 - Make usage of UPM (package manager) commit 492a30e905f6cf3f5899cb7080ef2bda73110f00 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 11:28:56 2019 +0100 Added extensions + code formatting fixes commit 709949a1016c236cfd363cf25392fedfd8d083ca Author: Oliver Biwer <soraphis@users.noreply.github.com> Date: Fri Mar 8 10:16:45 2019 +0100 More AtomicTags changes (#15) * - added assembly defintions, and unit tests - improved AtomicTags in regards of #8, #9 and #10 * Fixes #11 - Added Equality Members (inclusive HashCode) for ScriptableObjectVariableBase * removed Rider Plugins from git * Further AtomicTag optimization commit ae6584c879f182e727fe0a8d0aff4b0715829914 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 10:08:36 2019 +0100 Editor config commit 197d7067608600e4e2d13dc42db909ee8f8c75df Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Mar 8 09:23:12 2019 +0100 Added editor config file commit 53d6adc07bbc2967c12c17227a1c31d9f1cfba77 Author: Oliver Biwer <soraphis@users.noreply.github.com> Date: Tue Mar 5 22:57:47 2019 +0100 More efficient AtomicTags (#12) * - added assembly defintions, and unit tests - improved AtomicTags in regards of #8, #9 and #10 * Fixes #11 - Added Equality Members (inclusive HashCode) for ScriptableObjectVariableBase * removed Rider Plugins from git commit 81209d83b5195300d4c2d54411cff3c7983f0d97 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Wed Dec 12 20:54:17 2018 +0100 Added MonoHooks + ColliderType + bug fixes commit c6b240cebbdc410341fb05204235842df5da9d73 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Sat Dec 1 00:23:10 2018 +0100 Experimenting with adding UI state management commit dfd70a8944acbeabe5feba1cf6cff2be6802c470 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Nov 30 23:10:21 2018 +0100 Issue #6 - AtomicTags commit 8907763227f4d4c2a32c5684e7caa4d4a082eb16 Author: Adam Ramberg <andersson.adam.89@gmail.com> Date: Fri Nov 30 22:42:29 2018 +0100 First commit of v1.0.0
2019-03-17 18:43:20 -04:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityAtoms.Extensions
{
// Create an Action delegates that takes 5 parameters. Not included in the standard lib.
public delegate void Action<T1, T2, T3, T4, T5>(T1 p1, T2 p2, T3 p3, T4 p4, T5 p5);
public static class ListExtensions
{
public static void ForEach<T, P1>(this List<T> list, Action<T, P1> action, P1 param1)
{
for (int i = 0; list != null && i < list.Count; ++i)
{
action(list[i], param1);
}
}
public static void ForEach<T, P1, P2>(this List<T> list, Action<T, P1, P2> action, P1 param1, P2 param2)
{
for (int i = 0; list != null && i < list.Count; ++i)
{
action(list[i], param1, param2);
}
}
public static void ForEach<T, P1, P2, P3>(this List<T> list, Action<T, P1, P2, P3> action, P1 param1, P2 param2, P3 param3)
{
for (int i = 0; list != null && i < list.Count; ++i)
{
action(list[i], param1, param2, param3);
}
}
public static void ForEach<T, V, P1>(this List<T> list, Func<T, V> selector, Action<V, P1> action, P1 param1)
{
for (int i = 0; list != null && i < list.Count; ++i)
{
action(selector(list[i]), param1);
}
}
public static void ForEach<T, V, P1, P2>(this List<T> list, Func<T, V> selector, Action<V, P1, P2> action, P1 param1, P2 param2)
{
for (int i = 0; list != null && i < list.Count; ++i)
{
action(selector(list[i]), param1, param2);
}
}
public static void ForEach<T, V, P1, P2, P3>(this List<T> list, Func<T, V> selector, Action<V, P1, P2, P3> action, P1 param1, P2 param2, P3 param3)
{
for (int i = 0; list != null && i < list.Count; ++i)
{
action(selector(list[i]), param1, param2, param3);
}
}
public static void ForEach<T>(this List<T> list, Action<T, int> action)
{
for (int i = 0; list != null && i < list.Count; ++i)
{
action(list[i], i);
}
}
public static void ForEach<T, P1>(this List<T> list, Action<T, int, P1> action, P1 param1)
{
for (int i = 0; list != null && i < list.Count; ++i)
{
action(list[i], i, param1);
}
}
public static void ForEach<T, P1, P2>(this List<T> list, Action<T, int, P1, P2> action, P1 param1, P2 param2)
{
for (int i = 0; list != null && i < list.Count; ++i)
{
action(list[i], i, param1, param2);
}
}
public static void ForEach<T, P1, P2, P3>(this List<T> list, Action<T, int, P1, P2, P3> action, P1 param1, P2 param2, P3 param3)
{
for (int i = 0; list != null && i < list.Count; ++i)
{
action(list[i], i, param1, param2, param3);
}
}
public static R Reduce<R, T>(this List<T> list, Func<R, R, R> reducer, Func<T, R> getValue, R initialValue, Func<T, bool> skip = null)
{
R accumulator = initialValue;
for (int i = 0; list != null && i < list.Count; ++i)
{
if (skip != null && skip(list[i]))
{
continue;
}
accumulator = reducer(accumulator, getValue(list[i]));
}
return accumulator;
}
public static float ReturnMaxFloat(float acc, float cur) { return Mathf.Max(acc, cur); }
public static float ReturnMinFloat(float acc, float cur) { return Mathf.Min(acc, cur); }
public static T First<T>(this List<T> list, Func<T, bool> func)
{
for (int i = 0; list != null && i < list.Count; ++i)
{
if (func(list[i])) return list[i];
}
return default(T);
}
public static bool Some<T>(this List<T> list, Func<T, bool> func)
{
return EqualityComparer<T>.Default.Equals(list.First(func), default(T));
}
public static bool AddIfNotExists<T>(this List<T> list, T item)
{
if (!list.Contains(item))
{
list.Add(item);
return true;
}
return false;
}
public static T GetOrInstantiate<T>(this List<T> list, UnityEngine.Object prefab, Vector3 position, Quaternion quaternion, Func<T, bool> condition) where T : UnityEngine.Component
{
var component = list.First(condition);
if (component != null)
{
component.transform.position = position;
component.transform.rotation = quaternion;
return component;
}
var newComponent = GameObject.Instantiate(prefab, position, quaternion) as T;
list.Add(newComponent);
return newComponent;
}
public static GameObject GetOrInstantiate(this List<GameObject> list, UnityEngine.Object prefab, Vector3 position, Quaternion quaternion, Func<GameObject, bool> condition)
{
var component = list.First(condition);
if (component != null)
{
component.transform.position = position;
component.transform.rotation = quaternion;
return component;
}
var newGameObject = GameObject.Instantiate(prefab, position, quaternion) as GameObject;
list.Add(newGameObject);
return newGameObject;
}
public static T InstantiateAndAdd<T>(this List<T> list, UnityEngine.Object prefab, Vector3 position, Quaternion quaternion) where T : UnityEngine.Component
{
var component = GameObject.Instantiate(prefab, position, quaternion) as T;
list.Add(component);
return component;
}
}
}