Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using MaterialUI;
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- [ExecuteInEditMode]
- public class TabParallaxBackground : UIBehaviour
- {
- [HideInInspector]
- [SerializeField]
- private RectTransform m_RectTransform;
- public RectTransform rectTransform
- {
- get
- {
- if (m_RectTransform == null)
- {
- m_RectTransform = transform as RectTransform;
- }
- return m_RectTransform;
- }
- }
- [HideInInspector]
- [SerializeField]
- private Image m_Image;
- public Image image
- {
- get
- {
- if (m_Image == null)
- {
- m_Image = GetComponent<Image>();
- }
- return m_Image;
- }
- }
- [HideInInspector]
- [SerializeField]
- private TabView m_TabView;
- public TabView tabView
- {
- get
- {
- if (m_TabView == null)
- {
- m_TabView = GetComponentInParent<TabView>();
- }
- return m_TabView;
- }
- }
- [SerializeField]
- [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.")]
- private float m_ParallaxIntensity = 0.2f;
- public float parallaxIntensity
- {
- get { return m_ParallaxIntensity; }
- set { m_ParallaxIntensity = value; }
- }
- [SerializeField]
- private RectTransform m_ContainerRectTransform;
- [HideInInspector]
- [SerializeField]
- private RectTransform m_PagesRectTransform;
- public RectTransform pagesRectTransform
- {
- get
- {
- if (m_PagesRectTransform == null)
- {
- if (m_ContainerRectTransform != null)
- {
- m_PagesRectTransform = m_ContainerRectTransform.parent as RectTransform;
- }
- }
- return m_PagesRectTransform;
- }
- }
- // A way to update in edit mode that doesn't require object to be selected in inspector + input action in that frame.
- #if UNITY_EDITOR
- protected override void OnEnable()
- {
- if (PrefabUtility.GetPrefabType(this) == PrefabType.Prefab)
- {
- EditorUpdate.onEditorUpdate -= OnEditorUpdate;
- return;
- }
- EditorUpdate.Init();
- EditorUpdate.onEditorUpdate += OnEditorUpdate;
- }
- protected override void OnDisable()
- {
- EditorUpdate.onEditorUpdate -= OnEditorUpdate;
- }
- private void OnEditorUpdate()
- {
- if (IsDestroyed())
- {
- EditorUpdate.onEditorUpdate -= OnEditorUpdate;
- return;
- }
- if (m_ContainerRectTransform == null) return;
- SetSize();
- SetPosition();
- }
- #endif
- protected override void Start()
- {
- if (m_ContainerRectTransform == null)
- {
- Debug.LogWarning("Container RectTransform not set for TabParallaxBackground component", this);
- return;
- }
- image.preserveAspect = true;
- SetSize();
- SetPosition();
- }
- void Update()
- {
- if (m_ContainerRectTransform == null) return;
- SetPosition();
- }
- private void SetSize()
- {
- // The size of the sprite itself, used to find the target Image size.
- Vector2 spriteSize = new Vector2(image.sprite.texture.width, image.sprite.texture.height);
- // The desired minimum width and height of the Image, factoring in page width, number of pages, and parallax intensity.
- Vector2 sizeToEnvelop = new Vector2(pagesRectTransform.rect.width + pagesRectTransform.rect.width * m_ParallaxIntensity * (tabView.pages.Length - 1), pagesRectTransform.rect.height);
- // The calculated target size for the Image.
- Vector2 targetSize = new Vector2();
- // Calculate the target size, matching the height to the container's height.
- targetSize.y = sizeToEnvelop.y;
- targetSize.x = spriteSize.x * sizeToEnvelop.y / spriteSize.y;
- // If the width is too short when the height matches, match by width instead.
- if (targetSize.x < sizeToEnvelop.x)
- {
- targetSize.x = sizeToEnvelop.x;
- targetSize.y = spriteSize.y * sizeToEnvelop.x / spriteSize.x;
- }
- // Set the size.
- rectTransform.sizeDelta = targetSize;
- }
- private void SetPosition()
- {
- // Sets the x position of the Image, based on the position of the page container, and parallax intensity.
- Vector2 position = rectTransform.localPosition;
- position.x = (m_ContainerRectTransform.anchoredPosition.x + m_ContainerRectTransform.rect.width / 2 - pagesRectTransform.rect.width / 2) * m_ParallaxIntensity;
- rectTransform.localPosition = position;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement