175 lines
5.2 KiB
C#
175 lines
5.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class TabItem : MonoBehaviour
|
|
{
|
|
public CustomItem customItem;
|
|
|
|
public Image showIcon;
|
|
public Text nameText;
|
|
public Sprite nochoseSprite;
|
|
public Sprite choseSprite;
|
|
public Font nochoseFont;
|
|
public Font choseFont;
|
|
|
|
|
|
public Vector2 posPercent;
|
|
private RectTransform parentRectTransform;
|
|
private RectTransform tabRectTransform;
|
|
private RectTransform textRectTransform;
|
|
private RectTransform layoutRectTransform;
|
|
|
|
/// <summary>
|
|
/// 办事处按钮
|
|
/// </summary>
|
|
public List<WorkPlaceItem> workPlaceItems;
|
|
void Awake()
|
|
{
|
|
parentRectTransform = transform.parent.GetComponent<RectTransform>();
|
|
tabRectTransform=GetComponent<RectTransform>();
|
|
textRectTransform = nameText.transform.GetComponent<RectTransform>();
|
|
layoutRectTransform = transform.Find("layout").GetComponent<RectTransform>();
|
|
|
|
#if UNITY_EDITOR
|
|
Debug.Log("地图比例:"+gameObject.name + " " + tabRectTransform.anchoredPosition.x / parentRectTransform.rect.width + " " + tabRectTransform.anchoredPosition.y / parentRectTransform.rect.height);
|
|
#endif
|
|
|
|
if (customItem != null)
|
|
{
|
|
List<Button> btns = transform.Find("layout").GetComponentsInChildren<Button>(true).ToList();
|
|
for (int i = 0; i < btns.Count; i++)
|
|
{
|
|
int index = i;
|
|
//点击办事处按钮
|
|
btns[index].onClick.AddListener(() =>
|
|
{
|
|
workPlaceItems[index].Click();
|
|
FirstPanel.instance.tab.GetComponentsInChildren<TabItem>().ToList().ForEach(a =>
|
|
{
|
|
if (a != this)
|
|
{
|
|
a.SetNoChoseState();
|
|
}
|
|
else
|
|
{
|
|
//标签状态选中
|
|
a.PointIn();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
bool isin=false;
|
|
|
|
void Update()
|
|
{
|
|
tabRectTransform.anchoredPosition = new Vector2(posPercent.x * parentRectTransform.rect.width, posPercent.y * parentRectTransform.rect.height);
|
|
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(textRectTransform, Input.mousePosition, null, out Vector2 localPos);
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(layoutRectTransform, Input.mousePosition, null, out Vector2 localPos2);
|
|
//检测鼠标是否在tabtext里面或者layout里
|
|
if (textRectTransform.rect.Contains(localPos) || layoutRectTransform.rect.Contains(localPos2))
|
|
{
|
|
if (!isin)
|
|
{
|
|
isin = true;
|
|
PointIn();
|
|
Debug.Log("进入");
|
|
};
|
|
}
|
|
else
|
|
{
|
|
if (isin)
|
|
{
|
|
isin = false;
|
|
PointOut();
|
|
Debug.Log("离开");
|
|
};
|
|
}
|
|
|
|
}
|
|
|
|
public void PointIn()
|
|
{
|
|
showIcon.gameObject.SetActive(true);
|
|
GetComponent<Image>().sprite = choseSprite;
|
|
//联动
|
|
if (customItem != null)
|
|
{
|
|
nameText.fontSize = 80;
|
|
nameText.font = choseFont;
|
|
nameText.color= Color.white;
|
|
customItem.PointIn(null);
|
|
//显示箭头
|
|
transform.Find("箭头").gameObject.SetActive(true);
|
|
}
|
|
|
|
//显示办事处
|
|
transform.Find("layout").gameObject.SetActive(true);
|
|
}
|
|
|
|
public void PointOut()
|
|
{
|
|
//选中此关区,不切换状态
|
|
if (customItem != null && FirstPanel.currentChoseCustonItem == customItem)
|
|
{
|
|
return;
|
|
}
|
|
|
|
SetNoChoseState();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 未选中状态
|
|
/// </summary>
|
|
public void SetNoChoseState()
|
|
{
|
|
showIcon.gameObject.SetActive(false);
|
|
GetComponent<Image>().sprite = nochoseSprite;
|
|
//联动
|
|
if (customItem != null)
|
|
{
|
|
nameText.fontSize = 60;
|
|
nameText.font = nochoseFont;
|
|
nameText.color = new Color(148f / 255f, 184f / 255f, 1, 1);
|
|
customItem.PointOut(null);
|
|
//隐藏箭头
|
|
transform.Find("箭头").gameObject.SetActive(false);
|
|
}
|
|
//隐藏办事处
|
|
transform.Find("layout").gameObject.SetActive(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 点击海关标签
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
public void Click(BaseEventData data)
|
|
{
|
|
if (customItem != null)
|
|
{
|
|
//点海关,进入海关页面
|
|
customItem.EnterCity();
|
|
//其他状态取消
|
|
FirstPanel.instance.tab.GetComponentsInChildren<TabItem>().ToList().ForEach(a =>
|
|
{
|
|
if(a!=this)
|
|
{
|
|
a.SetNoChoseState();
|
|
}
|
|
else
|
|
{
|
|
//标签状态选中
|
|
a.PointIn();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|