Drawer.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace Popcron
  6. {
  7. public abstract class Drawer
  8. {
  9. private static Dictionary<Type, Drawer> typeToDrawer = null;
  10. public abstract int Draw(ref Vector3[] buffer, params object[] args);
  11. protected Drawer()
  12. {
  13. }
  14. public static Drawer Get<T>() where T : class
  15. {
  16. //find all drawers
  17. if (typeToDrawer == null)
  18. {
  19. typeToDrawer = new Dictionary<Type, Drawer>();
  20. Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  21. foreach (Assembly assembly in assemblies)
  22. {
  23. Type[] types = assembly.GetTypes();
  24. foreach (Type type in types)
  25. {
  26. if (type.IsAbstract)
  27. {
  28. continue;
  29. }
  30. if (type.IsSubclassOf(typeof(Drawer)))
  31. {
  32. Drawer value = (Drawer)Activator.CreateInstance(type);
  33. typeToDrawer.Add(type, value);
  34. }
  35. }
  36. }
  37. }
  38. if (typeToDrawer.TryGetValue(typeof(T), out Drawer drawer))
  39. {
  40. return drawer;
  41. }
  42. else
  43. {
  44. return null;
  45. }
  46. }
  47. }
  48. }