快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

Unity实现卡牌翻动效果

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

创新互联网站建设公司是一家服务多年做网站建设策划设计制作的公司,为广大用户提供了成都网站设计、网站制作、外贸营销网站建设,成都网站设计,广告投放,成都做网站选创新互联,贴合企业需求,高性价比,满足客户不同层次的需求一站式服务欢迎致电。

事实上这是项目需要,我改的一个代码,实际上就是利用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 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();
   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().anchoredPosition.y - _arrayOfElements[0].GetComponent().anchoredPosition.y);
   deltaY = _arrayOfElements[0].GetComponent().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

源代码是上下滑动的,再上我改过之后的代码,左右滑动的;

/// 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 UIVerticalScrollerMove : 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 ScrollLeftButton;//左按钮
  [Tooltip("Button to go to the previous page. (optional)")]
  public GameObject ScrollRightButton;//右按钮
  [Tooltip("Event fired when a specific item is clicked, exposes index number of item. (optional)")]
  public UnityEvent ButtonClicked;//按钮点击
 
 
  private float[] distReposition;//长度改变
  private float[] distance;//长度列表
  //private int elementsDistance;
  private int minElementsNum;//最小元素数
  private int elementLength;//元素长度
  //private int elementHalfLength;
  private float deltaX;//移动x
  private string result;//结果
 
  public UIVerticalScrollerMove() { }//构造函数
 
  public UIVerticalScrollerMove(RectTransform scrollingPanel, GameObject[] arrayOfElements, RectTransform center)
  {
   _scrollingPanel = scrollingPanel;
   _arrayOfElements = arrayOfElements;
   _center = center;
  }
 
 
  //初始化启动
  public void Awake()
  {
   var scrollRect = GetComponent();//获取到排列
   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().anchoredPosition.y - _arrayOfElements[0].GetComponent().anchoredPosition.y);
   deltaX = _arrayOfElements[0].GetComponent().rect.width * elementLength / 3 * 2;
   Vector2 startPosition = new Vector2( -deltaX,_scrollingPanel.anchoredPosition.y);
   _scrollingPanel.anchoredPosition = startPosition;//获取到更改的按钮
 
   for (var i = 0; i < _arrayOfElements.Length; i++)
   {
    AddListener(_arrayOfElements[i], i);
   }//监听每个按钮上挂载的方法
 
   //如果左右按钮的话,分别监听不同的方法
   if (ScrollLeftButton)
    ScrollLeftButton.GetComponent

这是可插件里面的类库,不过核心逻辑可以用Unity来重写,以上都有注释。

最后是引用方法:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UI.Extensions;
 
public class ScrollingCalendarTest : MonoBehaviour {
 public RectTransform monthsScrollingPanel;
 public GameObject monthsButtonPrefab;
 private GameObject[] monthsButtons;
 public RectTransform monthCenter;
 
 private int monthsSet;
 
 UIVerticalScrollerMove monthsVerticalScroller;
 //Initialize Months
 //生成预制体
 private void InitializeMonths()
 {
 int[] months = new int[12];
 
 monthsButtons = new GameObject[months.Length];
 for (int i = 0; i < months.Length; i++)
 {
 string month = "";
 months[i] = i;
 
 GameObject clone = (GameObject)Instantiate(monthsButtonPrefab, new Vector3(i * 380,0, 0), Quaternion.Euler(new Vector3(0, 0, 0))) as GameObject;
 clone.transform.SetParent(monthsScrollingPanel, false);
 clone.transform.localScale = new Vector3(1, 1, 1);
 
 month = ""+i;
 
 clone.GetComponentInChildren().text = month;
 clone.name = "Month_" + months[i];
 clone.AddComponent();
 monthsButtons[i] = clone;
 }
 }
 // Use this for initialization
  public void Awake()
  {
   InitializeMonths();
 
   //Yes Unity complains about this but it doesn't matter in this case.
   monthsVerticalScroller = new UIVerticalScrollerMove(monthsScrollingPanel, monthsButtons, monthCenter);
 
   monthsVerticalScroller.Start();
  }
 
  public void SetDate()
  {
//   monthsSet = int.Parse(inputFieldMonths.text) - 1;
 
   monthsVerticalScroller.SnapToElement(monthsSet);
  }
 
  void Update()
  {
   monthsVerticalScroller.Update();
 
   string monthString = monthsVerticalScroller.GetResults();
 
  }
 
 
  public void MonthsScrollUp()
  {
   monthsVerticalScroller.ScrollLeft();
  }
 
  public void MonthsScrollDown()
  {
   monthsVerticalScroller.ScrollRight();
  }
 
}

效果与引用:

Unity实现卡牌翻动效果Unity实现卡牌翻动效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


当前名称:Unity实现卡牌翻动效果
网站路径:http://6mz.cn/article/gddhip.html

其他资讯