Skip to content

Instantly share code, notes, and snippets.

@baba-s
Last active March 13, 2024 05:53
Show Gist options
  • Save baba-s/2a35637204b60cfe77443211cc0810e2 to your computer and use it in GitHub Desktop.
Save baba-s/2a35637204b60cfe77443211cc0810e2 to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
namespace Kogane
{
public static class ComponentExtensionMethods
{
public static GameObject FindByNameContains<T>
(
this T self,
string name,
bool includeInactive = false
) where T : Component
{
var children = self.GetComponentsInChildren<Transform>( includeInactive );
for ( var i = 0; i < children.Length; i++ )
{
var child = children[ i ];
if ( !child.name.Contains( name, StringComparison.Ordinal ) ) continue;
return child.gameObject;
}
return null;
}
}
}
using System;
using UnityEngine;
namespace Kogane
{
public static class GameObjectExtensionMethods
{
public static GameObject FindByNameContains
(
this GameObject self,
string name,
bool includeInactive = false
)
{
var children = self.GetComponentsInChildren<Transform>( includeInactive );
for ( var i = 0; i < children.Length; i++ )
{
var child = children[ i ];
if ( !child.name.Contains( name, StringComparison.Ordinal ) ) continue;
return child.gameObject;
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment