Created
July 2, 2017 19:51
-
-
Save unity3dcollege/255685652b82ce39f759d5c6e27d0649 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
public class SlidableMask : MonoBehaviour | |
{ | |
[SerializeField] | |
private Slider slider; | |
private RectTransform rectTransform; | |
private Vector3 farLeft; | |
private Vector3 farRight; | |
private void Awake() | |
{ | |
rectTransform = GetComponent<RectTransform>(); | |
slider.onValueChanged.AddListener(HandleSliderChanged); | |
farLeft = rectTransform.position - new Vector3(rectTransform.rect.width, 0f); | |
farRight = rectTransform.position; | |
} | |
private void Start() | |
{ | |
HandleSliderChanged(slider.value); // This needs to be called AFTER RectTransformLockPositions's Awake(). | |
} | |
private void HandleSliderChanged(float value) | |
{ | |
rectTransform.position = Vector2.Lerp(farLeft, farRight, value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment