Utility.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Reflection;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. public static class Utility
  6. {
  7. public enum ModKey
  8. {
  9. Control, Shift, Alt
  10. }
  11. internal static int Wrap(int value, int min, int max)
  12. {
  13. max -= 1;
  14. return value < min ? max : value > max ? min : value;
  15. }
  16. internal static int GetPix(int num)
  17. {
  18. return (int)((1f + (Screen.width / 1280f - 1f) * 0.6f) * num);
  19. }
  20. internal static Texture2D MakeTex(int width, int height, Color color)
  21. {
  22. Color[] colors = new Color[width * height];
  23. for (int i = 0; i < colors.Length; i++)
  24. {
  25. colors[i] = color;
  26. }
  27. Texture2D texture2D = new Texture2D(width, height);
  28. texture2D.SetPixels(colors);
  29. texture2D.Apply();
  30. return texture2D;
  31. }
  32. internal static FieldInfo GetFieldInfo<T>(string field)
  33. {
  34. BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
  35. return typeof(T).GetField(field, bindingFlags);
  36. }
  37. internal static TValue GetFieldValue<TType, TValue>(TType instance, string field)
  38. {
  39. FieldInfo fieldInfo = GetFieldInfo<TType>(field);
  40. if (fieldInfo == null || !fieldInfo.IsStatic && instance == null) return default(TValue);
  41. return (TValue)fieldInfo.GetValue(instance);
  42. }
  43. internal static void SetFieldValue<TType, TValue>(TType instance, string name, TValue value)
  44. {
  45. FieldInfo fieldInfo = GetFieldInfo<TType>(name);
  46. fieldInfo.SetValue(instance, value);
  47. }
  48. internal static bool GetModKey(ModKey key)
  49. {
  50. switch (key)
  51. {
  52. case ModKey.Control: return Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
  53. case ModKey.Alt: return Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt);
  54. case ModKey.Shift: return Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
  55. default: return false;
  56. }
  57. }
  58. internal static bool AnyMouseDown()
  59. {
  60. return Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2);
  61. }
  62. }
  63. }