1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System;
- using System.Reflection;
- using System.Collections.Generic;
- using UnityEngine;
- namespace Popcron
- {
- public abstract class Drawer
- {
- private static Dictionary<Type, Drawer> typeToDrawer = null;
- public abstract int Draw(ref Vector3[] buffer, params object[] args);
- protected Drawer()
- {
- }
- public static Drawer Get<T>() where T : class
- {
- //find all drawers
- if (typeToDrawer == null)
- {
- typeToDrawer = new Dictionary<Type, Drawer>();
- Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
- foreach (Assembly assembly in assemblies)
- {
- Type[] types = assembly.GetTypes();
- foreach (Type type in types)
- {
- if (type.IsAbstract)
- {
- continue;
- }
- if (type.IsSubclassOf(typeof(Drawer)))
- {
- Drawer value = (Drawer)Activator.CreateInstance(type);
- typeToDrawer.Add(type, value);
- }
- }
- }
- }
- if (typeToDrawer.TryGetValue(typeof(T), out Drawer drawer))
- {
- return drawer;
- }
- else
- {
- return null;
- }
- }
- }
- }
|