_xiaofang/xiaofang/Assets/Script/UI/PanelUI/ManagerPanel.cs

140 lines
4.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//板块信息
public class ManagerPanel : MonoBehaviour
{
public Panel panelScript; // 引用Panel类的实例
public Transform managerPanelContent; // 管理面板中用于显示人员的UI容器
public Transform panelContent;
public Transform personnelPanelCount;//不包含职责的容器
public GameObject personItemPrefab; // 用于显示人员信息的UI项
public GameObject scenePanelPrefab;
public GameObject peopleWindowsPrefab;//人员弹窗
public GameObject personnelPanel;//不包含职责的区域信息面板
public JSONReader jsonReader;
[Header("组件")]
public Text sceneText;
public ScrollRect scrollRect; // 引用ScrollRect组件处理滑动
[Header("数据")]
private GameObject currentSelectedItem = null; // 当前选中的项
private Vector3 originalScale; // 记录选中项的原始大小
public Panel panelInfo;
public PersonnelPanel personnelPanel2;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
// 根据场景名称和对应的人员列表生成场景面板和人员项
public void CreateScenePanel(string sceneName, List<SelectedInfo> peopleList,GameObject parentTransform)
{
if (personItemPrefab == null)
{
Debug.LogError("personItemPrefab 没有被正确赋值!");
}
// 遍历该场景下的所有人员生成对应的UI项
foreach (SelectedInfo person in peopleList)
{
CreatePersonItem(person, parentTransform); // 传递 managerPanelContent
}
}
// 根据人员信息生成一个UI项
private void CreatePersonItem(SelectedInfo person, GameObject parentTransform)
{
Transform contentTransform = parentTransform.transform.Find("Scroll View/Viewport/Content");
// 实例化一个人员项并将其作为场景面板的子物体
GameObject item = Instantiate(personItemPrefab, contentTransform);
ArrangementItem arrangementItem = item.GetComponent<ArrangementItem>();
arrangementItem.name.text = person.name; // 设置姓名
arrangementItem.duty.text = person.duty; // 设置职责
}
// 点击项时调用
public void OnItemClicked(GameObject clickedItem)
{
// 如果有之前选中的项,恢复其原始大小
if (currentSelectedItem != null)
{
RectTransform rt = currentSelectedItem.GetComponent<RectTransform>();
rt.localScale = originalScale; // 恢复原始大小
}
// 记录当前选中项的原始大小
RectTransform clickedItemRT = clickedItem.GetComponent<RectTransform>();
if (originalScale == Vector3.zero)
{
originalScale = clickedItemRT.localScale; // 只记录一次原始大小
}
// 更新当前选中的项
currentSelectedItem = clickedItem;
// 放大当前选中的项
clickedItemRT.localScale = originalScale * 1.2f; // 放大1.2倍
// 使当前选中的项居中显示
//CenterOnItem(clickedItemRT);
}
// 使选中的项居中显示
private void CenterOnItem(RectTransform item)
{
// 获取ScrollView的Viewport位置
RectTransform viewportRect = scrollRect.viewport.GetComponent<RectTransform>();
// 计算选中项相对于视口的偏移量
float itemMinX = item.localPosition.x - item.rect.width / 2;
float itemMaxX = item.localPosition.x + item.rect.width / 2;
// 获取Scroll Rect的宽度
float viewportWidth = viewportRect.rect.width;
// 计算Normalized Position
float normalizedPos = Mathf.InverseLerp(itemMinX, itemMaxX, 0);
// 设置Scroll Rect的normalizedPosition来使项目居中
scrollRect.horizontalNormalizedPosition = normalizedPos;
}
//关闭界面
public void OnClickCloseBtn()
{
transform.gameObject.SetActive(false);
}
//人员弹窗及加载人员面板
public void OnClickPeopleWindows()
{
peopleWindowsPrefab.gameObject.SetActive(true);
foreach (Transform child in personnelPanelCount)
{
Destroy(child.gameObject);
}
foreach (var sceneEntry in panelInfo.sceneDataDictionary)
{
GameObject managerPanelInstance = Instantiate(personnelPanel, personnelPanelCount);
PersonnelPanel scenetext = managerPanelInstance.GetComponent<PersonnelPanel>();
foreach(var item in panelInfo.peopleList)
scenetext.personelPanelText.text = sceneEntry.Key;
scenetext.personelNumText.text = sceneEntry.Value.Count.ToString();
scenetext.CreatePeopleItem(sceneEntry.Value);
}
//CreatePeopleItem();
}
//关闭人员窗口
public void OnCloseWindowsBtn()
{
peopleWindowsPrefab.gameObject.SetActive(false);
}
}