227 lines
7.1 KiB
C#
227 lines
7.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.InteropServices;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
public class SelectedInfo
|
|
{
|
|
public string name;
|
|
public string duty;
|
|
public string scene;
|
|
}
|
|
|
|
|
|
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;//人员的预制体
|
|
|
|
|
|
[Header("数据")]
|
|
public string name, duty, scene;
|
|
private List<GameObject> peopleList = new List<GameObject>(); // 存储所有已加载的人员预制体
|
|
private List<GameObject> filteredPeopleList = new List<GameObject>(); // 存储筛选后的人员列表
|
|
private GameObject selectedPerson = null; // 当前选中的角色
|
|
public SelectedInfo selectedInfo;
|
|
public Dictionary<string, List<SelectedInfo>> sceneDataDictionary = new Dictionary<string, List<SelectedInfo>>();//不同的场景存取不同的人员数据
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
selectedInfo = new SelectedInfo();
|
|
DynamicLoadingPeople();
|
|
}
|
|
|
|
// 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<GameObject>(peoplePrefab, peopleCountent);
|
|
peopleList.Add(item); // 将每个实例化的角色添加到列表中
|
|
}
|
|
}
|
|
//处理人员管理按钮
|
|
public void ClickPersonnelManagement()
|
|
{
|
|
Debug.Log("调用人员管理");
|
|
ManagerPanel.gameObject.SetActive(true);
|
|
}
|
|
|
|
//处理点击确认按钮
|
|
public void ClickSureBtn()
|
|
{
|
|
// 保存选中的人员信息到场景数据字典中
|
|
string sceneName = selectedInfo.scene;
|
|
|
|
// 如果场景字典中没有这个场景,先创建一个空列表
|
|
if (!sceneDataDictionary.ContainsKey(sceneName))
|
|
{
|
|
sceneDataDictionary[sceneName] = new List<SelectedInfo>();
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("没有选场景");
|
|
}
|
|
|
|
// 将当前选中的人员信息添加到对应场景的人员列表中
|
|
sceneDataDictionary[sceneName].Add(selectedInfo);
|
|
|
|
// 打印当前场景人员信息
|
|
Debug.Log($"场景: {sceneName},选中的人员: {selectedInfo.name},职责: {selectedInfo.duty}");
|
|
|
|
// 可以在这里根据需求继续处理选中的数据
|
|
}
|
|
|
|
//处理界面关闭按钮
|
|
public void ClickCloseBtn()
|
|
{
|
|
transform.gameObject.SetActive(false);
|
|
}
|
|
|
|
//获取鼠标点击位置的信息
|
|
public void GetData()
|
|
{
|
|
ManagerPanel managerPanel = new ManagerPanel();
|
|
|
|
// 当鼠标左键按下时进行检测
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
// 确保 raycaster 和 eventSystem 不为空
|
|
if (raycaster == null || eventSystem == null)
|
|
{
|
|
Debug.LogError("Raycaster 或 EventSystem 未正确分配,请在 Inspector 中进行分配。");
|
|
return;
|
|
}
|
|
|
|
// 创建 PointerEventData 来记录点击事件的数据
|
|
PointerEventData pointerData = new PointerEventData(eventSystem);
|
|
pointerData.position = Input.mousePosition;//获取鼠标点击的位置
|
|
|
|
// 用于存储射线检测的结果
|
|
List<RaycastResult> results = new List<RaycastResult>();
|
|
|
|
// 射线检测 UI
|
|
raycaster.Raycast(pointerData, results);
|
|
|
|
// 遍历射线检测的结果
|
|
foreach (RaycastResult result in results)
|
|
{
|
|
// 检测到点击了按钮
|
|
Button clickedButton = result.gameObject.GetComponent<Button>();
|
|
|
|
if (clickedButton != null)
|
|
{
|
|
Text buttonText = clickedButton.GetComponentInChildren<Text>();
|
|
if (buttonText != null && buttonText.tag == Tags.people)//这里可以获取标签为人员的信息
|
|
{
|
|
|
|
name = buttonText.text;
|
|
Debug.Log(name);
|
|
selectedInfo.name = name;
|
|
}
|
|
else if (buttonText != null && buttonText.tag == Tags.scene)//这里可以获取标签为场景的信息
|
|
{
|
|
scene = buttonText.text;
|
|
Debug.Log(scene);
|
|
selectedInfo.scene = scene;
|
|
}
|
|
else if (buttonText != null && buttonText.tag == Tags.duty)//这里可以获取标签为职责的信息
|
|
{
|
|
duty = buttonText.text;
|
|
Debug.Log(duty);
|
|
selectedInfo.duty = duty;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//设置安排文字
|
|
public void SetText()
|
|
{
|
|
arrangeText.text = "[" + name + "]担任[" + duty + "],位于[" + scene + "]";
|
|
}
|
|
//筛选
|
|
public void SetInputFile()
|
|
{
|
|
// 获取输入框内容
|
|
string filterText = shaixuaninputField.text.Trim();
|
|
|
|
// 遍历所有已实例化的人员预制体
|
|
foreach (Transform child in peopleCountent)
|
|
{
|
|
// 获取该子物体上的 Text 组件(假设它存储了姓名)
|
|
Text personNameText = child.GetComponentInChildren<Text>();
|
|
|
|
if (personNameText != null)
|
|
{
|
|
// 比较输入框中的内容与人员姓名
|
|
if (personNameText.text.Contains(filterText))
|
|
{
|
|
// 如果匹配,显示该人员
|
|
child.gameObject.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
// 如果不匹配,隐藏该人员
|
|
child.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// 点击选中角色,改变视觉效果
|
|
public void OnPeopleItemClicked(GameObject clickedItem)
|
|
{
|
|
// 如果有之前选中的角色,重置其视觉效果
|
|
if (selectedPerson != null)
|
|
{
|
|
Text prevText = selectedPerson.GetComponentInChildren<Text>();
|
|
if (prevText != null)
|
|
{
|
|
prevText.fontSize = 32; // 恢复原始字号
|
|
prevText.color = Color.white; // 恢复原始颜色
|
|
}
|
|
}
|
|
|
|
// 设置当前选中的角色为选中状态
|
|
selectedPerson = clickedItem;
|
|
Text personText = clickedItem.GetComponentInChildren<Text>();
|
|
if (personText != null)
|
|
{
|
|
// 字号变大和颜色变更(选中状态)
|
|
personText.fontSize = 36;
|
|
personText.color = Color.yellow; // 选中颜色
|
|
}
|
|
}
|
|
}
|
|
|