StaticSizerMethod.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEngine;
  6. #pragma warning disable 0649 // never assigned warning
  7. namespace TheraBytes.BetterUi
  8. {
  9. [Serializable]
  10. public class StaticSizerMethod
  11. {
  12. [SerializeField] string assemblyName = "Assembly-CSharp";
  13. [SerializeField] string typeName;
  14. [SerializeField] string methodName;
  15. public float Invoke(Component caller, Vector2 optimizedResolution, Vector2 actualResolution, float optimizedDpi, float actualDpi)
  16. {
  17. Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == assemblyName);
  18. if (assembly == null)
  19. {
  20. Debug.LogErrorFormat("Static Sizer Method: Assembly with name '{0}' could not be found.", assemblyName);
  21. return 0;
  22. }
  23. Type t = assembly.GetType(typeName, false);
  24. if(t == null)
  25. {
  26. Debug.LogErrorFormat("Static Sizer Method: Type '{0}' could not be found in assembly '{1}'. Make sure the name contains the full namespace.", typeName, assemblyName);
  27. return 0;
  28. }
  29. MethodInfo method = t.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public);
  30. if(method == null)
  31. {
  32. Debug.LogErrorFormat("Static Sizer Method: Method '{0}()' could not be found in Type '{0}'. Make sure it is declared public and static.", methodName, typeName);
  33. return 0;
  34. }
  35. try
  36. {
  37. object result = method.Invoke(null, new object[] { caller, optimizedResolution, actualResolution, optimizedDpi, actualDpi });
  38. return (float)result;
  39. }
  40. catch (Exception ex)
  41. {
  42. Debug.LogErrorFormat("Static Sizer Method: Method '{0}.{1}()' could be found but failed to be invoked (see details below). Make sure it has all parameters and returns a float.", methodName, typeName);
  43. Debug.LogException(ex);
  44. return 0;
  45. }
  46. }
  47. }
  48. }
  49. #pragma warning restore 0649