namespace UIWidgets.Examples { using System.Collections.Generic; using UIWidgets; using UnityEngine; /// /// Test layout switcher. /// public class TestLayoutSwitcher : MonoBehaviour { /// /// Layout switcher. /// [SerializeField] public LayoutSwitcher Switcher; /// /// Start this instance. /// public void Start() { Switcher.LayoutSelector = Selector; } /// /// Example of the custom layout selector. /// /// Layouts list. /// Display size. /// Aspect ratio. /// Layout. protected static UILayout Selector(List layouts, float displaySize, float aspectRatio) { var filtered = FilterLayouts(layouts, aspectRatio); if (aspectRatio < 1f) { var layout = SelectPortrait(filtered); if (layout != null) { return layout; } } return SelectLandscape(filtered, aspectRatio); } /// /// Select portrait layout. /// /// Layouts. /// Layout. protected static UILayout SelectPortrait(List layouts) { if (layouts.Count == 0) { return null; } var result = layouts[0]; for (int i = 1; i < layouts.Count; i++) { var layout = layouts[i]; if (layout.AspectRatioFloat < result.AspectRatioFloat) { result = layout; } } return result; } /// /// Select landscape layout. /// /// Layouts. /// Aspect ratio. /// Layout. protected static UILayout SelectLandscape(List layouts, float aspectRatio) { if (layouts.Count == 0) { return null; } var result = layouts[0]; var result_value = Mathf.Abs(aspectRatio - result.AspectRatioFloat); for (int i = 1; i < layouts.Count; i++) { var layout = layouts[i]; var layout_value = Mathf.Abs(aspectRatio - layout.AspectRatioFloat); if (layout_value < result_value) { result = layout; result_value = layout_value; } } return result; } /// /// Filter layouts. /// /// Layouts. /// Aspect ratio. /// Filtered layouts. protected static List FilterLayouts(List layouts, float aspectRatio) { var result = new List(layouts.Count); foreach (var l in layouts) { if (l.AspectRatioFloat >= aspectRatio) { result.Add(l); } } return result; } /// /// Select 4:3 layout only for 4:3, in other cases use 16:9 layout. /// /// Layouts list. /// Display size. /// Aspect ratio. /// Layout. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "displaySize", Justification = "Interface compatibility.")] protected static UILayout Selector43(List layouts, float displaySize, float aspectRatio) { // if aspect_ration 4:3 if (Mathf.Approximately(4f / 3f, aspectRatio)) { var layout_4_3_index = 0; return layouts[layout_4_3_index]; } var layout_9_16_index = 1; return layouts[layout_9_16_index]; } } }