using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using Unity.VisualScripting; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.SceneManagement; using UnityEngine.UI; public class SelectedInfo { public string name; public string duty; public string scene; public string leader; } public class Panel : MonoBehaviour { [Header("组件")] public Button choseJuBenSettingBtn; public Button emergencySettingBtn; public Button peoplePublishBtn; public Button materialReserveBtn; public Button dateSelectionBtn; public Button personnelmanagementBtn; public Button setName;//设置人员 public Button setDuty;//设置职责 public Button setScene;//设置场景 public Text arrangeText;//整体安排 public Button sureBtn;//确认信息按钮 public ToggleGroup scenetoggleGroup;//场景的ToggleGroup public InputField shaixuaninputField;//筛选输入框 public GraphicRaycaster raycaster; // 画布上的射线投射器 public EventSystem eventSystem; // 事件系统 [Header("物体")] public GameObject ManagerPanel;//人员管理界面 public Transform peopleCountent;//人员的窗口 public GameObject peoplePrefab;//人员的预制体 public Transform panelContent; public GameObject dutyPrefab;//职责预制体 public Transform dutyCount;//职责的窗口 public GameObject scenePrefab; public Transform sceneCount; public GameObject ManagerPanelPrefab; // 预制体,包含人员管理面板 [Header("数据")] public string name, duty, scene; public List peopleList = new List(); // 存储所有已加载的人员预制体 private List filteredPeopleList = new List(); // 存储筛选后的人员列表 private GameObject selectedPerson = null; // 当前选中的角色 public SelectedInfo selectedInfo; public Dictionary> sceneDataDictionary = new Dictionary>();//不同的场景存取不同的人员数据 public ManagerPanel managerPanel1; public JSONReader jsonReader1; public SelectScenePanel selectScenePanel; public DatePanel datePanel; private bool isPersonSelected = false; // 标志是否选择了人员 private bool isDutySelected = false; // 标志是否选择了职责 private bool isSceneSelected = false; // 标志是否选择了场景 // Start is called before the first frame update void Start() { selectedInfo = new SelectedInfo(); DynamicLoadingPeople(); DynamicLoadingDuty(); DynamicLoadingScene(); } // Update is called once per frame void Update() { GetData(); SetText(); //SetInputFile(); } //动态加载人员 public void DynamicLoadingPeople() { for (int i = 0; i < 20; i++) { GameObject item = GameObject.Instantiate(peoplePrefab, peopleCountent); PeopleItem peopleItem = item.GetComponent(); peopleList.Add(item);// 将每个实例化的角色添加到列表中 } } //动态加载职责 public void DynamicLoadingDuty() { Debug.Log("||||||||||||||||||||||||||||"+ selectScenePanel.eventId); foreach (var npcData in jsonReader1.npcDictionary) { string[] nameSections = npcData.Value.Name.Split('|'); foreach (var section in nameSections) { string[] sectionParts = section.Split(','); if (int.Parse(sectionParts[0])== 2001) { int key = int.Parse(sectionParts[1]); if (jsonReader1.npcDictionary.ContainsKey(key)) { var npcInfo = jsonReader1.npcDictionary[key]; // 在此处可以使用npcInfo做进一步的处理 Debug.Log("Found NPC: " + npcInfo.Name); // 创建DutyItem实例 GameObject item = GameObject.Instantiate(dutyPrefab, dutyCount); DutyItem dutyItem = item.GetComponent(); dutyItem.dutyNameText.text = npcData.Value.Note; dutyItem.leader = npcData.Value.GroupLeader; peopleList.Add(item); } } //GameObject item = GameObject.Instantiate(dutyPrefab, dutyCount); //DutyItem dutyItem = item.GetComponent(); //dutyItem.dutyNameText.text = npcData.Value.Note; //dutyItem.leader = npcData.Value.GroupLeader; //peopleList.Add(item);// 将每个实例化的角色添加到列表中 } } } //动态加载场景 public void DynamicLoadingScene() { foreach (var npcData in jsonReader1.locationDictionary) { // 解析角色限制字段 string roleLimit = npcData.Value.RoleLimit; if (!string.IsNullOrEmpty(roleLimit)) { // 先按“|”分隔 string[] roleLimitSections = roleLimit.Split('|'); // 遍历每个部分(按“|”分隔后得到的数组) bool shouldInstantiate = true; // 用于判断是否需要实例化 foreach (string section in roleLimitSections) { string[] roleLimits = section.Split(','); // 判断当前部分是否包含 "-1" 来决定是否跳过实例化 if (section.Contains("-1")) { if (roleLimits[0] == "-1") { shouldInstantiate = false; // 不实例化该NPC break; // 跳出循环,直接处理下一个NPC } } else { GameObject item = GameObject.Instantiate(scenePrefab, sceneCount); SceneItem sceneItem = item.GetComponent(); // 设置limitNum sceneItem.dutyId = int.Parse(roleLimits[1]); sceneItem.limitNum = int.Parse(roleLimits[2]); sceneItem.sceneName.text = npcData.Value.Note; // 将每个实例化的角色添加到列表中 peopleList.Add(item); } } // 如果不满足实例化条件,跳过当前NPC的实例化 if (!shouldInstantiate) { continue; } } } } //=============================================================按钮和点击事件================================================== //处理人员管理按钮 public void ClickPersonnelManagement() { foreach (Transform child in panelContent) { Destroy(child.gameObject); } foreach (var sceneEntry in sceneDataDictionary) { //Debug.Log($"场景: {sceneEntry.Key},人数: {sceneEntry.Value.Count}"); GameObject managerPanelInstance = Instantiate(ManagerPanelPrefab, panelContent); Button button= managerPanelInstance.GetComponent