using System.Collections;
using System.Collections.Generic;
using System.Data;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class TestLevelSelection : MonoBehaviour
{
public Button[] leveBtn;//关卡按钮
public List<GameObject> LevelGame = new List<GameObject>();//存储关卡
public GameObject Grid, ScoreP;
Button ReturnShouye;
private void Awake()
{
ReturnShouye = GameObject.Find("ReturnShouye").GetComponent<Button>();
}
void Start()
{
leveBtn = new Button[Grid.transform.GetChild(0).childCount];
for (int i = 0; i < leveBtn.Length; i++)
{
Grid.transform.GetChild(0).GetChild(i).GetChild(0).GetComponent<Text>().text = (i + 1).ToString();
leveBtn[i] = Grid.transform.GetChild(0).GetChild(i).GetComponent<Button>();
}
for (int i = 0; i < GameObject.Find("测试关").transform.childCount; i++)
{
LevelGame.Add(GameObject.Find("测试关").transform.GetChild(i).gameObject);
}
ReturnShouye.onClick.AddListener(() =>
{
SceneManager.LoadScene("ShouYe");
});
}
void Update()
{
StarSelectLevel();
}
void StarSelectLevel()
{
foreach (var levelbtns in leveBtn)
{
levelbtns.onClick.AddListener(delegate ()
{
Text text = levelbtns.gameObject.GetComponentInChildren<Text>();
for (int i = 0; i < LevelGame.Count; i++)
{
if (LevelGame[i] != null)
{
Grid.gameObject.SetActive(false);
ScoreP.gameObject.SetActive(true);
LevelGame[int.Parse(text.text) - 1].SetActive(true);
}
}
});
}
}
}
unity点击按钮激活对应关卡
最新推荐文章于 2024-05-01 22:54:32 发布