ModifierUtility.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4. namespace UnityEngine.UI{
  5. /// <summary>
  6. /// Helps with getting ModifierID Attributes etc.
  7. /// </summary>
  8. public static class ModifierUtility {
  9. /// <summary>
  10. /// Gets the instance with identifier specified in a ModifierID Attribute.
  11. /// </summary>
  12. /// <returns>The instance with identifier.</returns>
  13. /// <param name="id">Identifier.</param>
  14. public static ProceduralImageModifier GetInstanceWithId(string id){
  15. return (ProceduralImageModifier)Activator.CreateInstance(GetTypeWithId(id));
  16. }
  17. /// <summary>
  18. /// Gets the type with specified in a ModifierID Attribute.
  19. /// </summary>
  20. /// <returns>The type with identifier.</returns>
  21. /// <param name="id">Identifier.</param>
  22. public static Type GetTypeWithId(string id){
  23. foreach(Type type in Assembly.GetAssembly(typeof(ProceduralImageModifier)).GetTypes()) {
  24. if (type.IsSubclassOf(typeof(ProceduralImageModifier))){
  25. if(((ModifierID[])type.GetCustomAttributes(typeof(ModifierID),false))[0].Name == id){
  26. return type;
  27. }
  28. }
  29. }
  30. return null;
  31. }
  32. /// <summary>
  33. /// Gets a list of Attributes of type ModifierID.
  34. /// </summary>
  35. /// <returns>The attribute list.</returns>
  36. public static List<ModifierID> GetAttributeList(){
  37. List<ModifierID> l = new List<ModifierID> ();
  38. foreach(Type type in Assembly.GetAssembly(typeof(ProceduralImageModifier)).GetTypes()) {
  39. if (type.IsSubclassOf(typeof(ProceduralImageModifier))){
  40. l.Add (((ModifierID[])type.GetCustomAttributes(typeof(ModifierID),false))[0]);
  41. }
  42. }
  43. return l;
  44. }
  45. }
  46. }