Unity实现卡牌翻动效果-创新互联

本文实例为大家分享了Unity实现卡牌翻动效果展示的具体代码,供大家参考,具体内容如下

定安网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。创新互联公司从2013年成立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司

事实上这是项目需要,我改的一个代码,实际上就是利用unity的一些基础属性实现其效果。啥也不多说了,先上原代码:

/// 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;
  }
 }
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

网页题目:Unity实现卡牌翻动效果-创新互联
本文来源:https://www.cdcxhl.com/article30/deegpo.html

成都网站建设公司_创新互联,为您提供定制网站营销型网站建设全网营销推广面包屑导航关键词优化网站设计

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网页设计公司