mirror of
https://github.com/unity-atoms/unity-atoms.git
synced 2025-01-24 09:08:23 -05:00
943451b3d5
* Add abstract modifier to all generic classes meant not to be instantiated directly as the intent is to specify a pattern for a closed type that can be serialized. * Added sealed keyword to all closed types. This is an incidental performance improvement for mobile and desktop platforms utilizing il2cpp due to the way unsealed vs sealed virtual methods are handled in generated C++ code. * Also sealed all inspectors to mark them as closed types. * Dropped all where constraints to the next line as most of these class declarations break 120-130 characters. This should help improve readability and make this style consistent as it has been in use for the most-complex generic types already, but not for others. * Dropped all generic type parameters (generally) numbering three or more to the next line to improve readability. * Where found, added empty lines between property/field declarations in classes to improve readability. * Extracted several 2x event classes into their own files to match convention established by other event classes. * Removed unecessary meta files for C# solution. * Added compiler options file (csc.rsp) file for the Unity project and added global warning ignore for 0649 (Field 'field' is never assigned to, and will always have its default value 'value'). This is necessary since 2018.3 switched the compiler to Roslyn and by design they have decided to not suppress this warning for monobehavior fields that are private, but serialized. This is very widely contested as most developers will not make every field needed to be assigned in the Editor as public (breaks encapsulation, opens surface area for bugs) and this can in some cases generate hundreds if not thousands of warnings. Adding the compiler options file suppresses this warning which also may hide legitimate warnings, but is far the lesser of two evils. * Moved example scripts not in a namespace to UnityAtoms.Examples. * Reordered public/private fields and properties to be consistent. Order is as follows; public and private properties followed by public/private fields. * Renamed private fields to use '_' prefix and marked public fields as private where only used internally. Marked these fields with FormerlySerializedAs attribute to preserve older serialized values already in use. * Removed redundant initialization of null to reference types as this is their default value. * Marked implicitly private methods and members without an access modifier as explicitly private. * Updated unit tests to use new private name field when getting private member field info.
129 lines
4.2 KiB
C#
129 lines
4.2 KiB
C#
using UnityEngine;
|
|
using System;
|
|
|
|
namespace UnityAtoms.Extensions
|
|
{
|
|
public static class TransformExtensions
|
|
{
|
|
/* Finds a child to this transform by name. Searches not only the first level in the
|
|
* tree hierarchy of child objects, but all the children, grand children, and so on.
|
|
*/
|
|
public static Transform FindDeepChild(this Transform parent, string name)
|
|
{
|
|
var result = parent.Find(name);
|
|
|
|
if (result != null)
|
|
return result;
|
|
|
|
for (int i = 0; i < parent.childCount; ++i)
|
|
{
|
|
result = parent.GetChild(i).FindDeepChild(name);
|
|
if (result != null)
|
|
return result;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/* Traverse all the children of the transform and executes the action on this transform,
|
|
* as well as on all the children.
|
|
*/
|
|
public static void TraverseAndExecute(this Transform current, Action<Transform> action)
|
|
{
|
|
action(current);
|
|
|
|
for (int i = 0; i < current.childCount; ++i)
|
|
{
|
|
current.GetChild(i).TraverseAndExecute(action);
|
|
}
|
|
}
|
|
|
|
/* Traverse all the children of the transform and executes the func on this transform,
|
|
* as well as on all the children. Will return true if all of the funcs returns true.
|
|
*/
|
|
public static bool TraverseExecuteAndCheck(this Transform current, Func<Transform, bool> func)
|
|
{
|
|
bool ret = func(current);
|
|
|
|
for (int i = 0; i < current.childCount; ++i)
|
|
{
|
|
var temp = current.GetChild(i).TraverseExecuteAndCheck(func);
|
|
if (!temp)
|
|
ret = false;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
public static void ForEachChild(this Transform transform, Action<Transform> action)
|
|
{
|
|
for (int i = 0; i < transform.childCount; ++i)
|
|
{
|
|
action(transform.GetChild(i));
|
|
}
|
|
}
|
|
|
|
public static void ForEachChild<P1>(this Transform transform, Action<Transform, P1> action, P1 param1)
|
|
{
|
|
for (int i = 0; i < transform.childCount; ++i)
|
|
{
|
|
action(transform.GetChild(i), param1);
|
|
}
|
|
}
|
|
|
|
public static void ForEachChild<P1, P2>(this Transform transform, Action<Transform, P1, P2> action, P1 param1, P2 param2)
|
|
{
|
|
for (int i = 0; i < transform.childCount; ++i)
|
|
{
|
|
action(transform.GetChild(i), param1, param2);
|
|
}
|
|
}
|
|
|
|
public static void ForEachChild<P1, P2, P3>(this Transform transform, Action<Transform, P1, P2, P3> action, P1 param1, P2 param2, P3 param3)
|
|
{
|
|
for (int i = 0; i < transform.childCount; ++i)
|
|
{
|
|
action(transform.GetChild(i), param1, param2, param3);
|
|
}
|
|
}
|
|
|
|
public static void ForEachChild(this Transform transform, Action<Transform, int> action)
|
|
{
|
|
for (int i = 0; i < transform.childCount; ++i)
|
|
{
|
|
action(transform.GetChild(i), i);
|
|
}
|
|
}
|
|
|
|
public static void ForEachChild<P1>(this Transform transform, Action<Transform, int, P1> action, P1 param1)
|
|
{
|
|
for (int i = 0; i < transform.childCount; ++i)
|
|
{
|
|
action(transform.GetChild(i), i, param1);
|
|
}
|
|
}
|
|
|
|
public static void ForEachChild<P1, P2>(this Transform transform, Action<Transform, int, P1, P2> action, P1 param1, P2 param2)
|
|
{
|
|
for (int i = 0; i < transform.childCount; ++i)
|
|
{
|
|
action(transform.GetChild(i), i, param1, param2);
|
|
}
|
|
}
|
|
|
|
public static void ForEachChild<P1, P2, P3>(this Transform transform, Action<Transform, int, P1, P2, P3> action, P1 param1, P2 param2, P3 param3)
|
|
{
|
|
for (int i = 0; i < transform.childCount; ++i)
|
|
{
|
|
action(transform.GetChild(i), i, param1, param2, param3);
|
|
}
|
|
}
|
|
|
|
public static Transform AddParent(this Transform transform, Transform parent)
|
|
{
|
|
transform.parent = parent;
|
|
return transform;
|
|
}
|
|
}
|
|
}
|