_xiaofang/xiaofang/Assets/Script/UI/PanelUI/SelectScenePanel.cs
2024-11-27 02:11:27 +08:00

144 lines
4.5 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 TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
public class SelectScenePanel : MonoBehaviour
{
public GameObject schoolPrefab;//滑动视图预制体
public Transform schoolList;//预制体列表
public ToggleGroup schooltoggleGroup;
public ToggleGroup schoolimageGroup;
public Toggle[] toggleList;
public GameObject scoolSelectBtn;//学校选择按钮
public GameObject schoolChoiceLable;//学校选择界面
public GameObject eventChoiceLable;//事件选择界面
public bool isOpen = false;
public List<SchoolInfo> schoolInfos = new List<SchoolInfo>();//学校信息
public List<EventInfo> eventInfos = new List<EventInfo>();//事件信息
public List<Toggle> difficultyList = new List<Toggle>();
public Button continueBtn;
//数据
public int schoolId;
public string schoolName;
public int eventId;
public string eventName;
public string difficulty;
// Start is called before the first frame update
void Start()
{
schoolChoiceLable.gameObject.SetActive(true);
eventChoiceLable.gameObject.SetActive(false);
//动态加载学校选择预制体
for (int i=0;i<6;i++)
{
GameObject slot = GameObject.Instantiate<GameObject>(schoolPrefab, schoolList);
//schoolPrefab.gameObject.GetComponent<Image>().sprite = Resources.Load("");
Toggle toggle = slot.GetComponent<Toggle>();
if (toggle != null)
{
// 将 Toggle 添加到 ToggleGroup 中
toggle.group = schoolimageGroup;
// 将 Toggle 添加到 toggleList 中
toggleList[i] = toggle;
}
}
continueBtn.onClick.AddListener(OnClickContinueBtn);
}
// Update is called once per frame
void Update()
{
}
//学校选择,确定学校信息
public void SelectSchoolBtn()
{
SchoolInfo gameObject = GetComponentInChildren<SchoolInfo>();
foreach (SchoolInfo item in schoolInfos)
{
if(item.gameObject.transform.GetComponent<Toggle>().isOn)
{
this.schoolId = item.schoolId;
this.schoolName = item.schoolName;
}
}
//Debug.Log("###############1:"+ this.schoolId);
//Debug.Log("###############2:" + this.schoolName);
}
//事件及难度选择
public void SelectEvent()
{
EventInfo eventInfo = GetComponentInChildren<EventInfo>();
foreach(EventInfo item in eventInfos)
{
if (item.gameObject.transform.GetComponent<Toggle>().isOn)
{
this.eventId = item.eventId;
this.eventName = item.eventName;
}
}
//Debug.Log("%%%%%%%%%%%%%1:"+ this.eventId);
//Debug.Log("%%%%%%%%%%%%%2:" + this.eventName);
}
//游戏难度选择
public void SelsctDifficulty()
{
auth_createTemplate auth_CreateTemplate = new auth_createTemplate();
foreach(Toggle item in difficultyList)
{
if (item.isOn)
{
this.difficulty = item.transform.parent.GetComponentInChildren<Text>().text;
auth_CreateTemplate.mode = this.difficulty;//给结构体“难度”赋值
}
}
//Debug.Log("%%%%%%%%%%%%%3:" + this.difficulty);
}
//继续按钮,点击后上传数据
public void OnClickContinueBtn()
{
SelectSchoolBtn();
schoolChoiceLable.gameObject.SetActive(false);
eventChoiceLable.gameObject.SetActive(true);
}
//提交按钮,点击上传数据和隐藏界面
public void SubmitBtn()
{
SelectEvent();
SelsctDifficulty();
this.gameObject.SetActive(false);
}
//通过判断Toggle的IsOn是否被打开来判断继续按钮是否置灰
public void IsClick()
{
bool anyToggleSelected = false;
//continueBtn.transform.parent
foreach (Toggle toggle in toggleList)
{
if (toggle.isOn)
{
anyToggleSelected = true;
break; // 如果有一个 Toggle 被选中,停止检查
}
}
// 获取 ContinueBtn 按钮组件
// 根据是否有 Toggle 被选中,设置 ContinueBtn 是否可交互
if (continueBtn != null)
{
continueBtn.interactable = anyToggleSelected; // 如果有选中的 Toggle继续按钮可交互否则不可交互
}
else
{
Debug.LogError("ContinueBtn 按钮组件未找到!");
}
}
}