2 using System.Collections.Generic;
4 using Microsoft.Xna.Framework;
11 protected GUICanvas() : base(Size, parent: null) { }
18 if (_instance ==
null)
32 private readonly List<WeakReference<RectTransform>> childrenWeakRef =
new List<WeakReference<RectTransform>>();
38 private enum ResizeAxis {
Both = 0, X = 1, Y = 2 }
42 CrossThread.RequestExecutionOnMainThread(RefreshChildren);
45 private static void RefreshChildren()
48 foreach (var child
in _instance.
Children)
50 if (!_instance.childrenWeakRef.Any(c => c.TryGetTarget(out var existingChild) && existingChild == child))
52 _instance.childrenWeakRef.Add(
new WeakReference<RectTransform>(child));
58 for (
int i = _instance.childrenWeakRef.Count - 1; i >= 0; i--)
60 if (!_instance.childrenWeakRef[i].TryGetTarget(out var child) || child.Parent != _instance)
62 _instance.childrenWeakRef.RemoveAt(i);
68 private static void RecalculateSize()
70 Vector2 recalculatedSize = Size;
73 for (
int i = 0; i <
Instance.childrenWeakRef.Count; i++)
75 if (!_instance.childrenWeakRef[i].TryGetTarget(out
RectTransform target) || target ==
null) {
continue; };
79 if (target.RelativeSize.X < 1 && target.RelativeSize.Y < 1) {
continue; }
83 if (target.RelativeSize.X >= 1 && target.RelativeSize.Y >= 1)
85 axis = ResizeAxis.Both;
87 else if (target.RelativeSize.X >= 1)
99 target.RelativeSize = recalculatedSize;
103 target.RelativeSize =
new Vector2(recalculatedSize.X, target.RelativeSize.Y);
107 target.RelativeSize =
new Vector2(target.RelativeSize.X, recalculatedSize.Y);
override Rectangle NonScaledUIRect
static GUICanvas Instance
Action ResolutionChanged
NOTE: Use very carefully. You need to ensure that you ALWAYS unsubscribe from this when you no longer...