Advertisement
Saxy_Guy

DialogAnimatorSlideFromRight

Jan 29th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.23 KB | None | 0 0
  1. // Animates a dialog when showing/hiding it
  2.     public class DialogAnimatorSlideFromRight : DialogAnimator
  3.     {
  4.         //  Constructor method
  5.         public DialogAnimatorSlideFromRight(float animationDuration = 0.5f)
  6.         {
  7.             m_AnimationDuration = 0.5f;
  8.         }
  9.  
  10.         //  Called when the dialog is shown
  11.         public override void AnimateShow(Action callback)
  12.         {
  13.             base.AnimateShow(callback);
  14.  
  15.             //  Make sure that the dialog's layout is fully built before attempting to get its size
  16.             LayoutRebuilder.ForceRebuildLayoutImmediate(m_Dialog.rectTransform);
  17.  
  18.             float halfScreenWidth = Screen.width / 2f;
  19.             float halfDialogWidth = m_Dialog.rectTransform.GetProperSize().x / 2f;
  20.  
  21.             //  Multiplied by 1.1f so the dialog is *definitely* off the screen
  22.             float startPoint = halfScreenWidth + (halfDialogWidth * 1.1f);
  23.  
  24.             //  Set the position of the dialog before animating (just past the right edge of the screen
  25.             m_Dialog.rectTransform.anchoredPosition = new Vector2(startPoint, 0f);
  26.  
  27.             //  Make the dialog full-size, now that it's not onscreen
  28.             m_Dialog.rectTransform.localScale = Vector3.one;
  29.  
  30.             //  Animate the dialog from its position to 0,0 on the screen (middle). The callback (called when the tween ends) is so the dialog knows when it's finished animating
  31.             TweenManager.AutoTween(m_Dialog.gameObject, m_Dialog.rectTransform, "anchoredPosition", Vector2.zero, m_AnimationDuration, callback: callback);
  32.         }
  33.  
  34.         //  Called when the dialog is hidden
  35.         public override void AnimateHide(Action callback)
  36.         {
  37.             base.AnimateHide(callback);
  38.  
  39.             float halfScreenWidth = Screen.width / 2f;
  40.             float halfDialogWidth = m_Dialog.rectTransform.GetProperSize().x / 2f;
  41.  
  42.             //  Multiplied by 1.1f so the dialog is *definitely* off the screen
  43.             float endPoint = halfScreenWidth + (halfDialogWidth * 1.1f);
  44.  
  45.             TweenManager.AutoTween(m_Dialog.gameObject, m_Dialog.rectTransform, "anchoredPosition", new Vector2(endPoint, 0f), m_AnimationDuration, 0f, Tween.TweenType.EaseInCubed, callback: callback);
  46.         }
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement