36 lines
1006 B
C#
36 lines
1006 B
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public static class ComponentFinderExtensions
|
|
{
|
|
/// <summary>
|
|
/// 트랜스폼 기준으로 자식들의 컴포넌트를 이름(key)으로 매핑
|
|
/// </summary>
|
|
public static Dictionary<string, T> FindComponentDictionary<T>(this Transform root, bool includeInactive = true) where T : Component
|
|
{
|
|
var dict = new Dictionary<string, T>();
|
|
var components = root.GetComponentsInChildren<T>(includeInactive);
|
|
|
|
foreach (var comp in components)
|
|
{
|
|
if (string.IsNullOrEmpty(comp.name))
|
|
continue;
|
|
|
|
if (!dict.ContainsKey(comp.name))
|
|
dict[comp.name] = comp;
|
|
}
|
|
|
|
return dict;
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
}
|