_xiaofang/xiaofang/Assets/Res/gsj/scripts/Drill.cs
2024-12-27 23:12:22 +08:00

87 lines
2.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Drill : MonoBehaviour
{
public Text username;
public Text companyName;
private Button ScheduledBtn;
private bool isadministrator=false; // 是否为管理员
private bool eventAdded = false; // 防止重复添加事件
public delegate void BoolChangedHandler(bool value);
public event BoolChangedHandler OnBoolAChanged;
private Text noRoomText; // "没有演练房间"的文本
private Transform content; // Scroll View Content
// 记录上一次的 isadministrator 状态,用于减少重复执行逻辑
private bool lastIsAdministratorState;
void Start()
{
ScheduledBtn = transform.Find("down/ScheduledBtn").GetComponent<Button>();
ScheduledBtn.gameObject.SetActive(false); // 初始隐藏按钮
noRoomText = transform.Find("Text").GetComponent<Text>();
noRoomText.gameObject.SetActive(false); // 初始隐藏文本
content = transform.Find("Mid/Scroll View/Viewport/Content");
if(GlobalData.ServerData.data.isCreater=="N")
{
username.text = $"[{GlobalData.ServerData.data.userId}](管理员)";
}
else username.text = $"[{GlobalData.ServerData.data.userId}]";
companyName.text = "所属机构:[" + GlobalData.ServerData.data.companyName +"]";
}
void Update()
{
// 实时检测房间列表是否为空,控制 noRoomText 的可见性
noRoomText.gameObject.SetActive(content.childCount == 0);
isadministrator=ReadRoom.instance.isadministrator;
// 检测管理员状态是否发生变化
if (isadministrator != lastIsAdministratorState)
{
lastIsAdministratorState = isadministrator; // 更新状态记录
UpdateScheduledBtnVisibility(); // 更新按钮可见性
}
}
// 根据 isadministrator 状态实时更新 ScheduledBtn 的可见性
private void UpdateScheduledBtnVisibility()
{
if (isadministrator)
{
ScheduledBtn.gameObject.SetActive(true);
// 防止重复添加事件
if (!eventAdded)
{
ScheduledBtn.onClick.AddListener(OnClickScheduleBtn);
eventAdded = true;
}
}
else
{
ScheduledBtn.gameObject.SetActive(false);
}
// 触发事件,通知监听者
OnBoolAChanged?.Invoke(isadministrator);
}
// 演练计划按钮点击事件
void OnClickScheduleBtn()
{
//SceneManager.LoadScene(2);
Debug.Log("点击了演练计划按钮!");
// 这里可以添加具体逻辑
}
}