BetterAspectRatioFitter.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. namespace TheraBytes.BetterUi
  8. {
  9. #if UNITY_2018_3_OR_NEWER
  10. [ExecuteAlways]
  11. #else
  12. [ExecuteInEditMode]
  13. #endif
  14. [HelpURL("https://documentation.therabytes.de/better-ui/BetterAspectRatioFitter.html")]
  15. [AddComponentMenu("Better UI/Layout/Better Aspect Ratio Fitter", 30)]
  16. public class BetterAspectRatioFitter : AspectRatioFitter, IResolutionDependency
  17. {
  18. [Serializable]
  19. public class Settings : IScreenConfigConnection
  20. {
  21. public AspectMode AspectMode;
  22. public float AspectRatio = 1;
  23. [SerializeField]
  24. string screenConfigName;
  25. public string ScreenConfigName { get { return screenConfigName; } set { screenConfigName = value; } }
  26. }
  27. [Serializable]
  28. public class SettingsConfigCollection : SizeConfigCollection<Settings> { }
  29. public Settings CurrentSettings { get { return customSettings.GetCurrentItem(settingsFallback); } }
  30. public new AspectMode aspectMode
  31. {
  32. get { return base.aspectMode; }
  33. set
  34. {
  35. Config.Set(value, (o) => base.aspectMode = value, (o) => CurrentSettings.AspectMode = value);
  36. }
  37. }
  38. public new float aspectRatio
  39. {
  40. get { return base.aspectRatio; }
  41. set
  42. {
  43. Config.Set(value, (o) => base.aspectRatio = value, (o) => CurrentSettings.AspectRatio = value);
  44. }
  45. }
  46. [SerializeField]
  47. Settings settingsFallback = new Settings();
  48. [SerializeField]
  49. SettingsConfigCollection customSettings = new SettingsConfigCollection();
  50. protected override void OnEnable()
  51. {
  52. base.OnEnable();
  53. Apply();
  54. }
  55. public void OnResolutionChanged()
  56. {
  57. Apply();
  58. }
  59. void Apply()
  60. {
  61. base.aspectMode = CurrentSettings.AspectMode;
  62. base.aspectRatio = CurrentSettings.AspectRatio;
  63. }
  64. #if UNITY_EDITOR
  65. protected override void OnValidate()
  66. {
  67. base.OnValidate();
  68. Apply();
  69. }
  70. #endif
  71. }
  72. }