Skip to content

Commit

Permalink
Update UnityExtensions
Browse files Browse the repository at this point in the history
Adds some new ones, converts others to use arrays instead of lists
  • Loading branch information
Insprill committed Aug 8, 2023
1 parent dc7d9e6 commit a3415f7
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions Multiplayer/Utils/UnityExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using UnityEngine;
Expand All @@ -17,38 +18,59 @@ public static GameObject NewChild(this GameObject parent, string name)
};
}

[CanBeNull]
public static bool HasChildWithName(this Component parent, string name)
{
return parent.gameObject.HasChildWithName(name);
}

[NotNull]
public static GameObject FindChildByName(this Component parent, string name)
{
return parent.gameObject.FindChildByName(name);
}

[CanBeNull]
public static bool HasChildWithName(this GameObject parent, string name)
{
return FindChildrenByName(parent, name).Length > 0;
}

[NotNull]
public static GameObject FindChildByName(this GameObject parent, string name)
{
Transform child = parent.transform.FindChildByName(name);
return child == null ? null : child.gameObject;
return parent.transform.FindChildByName(name).gameObject;
}

[CanBeNull]
[NotNull]
public static Transform FindChildByName(this Transform parent, string name)
{
return FindChildrenByName(parent, name).FirstOrDefault();
return FindChildrenByName(parent, name).FirstOrDefault() ?? throw new NullReferenceException($"Failed to find child {name} in {parent.name}");
}

[NotNull]
public static List<GameObject> FindChildrenByName(this Component parent, string name)
public static GameObject[] FindChildrenByName(this Component parent, string name)
{
return FindChildrenByName(parent.gameObject, name);
}

[NotNull]
public static List<GameObject> FindChildrenByName(this GameObject parent, string name)
public static GameObject[] FindChildrenByName(this GameObject parent, string name)
{
List<Transform> transforms = FindChildrenByName(parent.transform, name);
List<GameObject> gameObjects = new(transforms.Count);
foreach (Transform t in transforms)
gameObjects.Add(t.gameObject);
GameObject[] gameObjects = new GameObject[transforms.Count];
for (int i = 0; i < transforms.Count; i++)
gameObjects[i] = transforms[i].gameObject;

return gameObjects;
}

[NotNull]
public static GameObject[] GetChildren(this GameObject parent)
{
Transform[] transforms = GetChildren(parent.transform);
GameObject[] gameObjects = new GameObject[transforms.Length];
for (int i = 0; i < transforms.Length; i++)
gameObjects[i] = transforms[i].gameObject;
return gameObjects;
}

Expand All @@ -59,10 +81,18 @@ public static List<Transform> FindChildrenByName(this Transform parent, string n
foreach (Transform t in parent.GetComponentsInChildren<Transform>(true))
if (t.name == name)
list.Add(t);

return list;
}

[NotNull]
public static Transform[] GetChildren(this Transform parent)
{
Transform[] array = new Transform[parent.childCount];
for (int i = 0; i < parent.childCount; i++)
array[i] = parent.GetChild(i);
return array;
}

public static T GetOrAddComponent<T>(this GameObject gameObject) where T : Component
{
T component = gameObject.GetComponent<T>();
Expand Down

0 comments on commit a3415f7

Please sign in to comment.