|
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
-
- [ExecuteInEditMode]
- [RequireComponent(typeof(RectTransform))]
- public class MarginsAffector : UIBehaviour
- {
- public void FindElements()
- {
- this._sizeElements.Clear();
- this._affectedElements.Clear();
- this._sizeElements.AddRange(base.gameObject.GetComponentsInChildren<MarginSize>(true));
- this._affectedElements.AddRange(base.gameObject.GetComponentsInChildren<MarginElement>(true));
- }
-
- public void AddSizeElement(MarginSize element)
- {
- if (!this._sizeElements.Contains(element))
- {
- this._sizeElements.Add(element);
- }
- }
-
- public void AddAffectedElement(MarginElement element)
- {
- if (!this._affectedElements.Contains(element))
- {
- this._affectedElements.Add(element);
- }
- }
-
- public void RemoveSizeElement(MarginSize element)
- {
- this._sizeElements.Remove(element);
- }
-
- public void RemoveAffectedElement(MarginElement element)
- {
- if (this._affectedElements.Contains(element))
- {
- this._affectedElements.Remove(element);
- }
- }
-
- public void Recalculate()
- {
- Vector2 size = new Vector2(-1f, -1f);
- for (int i = this._sizeElements.Count - 1; i >= 0; i--)
- {
- MarginSize marginSize = this._sizeElements[i];
- if (marginSize == null)
- {
- this._sizeElements.RemoveAt(i);
- }
- else
- {
- Vector2 preferedElementSize = marginSize.GetPreferedElementSize();
- if (marginSize.gameObject.activeInHierarchy && this.GetSizeValue(size) < this.GetSizeValue(preferedElementSize))
- {
- size = preferedElementSize;
- }
- }
- }
- for (int j = this._affectedElements.Count - 1; j >= 0; j--)
- {
- MarginElement marginElement = this._affectedElements[j];
- if (marginElement == null)
- {
- this._affectedElements.RemoveAt(j);
- }
- else
- {
- this.SetSizeValue(marginElement, size);
- }
- }
- }
-
- protected override void OnDisable()
- {
- for (int i = this._affectedElements.Count - 1; i >= 0; i--)
- {
- MarginElement marginElement = this._affectedElements[i];
- if (marginElement == null)
- {
- this._affectedElements.RemoveAt(i);
- }
- else
- {
- marginElement.AffectSize(null, null);
- }
- }
- }
-
- protected void Update()
- {
- if (base.transform.hasChanged)
- {
- this.Recalculate();
- }
- }
-
- private void SetSizeValue(MarginElement element, Vector2 size)
- {
- MarginsAffector.MarginType marginType = this._marginType;
- if (marginType != MarginsAffector.MarginType.All)
- {
- if (marginType != MarginsAffector.MarginType.Width)
- {
- if (marginType == MarginsAffector.MarginType.Height)
- {
- element.AffectSize(null, new float?(size.y));
- }
- }
- else
- {
- element.AffectSize(new float?(size.x), null);
- }
- }
- else
- {
- element.AffectSize(new float?(size.x), new float?(size.y));
- }
- }
-
- private float GetSizeValue(Vector2 size)
- {
- MarginsAffector.MarginType marginType = this._marginType;
- if (marginType == MarginsAffector.MarginType.All)
- {
- return size.x + size.y;
- }
- if (marginType == MarginsAffector.MarginType.Width)
- {
- return size.x;
- }
- if (marginType != MarginsAffector.MarginType.Height)
- {
- return -1f;
- }
- return size.y;
- }
-
- [Header("Options")]
- [SerializeField]
- private MarginsAffector.MarginType _marginType;
-
- [SerializeField]
- [HideInInspector]
- private List<MarginSize> _sizeElements = new List<MarginSize>();
-
- [SerializeField]
- [HideInInspector]
- private List<MarginElement> _affectedElements = new List<MarginElement>();
-
- private RectTransform _rectTransform;
-
- public enum MarginType
- {
- All,
- Width,
- Height
- }
- }
|