本文實(shí)例為大家分享了Unity實(shí)現(xiàn)卡牌翻動(dòng)效果展示的具體代碼,供大家參考,具體內(nèi)容如下
事實(shí)上這是項(xiàng)目需要,我改的一個(gè)代碼,實(shí)際上就是利用unity的一些基礎(chǔ)屬性實(shí)現(xiàn)其效果。啥也不多說(shuō)了,先上原代碼:
/// Credit Mrs. YakaYocha /// Sourced from - https://www.youtube.com/channel/UCHp8LZ_0-iCvl-5pjHATsgw /// Please donate: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RJ8D9FRFQF9VS using UnityEngine.Events; namespace UnityEngine.UI.Extensions { [RequireComponent(typeof(ScrollRect))] [AddComponentMenu("Layout/Extensions/Vertical Scroller")] public class UIVerticalScroller : MonoBehaviour { [Tooltip("Scrollable area (content of desired ScrollRect)")] public RectTransform _scrollingPanel; [Tooltip("Elements to populate inside the scroller")] public GameObject[] _arrayOfElements; [Tooltip("Center display area (position of zoomed content)")] public RectTransform _center; [Tooltip("Select the item to be in center on start. (optional)")] public int StartingIndex = -1; [Tooltip("Button to go to the next page. (optional)")] public GameObject ScrollUpButton; [Tooltip("Button to go to the previous page. (optional)")] public GameObject ScrollDownButton; [Tooltip("Event fired when a specific item is clicked, exposes index number of item. (optional)")] public UnityEvent<int> ButtonClicked; private float[] distReposition; private float[] distance; //private int elementsDistance; private int minElementsNum; private int elementLength; //private int elementHalfLength; private float deltaY; private string result; public UIVerticalScroller() { } public UIVerticalScroller(RectTransform scrollingPanel, GameObject[] arrayOfElements, RectTransform center) { _scrollingPanel = scrollingPanel; _arrayOfElements = arrayOfElements; _center = center; } public void Awake() { var scrollRect = GetComponent<ScrollRect>(); if (!_scrollingPanel) { _scrollingPanel = scrollRect.content; } if (!_center) { Debug.LogError("Please define the RectTransform for the Center viewport of the scrollable area"); } if (_arrayOfElements == null || _arrayOfElements.Length == 0) { var childCount = scrollRect.content.childCount; if (childCount > 0) { _arrayOfElements = new GameObject[childCount]; for (int i = 0; i < childCount; i++) { _arrayOfElements[i] = scrollRect.content.GetChild(i).gameObject; } } } } public void Start() { if (_arrayOfElements.Length < 1) { Debug.Log("No child content found, exiting.."); return; } elementLength = _arrayOfElements.Length; distance = new float[elementLength]; distReposition = new float[elementLength]; //get distance between buttons //elementsDistance = (int)Mathf.Abs(_arrayOfElements[1].GetComponent<RectTransform>().anchoredPosition.y - _arrayOfElements[0].GetComponent<RectTransform>().anchoredPosition.y); deltaY = _arrayOfElements[0].GetComponent<RectTransform>().rect.height * elementLength / 3 * 2; Vector2 startPosition = new Vector2(_scrollingPanel.anchoredPosition.x, -deltaY); _scrollingPanel.anchoredPosition = startPosition; for (var i = 0; i < _arrayOfElements.Length; i++) { AddListener(_arrayOfElements[i], i); } if (ScrollUpButton) ScrollUpButton.GetComponent<Button>().onClick.AddListener(() => { ScrollUp(); }); if (ScrollDownButton) ScrollDownButton.GetComponent<Button>().onClick.AddListener(() => { ScrollDown(); }); if (StartingIndex > -1) { StartingIndex = StartingIndex > _arrayOfElements.Length ? _arrayOfElements.Length - 1 : StartingIndex; SnapToElement(StartingIndex); } } private void AddListener(GameObject button, int index) { button.GetComponent<Button>().onClick.AddListener(() => DoSomething(index)); } private void DoSomething(int index) { if (ButtonClicked != null) { ButtonClicked.Invoke(index); } } public void Update() { if (_arrayOfElements.Length < 1) { return; } for (var i = 0; i < elementLength; i++) { distReposition[i] = _center.GetComponent<RectTransform>().position.y - _arrayOfElements[i].GetComponent<RectTransform>().position.y; distance[i] = Mathf.Abs(distReposition[i]); //Magnifying effect float scale = Mathf.Max(0.7f, 1 / (1 + distance[i] / 200)); _arrayOfElements[i].GetComponent<RectTransform>().transform.localScale = new Vector3(scale, scale, 1f); } float minDistance = Mathf.Min(distance); for (var i = 0; i < elementLength; i++) { _arrayOfElements[i].GetComponent<CanvasGroup>().interactable = false; if (minDistance == distance[i]) { minElementsNum = i; _arrayOfElements[i].GetComponent<CanvasGroup>().interactable = true; result = _arrayOfElements[i].GetComponentInChildren<Text>().text; } } ScrollingElements(-_arrayOfElements[minElementsNum].GetComponent<RectTransform>().anchoredPosition.y); } private void ScrollingElements(float position) { float newY = Mathf.Lerp(_scrollingPanel.anchoredPosition.y, position, Time.deltaTime * 1f); Vector2 newPosition = new Vector2(_scrollingPanel.anchoredPosition.x, newY); _scrollingPanel.anchoredPosition = newPosition; } public string GetResults() { return result; } public void SnapToElement(int element) { float deltaElementPositionY = _arrayOfElements[0].GetComponent<RectTransform>().rect.height * element; Vector2 newPosition = new Vector2(_scrollingPanel.anchoredPosition.x, -deltaElementPositionY); _scrollingPanel.anchoredPosition = newPosition; } public void ScrollUp() { float deltaUp = _arrayOfElements[0].GetComponent<RectTransform>().rect.height / 1.2f; Vector2 newPositionUp = new Vector2(_scrollingPanel.anchoredPosition.x, _scrollingPanel.anchoredPosition.y - deltaUp); _scrollingPanel.anchoredPosition = Vector2.Lerp(_scrollingPanel.anchoredPosition, newPositionUp, 1); } public void ScrollDown() { float deltaDown = _arrayOfElements[0].GetComponent<RectTransform>().rect.height / 1.2f; Vector2 newPositionDown = new Vector2(_scrollingPanel.anchoredPosition.x, _scrollingPanel.anchoredPosition.y + deltaDown); _scrollingPanel.anchoredPosition = newPositionDown; } } }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁(yè)題目:Unity實(shí)現(xiàn)卡牌翻動(dòng)效果-創(chuàng)新互聯(lián)
本文來(lái)源:http://chinadenli.net/article30/deegpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、營(yíng)銷型網(wǎng)站建設(shè)、全網(wǎng)營(yíng)銷推廣、面包屑導(dǎo)航、關(guān)鍵詞優(yōu)化、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容