unity實現翻頁效果

本文實例為大傢分享瞭unity實現翻頁效果的具體代碼,供大傢參考,具體內容如下

一、示意圖

二、步驟

創建物體ToPanel;添加組件ScrollRect,
在下面創建一個空物體用來裝需要移動的子物體,

創建一個Scrollbar;

三、代碼

下面展示一些 內聯代碼片。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

public class SliderControl : MonoBehaviour
{
  public Scrollbar m_Scrollbar;
  public ScrollRect m_ScrollRect;
  [Header("子物體的父物體")]
  public GameObject panel;
  [Header("移動到多少百分比圖片切換")]  //註釋
  [Range(0.1f, 0.9f)] //范圍
  public float banfenbi = 0.5f;
  [Header("是否限制一次隻能切換一張圖片")]  
  public bool limit = false;

  private float mTargetValue;
  private bool mNeedMove = false;
  private const float SMOOTH_TIME = 0.2F;
  private float mMoveSpeed = 0f;

  private float child_Count;
  private float num1;
  private float var_Down;
  private List<float> num_list1 =new List<float>();
  private List<float> num_list2 = new List<float>();
  private List<float> num_list3 = new List<float>();
  private int weizhi; //頁數
  void Start()
  {
    weizhi = 0;
    child_Count = panel.transform.childCount;
    num1 = 1 / (child_Count - 1);
    print("-----子物體數量------  " + child_Count + "   " + num1);
    for (int i = 0; i < child_Count; i++)
    {
      float a = 0;
      num_list1.Add(a + num1 * i);
      float b = (a + num1 * i) + (num1 * banfenbi);
      num_list2.Add(b);
      float c = (a + num1 * i) + (num1 * (1-banfenbi));
      num_list3.Add(c);
      print("---child_Count----- " + num_list1[i] + "  " + num_list2[i]);
    }
  }
  public void OnPointer()
  { }

  public void OnPointerDown()
  {
    //print("---OnPointerDown----");
    var_Down = m_Scrollbar.value;
    mNeedMove = false;
  }
  public void OnPointerUp()
  {
    //print("---OnPointerUp----");
    //print("-----m_Scrollbar.value---- " + m_Scrollbar.value);
    for (int i = 0; i < child_Count; i++)
    {
      float num;
      if (var_Down- m_Scrollbar.value<0)
      {
        num = num_list2[i];
      }
      else
      {
        num = num_list3[i];
      }
      if (m_Scrollbar.value <= num)
      {
        if (limit)
        {
          if (i> weizhi)
          {
            weizhi = weizhi + 1;
          }
          else if (i == weizhi)
          {
            weizhi = i;
          }
          else
          {
            weizhi = weizhi - 1;
          }
          mTargetValue = num_list1[weizhi];
        }
        else
        {
          mTargetValue = num_list1[i];
          weizhi = i;  //頁數
        }
        break;
      }
    }

    mNeedMove = true;
    mMoveSpeed = 0;
  }
  /// <summary>
  /// 直接跳轉到指定頁數
  /// </summary>
  /// <param name="頁數"></param>
  public void OnButtonClick(int value)
  {
    mTargetValue = num_list1[value];
    if (mTargetValue<0)
    {
      mTargetValue = 0;
    }
    if (mTargetValue>1)
    {
      mTargetValue = 1;
    }
    weizhi = value;
    mNeedMove = true;
  }
  void Update()
  {
    if (mNeedMove)
    {
      if (Mathf.Abs(m_Scrollbar.value - mTargetValue) < 0.01f)
      {
        m_Scrollbar.value = mTargetValue;
        mNeedMove = false;
        MoveEnd();
        return;
      }
      m_Scrollbar.value = Mathf.SmoothDamp(m_Scrollbar.value, mTargetValue, ref mMoveSpeed, SMOOTH_TIME);
    }
  }
  /// <summary>
  /// 移動完畢的回調
  /// </summary>
  public void MoveEnd()
  {
    print("----- 移動完畢-----第幾頁--- "+ (weizhi+1));
  }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: