namespace UIWidgets { using EasyLayoutNS; using UnityEngine; using UnityEngine.UI; /// /// Scroll compensation for Tsunami effect. /// public class TsunamiEffectScrollCompensation : MonoBehaviour, ILateUpdatable { /// /// Scroll Rect. /// public ScrollRect ScrollRect; /// /// Layout. /// protected EasyLayout Layout; /// /// Layout margin. /// protected Padding Margin; /// /// Tsunami effect compensations. /// protected Vector2 Compensation = Vector2.zero; /// /// Start this instance. /// protected virtual void Start() { Layout = GetComponent(); if (Layout != null) { if (Layout.Symmetric) { Layout.MarginLeft = Layout.Margin.x; Layout.MarginRight = Layout.Margin.x; Layout.MarginTop = Layout.Margin.y; Layout.MarginBottom = Layout.Margin.y; Layout.Symmetric = false; } Margin = new Padding(Layout.MarginLeft, Layout.MarginRight, Layout.MarginTop, Layout.MarginBottom); } } /// /// Process the enable event. /// protected virtual void OnEnable() { Updater.AddLateUpdate(this); } /// /// Process the disable event. /// protected virtual void OnDisable() { Updater.RemoveLateUpdate(this); } /// /// Late update. /// public virtual void RunLateUpdate() { if (Layout != null) { var compensation = ScrollRect.horizontal ? Compensation.x : Compensation.y; ApplyCompensatin(compensation); } Compensation = Vector2.zero; } void ApplyCompensatin(float compensation) { if (ScrollRect.horizontal) { Layout.MarginLeft = Margin.Left - (compensation * (1f - ScrollRect.horizontalNormalizedPosition)); } else { Layout.MarginTop = Margin.Top - (compensation * (1f - ScrollRect.verticalNormalizedPosition)); } Layout.UpdateLayout(); } /// /// Add compensation. /// /// Compensation. public void Add(Vector2 compensation) { Compensation += compensation; } } }