using Core.Utilities;
|
using DG.Tweening;
|
using MoreMountains.NiceVibrations;
|
using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using UnityEngine.UI;
|
|
/// <summary>
|
/// 主界面底部的Tab按钮界,确保动画正确,按钮显示正确。
|
/// </summary>
|
public class BottomMainTab : MonoBehaviour
|
{
|
/// <summary>
|
/// 按钮列表.
|
/// </summary>
|
public List<Button> mainButtonList;
|
|
/// <summary>
|
/// 主界面的Panel列表
|
/// </summary>
|
public List<Image> panelList;
|
|
/// <summary>
|
/// 选中Tab的背景.
|
/// </summary>
|
public Image selectBtnBg;
|
|
/// <summary>
|
/// 主界面视图:
|
/// </summary>
|
public PageView mainPageView;
|
|
/// <summary>
|
/// 当前正在选择的按钮.
|
/// </summary>
|
protected Button currentSelectBtn;
|
/// <summary>
|
/// 选择按钮在队列内的索引.
|
/// </summary>
|
protected int selectIdx = 2;
|
|
/// <summary>
|
/// 几个关键的位置信息
|
/// </summary>
|
protected float defaultBtnWidth = 120.0f;
|
protected float selectBgWidth = 240.0f;
|
protected float halfScreenWidth = 360.0f;
|
|
protected Timer pageViewAdjTimer;
|
|
|
// Start is called before the first frame update
|
void Start()
|
{
|
this.onButtonSelected(2);
|
}
|
|
|
/// <summary>
|
/// 点击按钮,触发对应的震动数据
|
/// </summary>
|
public void onTouchButton()
|
{
|
MMVibrationManager.Haptic(HapticTypes.Success);
|
}
|
|
public void onClickShop()
|
{
|
onButtonSelected(0);
|
}
|
public void onClickHero()
|
{
|
onButtonSelected(1);
|
}
|
public void onClickCombat()
|
{
|
onButtonSelected(2);
|
}
|
public void onClickActivity()
|
{
|
onButtonSelected(3);
|
}
|
public void onClickGuild()
|
{
|
onButtonSelected(4);
|
}
|
|
/// <summary>
|
/// 某一个索引的按钮被选中.
|
/// </summary>
|
/// <param name="idx"></param>
|
protected void onButtonSelected( int idx)
|
{
|
selectIdx = idx;
|
// 调整主Tab按钮的位置信息.
|
onButtonSelectAdjPos(idx);
|
// 调整每一个Tab按钮的显示信息.
|
for (int ti = 0; ti < mainButtonList.Count; ti++)
|
{
|
if (ti == selectIdx)
|
{
|
ButtonSelectDisplay(mainButtonList[ti]);
|
}
|
else
|
{
|
ButtonUnselDisplay(mainButtonList[ti]);
|
}
|
}
|
|
// 调整主Panel数据
|
//onMainPanelAdjPos(selectIdx);
|
mainPageView.pageTo(selectIdx);
|
}
|
|
/// <summary>
|
/// 页面切换相关的内容
|
/// </summary>
|
/// <param name="index"></param>
|
public void onPageSwith( int idx )
|
{
|
selectIdx = idx;
|
// 调整主Tab按钮的位置信息.
|
onButtonSelectAdjPos(idx);
|
// 调整每一个Tab按钮的显示信息.
|
for (int ti = 0; ti < mainButtonList.Count; ti++)
|
{
|
if (ti == selectIdx)
|
{
|
ButtonSelectDisplay(mainButtonList[ti]);
|
}
|
else
|
{
|
ButtonUnselDisplay(mainButtonList[ti]);
|
}
|
}
|
}
|
|
/// <summary>
|
/// 调整每一个主Panel的位置信息.
|
/// 算法描述:
|
/// 选中的idx Panel的位置是0.
|
/// 向前设置每一个Panel.
|
/// 向后设置每一个Panel.
|
/// </summary>
|
/// <param name="idx"></param>
|
protected void onMainPanelAdjPos( int idx )
|
{
|
if ((idx < 0) || (idx >= panelList.Count) )
|
return;
|
|
RectTransform tr;
|
tr = panelList[idx].GetComponent<RectTransform>();
|
tr.DOKill();
|
tr.DOAnchorPosX(0, 0.4f);
|
|
// 向前设置.
|
float xMove = 0;
|
for( int ti = idx-1;ti>=0;ti --)
|
{
|
xMove -= 720;
|
tr = panelList[ti].GetComponent<RectTransform>();
|
tr.DOKill();
|
tr.DOAnchorPosX(xMove, 0.4f);
|
}
|
|
// 向后设置.
|
xMove = 0;
|
for( int ti = idx + 1;ti <panelList.Count;ti ++)
|
{
|
xMove += 720;
|
tr = panelList[ti].GetComponent<RectTransform>();
|
tr.DOKill();
|
tr.DOAnchorPosX(xMove, 0.4f);
|
}
|
|
return;
|
}
|
|
/// <summary>
|
/// 处理某一个按钮被选中,需要重新设置位置等相关的信息
|
/// 算法描述:
|
/// 1:先设置所有按钮的位置信息,根据不同被选中的按钮。
|
/// 2:再设置选中背景的位置信息.
|
/// 3: 设置选中和非选中按钮的显示相关信息。
|
/// </summary>
|
/// <param name="idx"></param>
|
protected void onButtonSelectAdjPos( int idx)
|
{
|
float posStart = defaultBtnWidth / 2.0f;
|
//Vector2 tpos;
|
RectTransform tr;
|
|
// 设置选中按钮之前的按钮位置信息.
|
for ( int ti = 0;ti<idx;ti ++)
|
{
|
tr = mainButtonList[ti].GetComponent<RectTransform>();
|
if( tr)
|
{
|
/*
|
tpos = tr.anchoredPosition;
|
tpos.x = posStart;
|
tr.anchoredPosition = tpos;*/
|
tr.DOKill();
|
tr.DOAnchorPosX(posStart, 0.25f);
|
}
|
posStart += defaultBtnWidth;
|
}
|
|
// 设置选中按钮的位置信息和BackGround的位置信息.
|
posStart -= defaultBtnWidth;
|
posStart += (defaultBtnWidth / 2.0f);
|
posStart += (selectBgWidth / 2.0f);
|
tr = mainButtonList[idx].GetComponent<RectTransform>();
|
if( tr)
|
{
|
/*
|
tpos = tr.anchoredPosition;
|
tpos.x = posStart;
|
tr.anchoredPosition = tpos;
|
*/
|
tr.DOKill();
|
tr.DOAnchorPosX(posStart, 0.25f);
|
}
|
tr = selectBtnBg.GetComponent<RectTransform>();
|
if (tr)
|
{
|
tr.DOKill();
|
tr.DOAnchorPosX(posStart, 0.25f);
|
}
|
posStart += (defaultBtnWidth / 2.0f);
|
posStart += (selectBgWidth / 2.0f);
|
|
// 设置选中位置按钮之后的位置信息。
|
for(int ti = idx + 1;ti<mainButtonList.Count;ti ++)
|
{
|
tr = mainButtonList[ti].GetComponent<RectTransform>();
|
if (tr)
|
{
|
/*
|
tpos = tr.anchoredPosition;
|
tpos.x = posStart;
|
tr.anchoredPosition = tpos;*/
|
tr.DOKill();
|
tr.DOAnchorPosX(posStart, 0.25f);
|
}
|
posStart += defaultBtnWidth;
|
}
|
}
|
|
protected void ButtonSelectDisplay( Button btn )
|
{
|
btn.image.color = new Color32(255, 255, 255, 0);
|
|
|
Transform[] father = btn.GetComponentsInChildren<Transform>();
|
foreach (var child in father)
|
{
|
Image timg;
|
if ( child.name == "imgBig")
|
{
|
timg = child.GetComponent<Image>();
|
if (timg)
|
timg.color = new Color32(255, 255, 255, 255);
|
}
|
else
|
{
|
timg = child.GetComponent<Image>();
|
if (timg)
|
timg.color = new Color32(255, 255, 255, 0);
|
}
|
}
|
}
|
|
protected void ButtonUnselDisplay( Button btn)
|
{
|
Transform[] father = btn.GetComponentsInChildren<Transform>();
|
foreach (var child in father)
|
{
|
Image timg;
|
if (child.name == "imgBig")
|
{
|
timg = child.GetComponent<Image>();
|
if (timg)
|
timg.color = new Color32(255, 255, 255, 0);
|
}
|
else
|
{
|
timg = child.GetComponent<Image>();
|
if (timg)
|
timg.color = new Color32(255, 255, 255, 255);
|
}
|
}
|
}
|
|
// Update is called once per frame
|
void Update()
|
{
|
}
|
}
|