Advertisement
Saxy_Guy

TabParallaxBackground

Sep 6th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.92 KB | None | 0 0
  1. using MaterialUI;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6.  
  7. [ExecuteInEditMode]
  8. public class TabParallaxBackground : UIBehaviour
  9. {
  10.     [HideInInspector]
  11.     [SerializeField]
  12.     private RectTransform m_RectTransform;
  13.     public RectTransform rectTransform
  14.     {
  15.         get
  16.         {
  17.             if (m_RectTransform == null)
  18.             {
  19.                 m_RectTransform = transform as RectTransform;
  20.             }
  21.             return m_RectTransform;
  22.         }
  23.     }
  24.  
  25.     [HideInInspector]
  26.     [SerializeField]
  27.     private Image m_Image;
  28.     public Image image
  29.     {
  30.         get
  31.         {
  32.             if (m_Image == null)
  33.             {
  34.                 m_Image = GetComponent<Image>();
  35.             }
  36.             return m_Image;
  37.         }
  38.     }
  39.  
  40.     [HideInInspector]
  41.     [SerializeField]
  42.     private TabView m_TabView;
  43.     public TabView tabView
  44.     {
  45.         get
  46.         {
  47.             if (m_TabView == null)
  48.             {
  49.                 m_TabView = GetComponentInParent<TabView>();
  50.             }
  51.             return m_TabView;
  52.         }
  53.     }
  54.  
  55.     [SerializeField]
  56.     [Tooltip("A value of 1 means the background moves the same as the container, and a value of 0 means the background doesn't move at all.")]
  57.     private float m_ParallaxIntensity = 0.2f;
  58.     public float parallaxIntensity
  59.     {
  60.         get { return m_ParallaxIntensity; }
  61.         set { m_ParallaxIntensity = value; }
  62.     }
  63.  
  64.     [SerializeField]
  65.     private RectTransform m_ContainerRectTransform;
  66.  
  67.     [HideInInspector]
  68.     [SerializeField]
  69.     private RectTransform m_PagesRectTransform;
  70.     public RectTransform pagesRectTransform
  71.     {
  72.         get
  73.         {
  74.             if (m_PagesRectTransform == null)
  75.             {
  76.                 if (m_ContainerRectTransform != null)
  77.                 {
  78.                     m_PagesRectTransform = m_ContainerRectTransform.parent as RectTransform;
  79.                 }
  80.             }
  81.             return m_PagesRectTransform;
  82.         }
  83.     }
  84.  
  85.     //  A way to update in edit mode that doesn't require object to be selected in inspector + input action in that frame.
  86. #if UNITY_EDITOR
  87.     protected override void OnEnable()
  88.     {
  89.         if (PrefabUtility.GetPrefabType(this) == PrefabType.Prefab)
  90.         {
  91.             EditorUpdate.onEditorUpdate -= OnEditorUpdate;
  92.             return;
  93.         }
  94.  
  95.         EditorUpdate.Init();
  96.         EditorUpdate.onEditorUpdate += OnEditorUpdate;
  97.     }
  98.  
  99.     protected override void OnDisable()
  100.     {
  101.         EditorUpdate.onEditorUpdate -= OnEditorUpdate;
  102.     }
  103.  
  104.     private void OnEditorUpdate()
  105.     {
  106.         if (IsDestroyed())
  107.         {
  108.             EditorUpdate.onEditorUpdate -= OnEditorUpdate;
  109.             return;
  110.         }
  111.  
  112.         if (m_ContainerRectTransform == null) return;
  113.  
  114.         SetSize();
  115.         SetPosition();
  116.     }
  117. #endif
  118.  
  119.     protected override void Start()
  120.     {
  121.         if (m_ContainerRectTransform == null)
  122.         {
  123.             Debug.LogWarning("Container RectTransform not set for TabParallaxBackground component", this);
  124.             return;
  125.         }
  126.  
  127.         image.preserveAspect = true;
  128.         SetSize();
  129.         SetPosition();
  130.     }
  131.  
  132.     void Update()
  133.     {
  134.         if (m_ContainerRectTransform == null) return;
  135.  
  136.         SetPosition();
  137.     }
  138.  
  139.     private void SetSize()
  140.     {
  141.         //  The size of the sprite itself, used to find the target Image size.
  142.         Vector2 spriteSize = new Vector2(image.sprite.texture.width, image.sprite.texture.height);
  143.  
  144.         //  The desired minimum width and height of the Image, factoring in page width, number of pages, and parallax intensity.
  145.         Vector2 sizeToEnvelop = new Vector2(pagesRectTransform.rect.width + pagesRectTransform.rect.width * m_ParallaxIntensity * (tabView.pages.Length - 1), pagesRectTransform.rect.height);
  146.  
  147.         //  The calculated target size for the Image.
  148.         Vector2 targetSize = new Vector2();
  149.  
  150.         //  Calculate the target size, matching the height to the container's height.
  151.         targetSize.y = sizeToEnvelop.y;
  152.         targetSize.x = spriteSize.x * sizeToEnvelop.y / spriteSize.y;
  153.  
  154.         //  If the width is too short when the height matches, match by width instead.
  155.         if (targetSize.x < sizeToEnvelop.x)
  156.         {
  157.             targetSize.x = sizeToEnvelop.x;
  158.             targetSize.y = spriteSize.y * sizeToEnvelop.x / spriteSize.x;
  159.         }
  160.  
  161.         //  Set the size.
  162.         rectTransform.sizeDelta = targetSize;
  163.     }
  164.  
  165.     private void SetPosition()
  166.     {
  167.         //  Sets the x position of the Image, based on the position of the page container, and parallax intensity.
  168.         Vector2 position = rectTransform.localPosition;
  169.         position.x = (m_ContainerRectTransform.anchoredPosition.x + m_ContainerRectTransform.rect.width / 2 - pagesRectTransform.rect.width / 2) * m_ParallaxIntensity;
  170.         rectTransform.localPosition = position;
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement