using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using System.Linq; namespace BITKit { public static partial class Extensions { public static bool TryGetRoot(this Component self, out Root root) { root = null; try { if (self is not null && self.TryGetComponent(out root)) { root = root.root; return true; } } catch (System.Exception) { return false; } return false; } } public class Root : SerializedMonoBehaviour { [Header(Constant.Header.Settings)] public Root root; public string id; [Header(Constant.Header.Gameobjects)] public Dictionary childs = new(); public Root Get(string id) { if (id.IsNullOrEmpty()) { return this; } else { return childs[id]; } } [Button] void AutoSetUp() { if (id.IsNullOrEmpty()) { id = name; } childs.Clear(); if (root == this) { GetComponentsInChildren(true) .Where(x => x.root == root) .ForEach(x => { childs.TryAdd(x.id, x); }); } } } }