39 lines
1.1 KiB
C#
39 lines
1.1 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public static class ComponentFinderExtensions
|
|
{
|
|
/// <summary>
|
|
/// 트랜스폼 기준으로 자식들의 컴포넌트를 이름(key)으로 매핑
|
|
/// </summary>
|
|
public static Dictionary<string, T> GetChildComponentsByName<T>(this Transform root, bool includeInactive = true) where T : Component
|
|
{
|
|
var dictionary = new Dictionary<string, T>();
|
|
var components = root.GetComponentsInChildren<T>(includeInactive);
|
|
|
|
foreach (var comp in components)
|
|
{
|
|
if (string.IsNullOrEmpty(comp.name))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (!dictionary.ContainsKey(comp.name))
|
|
{
|
|
dictionary[comp.name] = comp;
|
|
}
|
|
}
|
|
|
|
return dictionary;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dictionary에서 key로 안전하게 가져오기
|
|
/// </summary>
|
|
public static T GetOrNull<T>(this Dictionary<string, T> dict, string name) where T : UnityEngine.Object
|
|
{
|
|
dict.TryGetValue(name, out var value);
|
|
return value;
|
|
}
|
|
}
|