Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public static class ComponentDatabase
- {
- public class TypeNode : IEnumerable<System.Type>
- {
- public System.Type type;
- public TypeNode next = null;
- public IEnumerator<System.Type> GetEnumerator()
- {
- for (var t = this; t != null; t = t.next)
- yield return t.type;
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
- private static List<System.Type> m_Types;
- private static Dictionary<string, TypeNode> m_Dict;
- static ComponentDatabase()
- {
- var comp = typeof(Component);
- var hashset = new HashSet<System.Type>();
- m_Dict = new Dictionary<string, TypeNode>();
- foreach (var a in System.AppDomain.CurrentDomain.GetAssemblies())
- {
- foreach (var t in a.GetTypes())
- {
- if (comp.IsAssignableFrom(t) && !t.IsAbstract && !hashset.Contains(t) && t != comp)
- {
- hashset.Add(t);
- TypeNode tn = null;
- m_Dict.TryGetValue(t.Name, out tn);
- tn = new TypeNode { next = tn, type = t };
- m_Dict[t.Name] = tn;
- }
- }
- }
- m_Types = new List<System.Type>(hashset.Count);
- m_Types.AddRange(hashset);
- }
- public static TypeNode FindComponent(string aComponentName)
- {
- TypeNode tn;
- if (m_Dict.TryGetValue(aComponentName, out tn))
- return tn;
- return null;
- }
- public static List<System.Type> GetTypes(System.Type aBaseType)
- {
- var res = new List<System.Type>();
- foreach (var t in m_Types)
- {
- if (aBaseType.IsAssignableFrom(t))
- res.Add(t);
- }
- return res;
- }
- public static List<System.Type> GetTypes<T>()
- {
- return GetTypes(typeof(T));
- }
- public static IEnumerable<System.Type> GetAllTypes()
- {
- return m_Types.AsReadOnly();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement