最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

unity實現(xiàn)翻頁按鈕功能

 更新時間:2020年04月16日 10:05:52   作者:貪玩的孩紙時代  
這篇文章主要為大家詳細介紹了unity實現(xiàn)翻頁按鈕功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了unity實現(xiàn)翻頁按鈕功能的具體代碼,供大家參考,具體內(nèi)容如下

效果圖:

UI子父級關系:

代碼中也都有加入注釋,有不懂可私信我。腳本中用到了對象池,我沒有上傳,可根據(jù)自己需求做相應變動。

腳本:PageBtnPanelC

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
/// <summary>
/// 分頁按鈕面板控制器
/// </summary>
public class PageBtnPanelC : MonoBehaviour {
 private HorizontalLayoutGroup self_HLG;
 /// <summary>
 /// 上一頁按鈕
 /// </summary>
 private Button lastPageBtn;
 /// <summary>
 /// 下一頁按鈕
 /// </summary>
 private Button nextPageBtn;
 /// <summary>
 /// 頁數(shù)的父物體
 /// </summary>
 private RectTransform pageBtnParent;
 private HorizontalLayoutGroup pageBtnParent_HLG;
 /// <summary>
 /// 上一頁按鈕點擊事件
 /// </summary>
 private UnityAction<int> lastPageBtnEvent;
 /// <summary>
 /// 下一頁按鈕點擊事件
 /// </summary>
 private UnityAction<int> nextPageBtnEvent;
 /// <summary>
 /// 當前顯示頁面的下標
 /// </summary>
 private int _currentShowPageIndex = 1;
 public int CurrentShowPageIndex {
  get {
   return _currentShowPageIndex;
  }
  set {
   _currentShowPageIndex = value;
  }
 }
 /// <summary>
 /// 總的頁面數(shù)
 /// </summary>
 private int totalPageNumber = 0;
 /// <summary>
 /// 顯示按鈕的個數(shù) 奇數(shù)個
 /// </summary>
 [Header("必須是奇數(shù)個,且小于總頁數(shù)")]
 private int _showBtnCount = 5;
 public int ShowBtnCount {
  get {
   if (_showBtnCount > totalPageNumber) {
    _showBtnCount = totalPageNumber;
   }
 
   return _showBtnCount;
  }
  set {
   if (value % 2 == 0)
   {
    _showBtnCount = value - 1;
   }
   else {
    _showBtnCount = value;
   }
  }
 }
 /// <summary>
 /// 頁數(shù)按鈕 預設體
 /// </summary>
 public GameObject btnPrefabs;
 /// <summary>
 /// 頁數(shù)按鈕 存放list
 /// </summary>
 public List<PageBtnC> pbcList;
 
 private void Start()
 {
  Init();
  Set(14, 9, (index1) =>
  {
   Debug.Log("當前顯示第:" + CurrentShowPageIndex + "頁");
  }, (index2) =>
  {
   Debug.Log("當前顯示第:" + CurrentShowPageIndex + "頁");
  }, (_pageIndex, _pbc) =>
  {
   Debug.Log("當前顯示第:" + CurrentShowPageIndex + "頁");
  });
 }
 /// <summary>
 /// 改變顯示的狀態(tài)
 /// </summary>
 void ChangeShowState() {
  int _showBtnCount = ShowBtnCount;
  /*
   判斷是否在可更新范圍內(nèi),如果在更新范圍內(nèi),則將CurrentShowPageIndex設置為中心位置的按鈕
   eg:假設總共有10頁(totalPageNumber = 10),顯示按鈕的個數(shù)為7(ShowBtnCount = 7)
   則應該在 (ShowBtnCount / 2 + 1) = 4 到 totalPageNumber - ShowBtnCount / 2 = 7 之間設置
   如果CurrentShowPageIndex = 5或6
   則應該這樣顯示  1.. 3 4 5 6 7 ..10
   如果不在更新范圍內(nèi),
   如果CurrentShowPageIndex = 1或2或3或4  則應該這樣顯示: 1 2 3 4 5 6 ..10
   如果如果CurrentShowPageIndex = 7或8或9或10 則應該這樣顯示 1.. 5 6 7 8 9 10
   */
  if (CurrentShowPageIndex >= (ShowBtnCount / 2 + 1) && CurrentShowPageIndex <= (totalPageNumber - ShowBtnCount / 2))
  {
   int _showBtnCount2 = _showBtnCount - 2;
   _showBtnCount2 /= 2;
 
   //判斷起始下標
   int startIndex = CurrentShowPageIndex - _showBtnCount2;
   int endIndex = CurrentShowPageIndex + _showBtnCount2;
 
   //防止超出
   if (startIndex <= 1)
   {
    startIndex = 2;
   }
   //防止超出
   if (endIndex >= totalPageNumber)
   {
    endIndex = totalPageNumber - 1;
   }
 
   //計算中心位置按鈕的下標 因為showBtnCount不定
   int centerIndex = ShowBtnCount / 2;
 
   pbcList[centerIndex].Set(CurrentShowPageIndex);
 
   //循環(huán)設置前面一部分按鈕信息
   for (int i = 1; i < centerIndex; i++)
   {
    pbcList[i].Set(startIndex++);
   }
 
   //循環(huán)設置后面一部分按鈕信息
   for (int i = centerIndex + 1; i < ShowBtnCount - 1; i++)
   {
    startIndex++;
    pbcList[i].Set(startIndex);
   }
  }
  else {
   //如果點擊的是小于等于4的按鈕下標
   if (CurrentShowPageIndex < (ShowBtnCount / 2 + 1))
   {
    for (int i = 0; i < ShowBtnCount - 1; i++) {
     pbcList[i].Set(i+1);
    }
   }//如果點擊的事大于等于7的按鈕下標
   else if (CurrentShowPageIndex > (totalPageNumber - ShowBtnCount / 2)) {
 
    int startNumber = totalPageNumber - ShowBtnCount + 2;
 
    for (int i = 1; i < ShowBtnCount; i++) {
     pbcList[i].Set(startNumber++);
    }
   }
  }
 
  /*
   判斷總顯示頁數(shù)是否大于顯示頁數(shù)
   以防止出現(xiàn)這種效果:
   例如:totalPageNumber = 7,ShowBtnCount =7
   防止出現(xiàn)的效果:1 2 3 4 5 6 ..7 和 1.. 2 3 4 5 6 7
   應該出現(xiàn)的效果:1 2 3 4 5 6 7
   */
  if (totalPageNumber > ShowBtnCount){
   _showBtnCount -= 2;
   _showBtnCount /= 2;
   if (CurrentShowPageIndex - _showBtnCount - 1 > 1)
   {
    pbcList[0].Set(1, "1..");
   }
   else
   {
    pbcList[0].Set(1);
   }
   if (CurrentShowPageIndex + _showBtnCount + 1 < totalPageNumber)
   {
    pbcList[ShowBtnCount - 1].Set(totalPageNumber, ".." + totalPageNumber);
   }
   else
   {
    pbcList[ShowBtnCount - 1].Set(totalPageNumber);
   }
  }
 }
 
 private bool isInit = false;
 public void Init() {
  if (isInit)
   return;
  isInit = true;
 
  pbcList = new List<PageBtnC>();
 
  self_HLG = transform.GetComponent<HorizontalLayoutGroup>();
 
  pageBtnParent = transform.Find("PageIndexParent") as RectTransform;
  pageBtnParent_HLG = pageBtnParent.GetComponent<HorizontalLayoutGroup>();
 
  lastPageBtn = transform.Find("LastPageBtn").GetComponent<Button>();
  lastPageBtn.onClick.AddListener(()=> {
   if (totalPageNumber <= 0)
    return;
 
   if (CurrentShowPageIndex > 1) {
    CurrentShowPageIndex--;
   }
 
   ResetPageBtnState();
 
   ChangeShowState();
 
   PageBtnC pbc = GetPBCFromIndex(CurrentShowPageIndex);
   if (pbc != null) {
    pbc.SetHighlightColor();
   }
 
   if (lastPageBtnEvent != null)
   {
    lastPageBtnEvent(CurrentShowPageIndex);
   }
  });
 
  nextPageBtn = transform.Find("NextPageBtn").GetComponent<Button>();
  nextPageBtn.onClick.AddListener(()=> {
   if (totalPageNumber <= 0)
    return;
 
   if (CurrentShowPageIndex < totalPageNumber) {
    CurrentShowPageIndex++;
   }
 
   ResetPageBtnState();
 
   ChangeShowState();
   
   PageBtnC pbc = GetPBCFromIndex(CurrentShowPageIndex);
   if (pbc != null)
   {
    pbc.SetHighlightColor();
   }
 
   if (nextPageBtnEvent != null)
   {
    nextPageBtnEvent(CurrentShowPageIndex);
   }
 
  });
 }
 /// <summary>
 /// 設置信息
 /// </summary>
 /// <param name="totalPageNumber">總頁數(shù)</param>
 /// <param name="showBtnCount">顯示多少個按鈕,填奇數(shù),如果填偶數(shù),則會強制減1,如:填6,則實際為5</param>
 /// <param name="lastBtnEvent">上一頁按鈕事件</param>
 /// <param name="nextBtnEvent">下一頁按鈕事件</param>
 /// <param name="pageBtnClickEvent">單獨點擊頁面按鈕事件</param>
 public void Set(int totalPageNumber,int showBtnCount,UnityAction<int> lastBtnEvent,UnityAction<int> nextBtnEvent,UnityAction<int,PageBtnC> pageBtnClickEvent) {
  if (totalPageNumber <= 0)
  {
   this.totalPageNumber = 1;
  }
  else {
   this.totalPageNumber = totalPageNumber;
  }
  
  this.ShowBtnCount = showBtnCount;
 
  this.lastPageBtnEvent = lastBtnEvent;
  this.nextPageBtnEvent = nextBtnEvent;
 
  CurrentShowPageIndex = 1;
 
  pbcList.Clear();
 
  for (int i = 1; i <= ShowBtnCount; i++) {
   int index = i;
   PageBtnC pbc = PoolManager.Instance.Spawn(btnPrefabs, pageBtnParent).GetComponent<PageBtnC>();
   if (pbc) {
    pbc.Set(index,null, (pageIndex,pbc111) =>
    {
     CurrentShowPageIndex = pageIndex;
 
     ResetPageBtnState();
 
     ChangeShowState();
 
     PageBtnC pbc1 = GetPBCFromIndex(CurrentShowPageIndex);
     if (pbc1 != null)
     {
      pbc1.SetHighlightColor();
     }
     if (pageBtnClickEvent != null) {
      pageBtnClickEvent(pageIndex, pbc111);
     }
    });
   }
   pbcList.Add(pbc);
  }
 
  pbcList[0].SetHighlightColor();
 
  ChangeShowState();
 
  Util.SetWidth_ChildWidthSame(pageBtnParent_HLG, pageBtnParent);
 
  Util.SetWidth_ChildWidthNotSame(self_HLG, transform as RectTransform);
 }
 /// <summary>
 /// 重置所有按鈕的狀態(tài)
 /// </summary>
 void ResetPageBtnState() {
  for (int i = 0; i < pbcList.Count; i++) {
   pbcList[i].SetNormalColor();
  }
 }
 /// <summary>
 /// 回收所有頁碼
 /// </summary>
 public void Unspawn() {
  for (int i = pageBtnParent.childCount - 1; i >= 0; i--) {
   
   PoolManager.Instance.UnSpawn(pageBtnParent.GetChild(i).gameObject);
  }
 }
 
 public void Clear() {
  lastPageBtnEvent = null;
  nextPageBtnEvent = null;
 }
 /// <summary>
 /// 根據(jù)index得到pagebtnc物體
 /// </summary>
 /// <param name="index"></param>
 /// <returns></returns>
 PageBtnC GetPBCFromIndex(int index) {
  for (int i = 0; i < pbcList.Count; i++) {
   if (pbcList[i].CurrentPageIndex.Equals(index)) {
    return pbcList[i];
   }
  }
 
  return null;
 }
}
 
public class Util
{
 /// <summary>
 /// 設置物體寬度 子物體寬度相同
 /// </summary>
 /// <param name="parentHLG"></param>
 /// <param name="parent"></param>
 /// <param name="callback"></param>
 public static void SetWidth_ChildWidthSame(HorizontalLayoutGroup parentHLG, RectTransform parent, UnityAction<float> endCallBack = null)
 {
  float width = 0;
 
  float leftPadding = parentHLG.padding.left;
  float spacing = parentHLG.spacing;
 
  int childCount = parent.childCount;
 
  if (childCount > 0)
  {
   RectTransform singleChildRT = parent.GetChild(0) as RectTransform;
 
   width = childCount * singleChildRT.rect.width + (childCount - 1) * spacing + leftPadding;
  }
 
  parent.sizeDelta = new Vector2(width, parent.sizeDelta.y);
 
  if (endCallBack != null)
  {
   endCallBack(width);
  }
 }
 
 /// <summary>
 /// 設置物體寬度 子物體寬度不同
 /// </summary>
 /// <param name="parentHLG"></param>
 /// <param name="parent"></param>
 /// <param name="callback"></param>
 public static void SetWidth_ChildWidthNotSame(HorizontalLayoutGroup parentHLG, RectTransform parent, UnityAction<float> endCallBack = null)
 {
  float width = 0;
 
  RectOffset Padding = parentHLG.padding;
  float spacing = parentHLG.spacing;
 
  int childCount = parent.childCount;
 
  if (childCount > 0)
  {
   for (int i = 0; i < childCount; i++)
   {
    RectTransform childRT = parent.GetChild(i) as RectTransform;
    width += childRT.rect.width;
   }
 
   width += (childCount - 1) * spacing + Padding.left + Padding.right;
  }
 
  parent.sizeDelta = new Vector2(width, parent.sizeDelta.y);
 
  if (endCallBack != null)
  {
   endCallBack(width);
  }
 }
}

腳本:PageBtnC

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
/// <summary>
/// 頁碼按鈕控制器
/// </summary>
public class PageBtnC : MonoBehaviour,IPool {
 /// <summary>
 /// 自己顯示的頁碼
 /// </summary>
 private int currentPageIndex = 0;
 public int CurrentPageIndex {
  get {
   return currentPageIndex;
  }
 }
 
 private Button SelfBtn;
 private Image img;
 private Text selfText;
 
 /// <summary>
 /// 按鈕事件
 /// </summary>
 public UnityAction<int,PageBtnC> btnClickEvent;
 
 /// <summary>
 /// 按鈕正常和高亮顏色
 /// </summary>
 public Color normalColor;
 public Color highlightColor;
 
 /// <summary>
 /// 文本正常和高亮顏色
 /// </summary>
 public Color normalTextColor;
 public Color highlightTextColor;
 
 private bool isInit = false;
 void Init() {
  if (isInit)
   return;
  isInit = true;
 
  img = transform.GetComponent<Image>();
 
  selfText = transform.Find("Text").GetComponent<Text>();
 
  SelfBtn = transform.GetComponent<Button>();
  SelfBtn.onClick.AddListener(()=> {
   
   if (btnClickEvent != null) {
    btnClickEvent(currentPageIndex,this);
   }
  });
 }
 /// <summary>
 /// 設置正常顏色
 /// </summary>
 public void SetNormalColor() {
  img.color = normalColor;
  selfText.color = normalTextColor;
 }
 /// <summary>
 /// 設置高亮顏色
 /// </summary>
 public void SetHighlightColor() {
  img.color = highlightColor;
  selfText.color = highlightTextColor;
 }
 /// <summary>
 /// 設置事件 文本內(nèi)容等
 /// </summary>
 /// <param name="currentPageIndex"></param>
 /// <param name="selfTextStr"></param>
 /// <param name="btnClickEvent"></param>
 public void Set(int currentPageIndex,string selfTextStr = null,UnityAction<int,PageBtnC> btnClickEvent = null) {
  this.currentPageIndex = currentPageIndex;
 
  if (btnClickEvent != null)
  {
   this.btnClickEvent = btnClickEvent;
  }
 
  if (string.IsNullOrEmpty(selfTextStr))
  {
   selfText.text = currentPageIndex.ToString();
  }
  else {
   selfText.text = selfTextStr;
  }
 }
 
 #region 對象池接口方法
 public int GetMaxCount()
 {
  return 10;
 }
 
 public string SingletonName()
 {
  return this.GetType().Name;
 }
 
 public void Spawn()
 {
  Init();
 }
 
 public void UnSpawn()
 {
  SetNormalColor();
 }
 #endregion
}

github地址

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C#中設置textbox限制條件的方法

    C#中設置textbox限制條件的方法

    這篇文章主要介紹了C#中設置textbox限制條件的方法,可實現(xiàn)設置像數(shù)量、價格、金額等的textbox的限制條件,用戶只能輸入數(shù)字或小數(shù),是非常實用的技巧,需要的朋友可以參考下
    2014-12-12
  • C# 通過 inline-asm 解決嵌入x86匯編

    C# 通過 inline-asm 解決嵌入x86匯編

    此篇文章通過C#語言解決嵌入x86匯編,主要通過INline-asm方法來實現(xiàn),下面我通過圖片和代碼的形式給大家分享下,需要的朋友可以參考下
    2015-07-07
  • C# DataTable 轉換為 實體類對象實例

    C# DataTable 轉換為 實體類對象實例

    如果你的實體類與數(shù)據(jù)庫表是完全一致的。上代碼:
    2013-04-04
  • 如何使用C#程序給PDF文件添加編輯域

    如何使用C#程序給PDF文件添加編輯域

    本文主要給大家分享的是通過C#操作PDF類庫iTextSharp來實現(xiàn)在在PDF文檔中填寫日期或簽名之類的能編輯的文本域,非常的簡單實用,有需要的小伙伴可以參考下。
    2017-01-01
  • C#實現(xiàn)Word和ODT文檔相互轉換詳解

    C#實現(xiàn)Word和ODT文檔相互轉換詳解

    ODT文檔格式一種開放文檔格式(OpenDocument Text)。本文以C#及VB.NET代碼展示ODT和Word文檔之間相互轉換的方法,感興趣的可以學習一下
    2022-05-05
  • 詳細聊聊C#的并發(fā)機制優(yōu)秀在哪

    詳細聊聊C#的并發(fā)機制優(yōu)秀在哪

    并發(fā)其實是一個很泛的概念,字面意思就是"同時做多件事",不過方式有所不同,下面這篇文章主要給大家介紹了關于C#并發(fā)機制的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-02-02
  • c#中@的3種作用

    c#中@的3種作用

    本文主要介紹了c#中@的3種作用。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • 在C#中使用適配器Adapter模式和擴展方法解決面向?qū)ο笤O計問題記錄

    在C#中使用適配器Adapter模式和擴展方法解決面向?qū)ο笤O計問題記錄

    在開發(fā)基于MonoGame的游戲框架時,面臨SpriteFont和DynamicSpriteFont兼容問題,SpriteFont在內(nèi)容管道中編譯確定字號,導致不同字號需加載多個字體資源,本文給大家介紹在C#中使用適配器Adapter模式和擴展方法解決面向?qū)ο笤O計問題,感興趣的朋友一起看看吧
    2024-10-10
  • C# winform自定義翻頁控件詳解

    C# winform自定義翻頁控件詳解

    這篇文章主要為大家詳細介紹了C# winform自定義翻頁控件的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • WPF中引入WindowsForms控件的方法

    WPF中引入WindowsForms控件的方法

    這篇文章主要介紹了WPF中引入WindowsForms控件的方法,結合實例形式分析了在WPF中調(diào)用Windows Forms控件的具體步驟與相關實現(xiàn)技巧,需要的朋友可以參考下
    2016-07-07

最新評論

扎囊县| 交口县| 辛集市| 花垣县| 伊宁县| 高碑店市| 江山市| 阜阳市| 江西省| 长丰县| 延吉市| 依安县| 深泽县| 和田县| 昭通市| 聂拉木县| 乐业县| 天等县| 大方县| 永兴县| 固阳县| 德江县| 塔河县| 威远县| 大兴区| 安陆市| 睢宁县| 新田县| 德令哈市| 子洲县| 三亚市| 宝坻区| 徐州市| 云南省| 洪泽县| 湘乡市| 东源县| 娄烦县| 枣强县| 辽中县| 仙游县|