123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- using System;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- namespace SRF.UI
- {
- [ExecuteInEditMode]
- [RequireComponent(typeof(RectTransform))]
- [AddComponentMenu("SRF/UI/Content Fit Text")]
- public class ContentFitText : UIBehaviour, ILayoutElement
- {
- public float minWidth
- {
- get
- {
- if (this.CopySource == null)
- {
- return -1f;
- }
- return LayoutUtility.GetMinWidth(this.CopySource.rectTransform) + this.Padding.x;
- }
- }
- public float preferredWidth
- {
- get
- {
- if (this.CopySource == null)
- {
- return -1f;
- }
- return LayoutUtility.GetPreferredWidth(this.CopySource.rectTransform) + this.Padding.x;
- }
- }
- public float flexibleWidth
- {
- get
- {
- if (this.CopySource == null)
- {
- return -1f;
- }
- return LayoutUtility.GetFlexibleWidth(this.CopySource.rectTransform);
- }
- }
- public float minHeight
- {
- get
- {
- if (this.CopySource == null)
- {
- return -1f;
- }
- return LayoutUtility.GetFlexibleHeight(this.CopySource.rectTransform) + this.Padding.y;
- }
- }
- public float preferredHeight
- {
- get
- {
- if (this.CopySource == null)
- {
- return -1f;
- }
- return LayoutUtility.GetPreferredHeight(this.CopySource.rectTransform) + this.Padding.y;
- }
- }
- public float flexibleHeight
- {
- get
- {
- if (this.CopySource == null)
- {
- return -1f;
- }
- return LayoutUtility.GetFlexibleHeight(this.CopySource.rectTransform);
- }
- }
- public int layoutPriority
- {
- get
- {
- return 0;
- }
- }
- public void CalculateLayoutInputHorizontal()
- {
- this.CopySource.CalculateLayoutInputHorizontal();
- }
- public void CalculateLayoutInputVertical()
- {
- this.CopySource.CalculateLayoutInputVertical();
- }
- protected override void OnEnable()
- {
- this.SetDirty();
- this.CopySource.LayoutDirty += this.CopySourceOnLayoutDirty;
- }
- private void CopySourceOnLayoutDirty(SRText srText)
- {
- this.SetDirty();
- }
- protected override void OnTransformParentChanged()
- {
- this.SetDirty();
- }
- protected override void OnDisable()
- {
- this.CopySource.LayoutDirty -= this.CopySourceOnLayoutDirty;
- this.SetDirty();
- }
- protected override void OnDidApplyAnimationProperties()
- {
- this.SetDirty();
- }
- protected override void OnBeforeTransformParentChanged()
- {
- this.SetDirty();
- }
- protected void SetDirty()
- {
- if (!this.IsActive())
- {
- return;
- }
- LayoutRebuilder.MarkLayoutForRebuild(base.transform as RectTransform);
- }
- public SRText CopySource;
- public Vector2 Padding;
- }
- }
|