409 lines
12 KiB
C#
409 lines
12 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using TMPro;
|
||
using Unity.VisualScripting;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class SelectScenePanel : MonoBehaviour
|
||
{
|
||
[Header("组件")]
|
||
public GameObject scenePrefab;//滑动视图预制体
|
||
public Transform sceneList;//预制体列表
|
||
public GameObject schoolPrefab;//学校按钮预制体
|
||
public Transform schoolList;//学校预制体列表
|
||
public GameObject eventPrefab;//事件按钮预制体
|
||
public Transform eventList;//学校预制体列表
|
||
public ToggleGroup schoolGroup;//学校ToggleGroup
|
||
public ToggleGroup sceneGroup;//场景ToggleGroup
|
||
public ToggleGroup eventGroup;//事件ToggleGroup
|
||
public GameObject scoolSelectBtn;//学校选择按钮
|
||
public GameObject schoolChoiceLable;//学校选择界面
|
||
public GameObject eventChoiceLable;//事件选择界面
|
||
public GameObject mainPanel;
|
||
public List<SchoolInfo> schoolInfoList = new List<SchoolInfo>();//学校信息
|
||
public List<EventInfo> eventInfoList = new List<EventInfo>();//事件信息
|
||
public List<SceneItem> sceneItemList = new List<SceneItem>();//场景信息
|
||
public List<Toggle> difficultyList = new List<Toggle>();//游戏难度
|
||
public Button continueBtn;
|
||
public Toggle[] toggleList;
|
||
public Toggle[] schooltoggleList;
|
||
public Toggle[] eventToggleList;
|
||
|
||
public ToggleGroup toggleGroup;
|
||
[Header("数据")]
|
||
public DatePanel datePanel;
|
||
public int schoolId;
|
||
public string schoolName;
|
||
public string sceneName;
|
||
public int eventId;
|
||
public string eventName;
|
||
public string difficulty;
|
||
public string isBuy;
|
||
// Start is called before the first frame update
|
||
void Start()
|
||
{
|
||
schoolChoiceLable.gameObject.SetActive(true);
|
||
eventChoiceLable.gameObject.SetActive(false);
|
||
InstantiateScenePrefab();
|
||
InstantiateSchoolPrefab();
|
||
InstantiateEventPrefab();
|
||
SetEventToggleOpenorClose(eventInfoList);
|
||
continueBtn.onClick.AddListener(OnClickContinueBtn);
|
||
SetFirstChoise(toggleList);
|
||
SetToggleOpenorClose(sceneItemList);//未购买无法选择
|
||
SetScloolToggleOpenorClose(schoolInfoList);
|
||
|
||
}
|
||
|
||
// Update is called once per frame
|
||
void Update()
|
||
{
|
||
IsClick();
|
||
}
|
||
|
||
//动态加载场景选择预制体
|
||
public void InstantiateScenePrefab()
|
||
{
|
||
for (int i = 0; i < 6; i++)
|
||
{
|
||
GameObject slot = GameObject.Instantiate<GameObject>(scenePrefab, sceneList);
|
||
SceneItem item = slot.GetComponent<SceneItem>();
|
||
|
||
|
||
|
||
item.sceneName.text = "场景" + i;
|
||
if (item.IsOpen)
|
||
{
|
||
item.maskImage.gameObject.SetActive(false);
|
||
}
|
||
else
|
||
{
|
||
item.maskImage.gameObject.SetActive(true);
|
||
}
|
||
Toggle toggle = slot.GetComponent<Toggle>();
|
||
if (toggle != null)
|
||
{
|
||
toggle.group = sceneGroup;
|
||
toggleList[i] = toggle;
|
||
sceneItemList.Add(item);
|
||
}
|
||
}
|
||
}
|
||
|
||
//动态加载学校选择预制体
|
||
public void InstantiateSchoolPrefab()
|
||
{
|
||
for (int i = 0; i < 6; i++)
|
||
{
|
||
GameObject slot = GameObject.Instantiate<GameObject>(schoolPrefab, schoolList);
|
||
SchoolInfo item = slot.GetComponent<SchoolInfo>();
|
||
item.schoolName.text = "南山中学";
|
||
item.schoolId = i;
|
||
Toggle toggle = slot.GetComponent<Toggle>();
|
||
|
||
if (toggle != null)
|
||
{
|
||
// 将 Toggle 添加到 ToggleGroup 中
|
||
toggle.group = schoolGroup;
|
||
|
||
// 将 Toggle 添加到 schooltoggleList 中
|
||
schooltoggleList[i] = toggle;
|
||
|
||
// 将 SchoolInfo 对象添加到 schoolInfos 列表中
|
||
schoolInfoList.Add(item);
|
||
}
|
||
}
|
||
}
|
||
|
||
//动态加载事件选择预制体
|
||
public void InstantiateEventPrefab()
|
||
{
|
||
for (int i = 0; i < 6; i++)
|
||
{
|
||
GameObject slot = GameObject.Instantiate<GameObject>(eventPrefab, eventList);
|
||
EventInfo item = slot.GetComponent<EventInfo>();
|
||
item.eventName.text = "停车场起火";
|
||
item.eventId = i*10;
|
||
Toggle toggle = slot.GetComponent<Toggle>();
|
||
|
||
if (toggle != null)
|
||
{
|
||
// 将 Toggle 添加到 ToggleGroup 中
|
||
toggle.group = schoolGroup;
|
||
toggle.isOn = false;
|
||
|
||
// 将 Toggle 添加到 schooltoggleList 中
|
||
eventToggleList[i] = toggle;
|
||
|
||
// 将 SchoolInfo 对象添加到 schoolInfos 列表中
|
||
eventInfoList.Add(item);
|
||
}
|
||
}
|
||
}
|
||
|
||
//学校选择
|
||
public void SelectSchoolBtn()
|
||
{
|
||
SchoolInfo gameObject = GetComponentInChildren<SchoolInfo>();
|
||
foreach (SchoolInfo item in schoolInfoList)
|
||
{
|
||
if (item.gameObject.transform.GetComponent<Toggle>().isOn)
|
||
{
|
||
this.schoolId = item.schoolId;
|
||
this.schoolName = item.schoolName.ToString();
|
||
}
|
||
}
|
||
Debug.Log("###############1:" + this.schoolId);
|
||
Debug.Log("###############2:" + this.schoolName);
|
||
}
|
||
|
||
//场景选择
|
||
public void SelectSceneBtn()
|
||
{
|
||
bool sceneSelected = false; // 检查是否有场景被选择
|
||
foreach (SceneItem item in sceneItemList)
|
||
{
|
||
// 检查当前的 Toggle 是否被选中
|
||
if (item.gameObject.transform.GetComponent<Toggle>().isOn)
|
||
{
|
||
// 设置场景名称
|
||
this.sceneName = item.sceneName.text; // 获取 Text 组件的文本
|
||
sceneSelected = true;
|
||
break; // 找到选中的场景后退出循环
|
||
}
|
||
}
|
||
Debug.Log("###############3:" + this.schoolId);
|
||
//IsClick();
|
||
}
|
||
|
||
//事件选择
|
||
public void SelectEvnentBtn()
|
||
{
|
||
bool eventSelected = false; // 检查是否有场景被选择
|
||
foreach (EventInfo item in eventInfoList)
|
||
{
|
||
// 检查当前的 Toggle 是否被选中
|
||
if (item.gameObject.transform.GetComponent<Toggle>().isOn)
|
||
{
|
||
// 设置场景名称
|
||
this.eventName = item.eventName.text; // 获取 Text 组件的文本
|
||
eventSelected = true;
|
||
break; // 找到选中的场景后退出循环
|
||
}
|
||
}
|
||
}
|
||
|
||
//事件及难度选择
|
||
public void SelectEvent()
|
||
{
|
||
EventInfo eventInfo = GetComponentInChildren<EventInfo>();
|
||
foreach (EventInfo item in eventInfoList)
|
||
{
|
||
if (item.gameObject.transform.GetComponent<Toggle>().isOn)
|
||
{
|
||
this.eventId = item.eventId;
|
||
this.eventName = item.eventName.text;
|
||
}
|
||
}
|
||
//Debug.Log("%%%%%%%%%%%%%1:"+ this.eventId);
|
||
//Debug.Log("%%%%%%%%%%%%%2:" + this.eventName);
|
||
}
|
||
|
||
//随机事件
|
||
public void OnRandomEventClick()
|
||
{
|
||
//Debug.Log("&&&&&&&&&&&&&&&&&&&&&&&&&7" + eventInfoList.Count);
|
||
// 随机选择一个事件
|
||
if (eventInfoList.Count > 0)
|
||
{
|
||
int randomIndex = Random.Range(0, eventInfoList.Count);
|
||
|
||
// 取消所有Toggle的选中状态
|
||
foreach (Toggle toggle in eventToggleList)
|
||
{
|
||
// 先检查toggle是否为null
|
||
if (toggle != null)
|
||
{
|
||
toggle.isOn = false;
|
||
}
|
||
}
|
||
|
||
// 选中随机事件
|
||
if (eventToggleList[randomIndex] != null&& eventToggleList[randomIndex].GetComponent<EventInfo>().isOpen!=false)
|
||
{
|
||
eventToggleList[randomIndex].isOn = true;
|
||
this.eventName = eventInfoList[randomIndex].eventName.text; // 设置随机选择的事件名称
|
||
Debug.Log("已选择随机事件: " + this.eventName);
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning("随机选择的事件 Toggle 为 null!");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning("事件列表为空!");
|
||
}
|
||
}
|
||
|
||
//游戏难度选择
|
||
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()
|
||
{
|
||
//IsClick(); // 调用 IsClick 检查是否选中学校和场景
|
||
SelectSchoolBtn();
|
||
SelectSceneBtn();
|
||
// 如果场景或学校没有选中,则不执行下面的操作
|
||
if (continueBtn.interactable)
|
||
{
|
||
schoolChoiceLable.gameObject.SetActive(false);
|
||
eventChoiceLable.gameObject.SetActive(true);
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("没有选择场景或学校,继续按钮不可用!");
|
||
}
|
||
}
|
||
|
||
//提交按钮,点击上传数据和隐藏界面
|
||
public void SubmitBtn()
|
||
{
|
||
SelectEvent();
|
||
SelsctDifficulty();
|
||
SelectEvnentBtn();
|
||
SetDataPanelInfo();
|
||
this.gameObject.SetActive(false);
|
||
}
|
||
|
||
//关闭按钮
|
||
public void OnClickCloseBtn()
|
||
{
|
||
transform.gameObject.SetActive(false);
|
||
}
|
||
|
||
//通过判断Toggle的IsOn是否被打开来判断继续按钮是否置灰
|
||
public void IsClick()
|
||
{
|
||
bool anyToggleSelected = false;
|
||
// // 检查场景选择
|
||
foreach (Toggle toggle in toggleList)
|
||
{
|
||
if (toggle.isOn)
|
||
{
|
||
anyToggleSelected = true;
|
||
break; // 如果有一个场景 Toggle 被选中,停止检查
|
||
}
|
||
}
|
||
// //根据是否有 Toggle 被选中,设置 ContinueBtn 是否可交互
|
||
if (continueBtn != null)
|
||
{
|
||
//Debug.Log(anyToggleSelected);
|
||
continueBtn.interactable = anyToggleSelected; // 如果有选中的 Toggle,继续按钮可交互,否则不可交互
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("ContinueBtn 按钮组件未找到!");
|
||
}
|
||
}
|
||
|
||
public void SetFirstChoise(Toggle[] toggles)//默认选择第一个Toggle
|
||
{
|
||
// 获取ToggleGroup中的所有Toggle
|
||
|
||
|
||
// 如果有Toggle,则选中第一个
|
||
if (toggles.Length > 0)
|
||
{
|
||
toggles[0].isOn = true;
|
||
}
|
||
}
|
||
|
||
//场景toggle
|
||
public void SetToggleOpenorClose(List<SceneItem> sceneItems)
|
||
{
|
||
foreach (SceneItem item in sceneItems)
|
||
{
|
||
if (item.IsOpen==false)
|
||
{
|
||
item.GetComponent<Toggle>().interactable = false;
|
||
if (item.GetComponent<Toggle>().isOn)
|
||
{
|
||
item.GetComponent<Toggle>().isOn = false;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
item.GetComponent<Toggle>().interactable = true;
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
|
||
//学校Toggle
|
||
public void SetScloolToggleOpenorClose(List<SchoolInfo> sceneItems)
|
||
{
|
||
foreach (SchoolInfo item in sceneItems)
|
||
{
|
||
if (item.isOpen == false)
|
||
{
|
||
item.GetComponent<Toggle>().interactable = false;
|
||
if (item.GetComponent<Toggle>().isOn)
|
||
{
|
||
item.GetComponent<Toggle>().isOn = false;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
item.GetComponent<Toggle>().interactable = true;
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
|
||
//事件Toggle
|
||
public void SetEventToggleOpenorClose(List<EventInfo> sceneItems)
|
||
{
|
||
foreach (EventInfo item in sceneItems)
|
||
{
|
||
if (item.isOpen == false)
|
||
{
|
||
item.GetComponent<Toggle>().interactable = false;
|
||
if (item.GetComponent<Toggle>().isOn)
|
||
{
|
||
item.GetComponent<Toggle>().isOn = false;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
item.GetComponent<Toggle>().interactable = true;
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
|
||
//设置日期界面的数据
|
||
public void SetDataPanelInfo()
|
||
{
|
||
datePanel.SchoolText.text = schoolName;
|
||
datePanel.PlaceText.text = eventName;
|
||
}
|
||
} |