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 GameObject personItemPrefab; // 用于显示人员信息的UI项 public GameObject scenePanelPrefab; [Header("组件")] public Text sceneText; public Text nameText; public Text dutyText; [Header("数据")] private GameObject currentSelectedItem = null; // 当前选中的项 private Vector3 originalScale; // 记录选中项的原始大小 // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { //SetPlate(); } public void SetPlate() { // 清空当前显示 foreach (Transform child in managerPanelContent) { Debug.Log("Destroying: " + child.gameObject.name); Destroy(child.gameObject); } // 遍历字典中的每个场景,生成对应的UI项 foreach (var sceneEntry in panelScript.sceneDataDictionary) { CreateScenePanel(sceneEntry.Key, sceneEntry.Value); // 为每个场景调用封装的方法 } } // 根据场景名称和对应的人员列表生成场景面板和人员项 private void CreateScenePanel(string sceneName, List peopleList) { // 为每个场景生成一个新的面板 GameObject scenePanel = Instantiate(scenePanelPrefab, panelContent); // 在每个场景面板上找到场景名称的文本组件并设置 Text sceneText = scenePanel.transform.Find("top/sceneName").GetComponent(); sceneText.text = sceneName; // 遍历该场景下的所有人员,生成对应的UI项 foreach (SelectedInfo person in peopleList) { CreatePersonItem(scenePanel, person); // 调用封装方法生成人员项 } } // 根据人员信息生成一个UI项 private void CreatePersonItem(GameObject scenePanel, SelectedInfo person) { // 实例化一个人员项并将其作为场景面板的子物体 GameObject item = Instantiate(personItemPrefab, managerPanelContent); // 获取并设置人员信息 Text nameText = item.transform.Find("name").GetComponent(); Text dutyText = item.transform.Find("Profession").GetComponent(); Debug.Log("----------------------" + nameText.text); Debug.Log("++++++++++++++++++++++" + dutyText.text); nameText.text = person.name; // 设置姓名 dutyText.text = person.duty; // 设置职责 } // 点击项时调用 //public void OnItemClicked(GameObject clickedItem) //{ // // 如果有之前选中的项,恢复其原始大小 // if (currentSelectedItem != null) // { // RectTransform rt = currentSelectedItem.GetComponent(); // rt.localScale = originalScale; // 恢复原始大小 // } // // 更新当前选中的项 // currentSelectedItem = clickedItem; // RectTransform clickedItemRT = clickedItem.GetComponent(); // // 放大当前选中的项 // clickedItemRT.localScale = originalScale * 1.5f; // 放大1.5倍 // // 使当前选中的项居中显示 // ScrollRect scrollRect = panelContent.GetComponentInParent(); // if (scrollRect != null) // { // float targetX = clickedItemRT.position.x - scrollRect.viewport.position.x; // Vector3 targetPos = new Vector3(targetX, clickedItemRT.position.y, clickedItemRT.position.z); // StartCoroutine(SmoothScrollToPosition(targetPos, 0.5f)); // 平滑滚动到目标位置 // } //} //// 平滑滚动到目标位置 //private IEnumerator SmoothScrollToPosition(Vector3 targetPosition, float duration) //{ // Vector3 startPos = panelContent.position; // float time = 0; // while (time < duration) // { // panelContent.position = Vector3.Lerp(startPos, targetPosition, time / duration); // time += Time.deltaTime; // yield return null; // } // panelContent.position = targetPosition; //} }