WXMC/proj/unity/Assets/UILevelGroup.cs

78 lines
2.7 KiB
C#
Raw Normal View History

2024-12-04 16:18:46 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class UILevelGroup : MonoBehaviour
{
public GameObject[] levels;
private Vector3[] levelPositions;
private bool isExpanded = false;
private int isPlayingCount = 0;
// Start is called before the first frame update
void Start()
{
var levelComponents = GetComponentsInChildren<UILevel>();
levels = new GameObject[levelComponents.Length];
levelPositions = new Vector3[levelComponents.Length];
for (int i = 0; i < levelComponents.Length; i++)
{
levels[i] = levelComponents[i].gameObject;
levelPositions[i] = levels[i].transform.position;
levels[i].transform.position = transform.position;
var background = levels[i].GetComponent<UILevel>().background;
background.color = new Color(background.color.r, background.color.g, background.color.b, 0.0f);
var number = levels[i].GetComponent<UILevel>().number;
number.color = new Color(number.color.r, number.color.g, number.color.b, 0.0f);
levels[i].SetActive(false);
}
}
public void ClickLevelGroupButton()
{
Debug.Log("Clicked!");
Debug.Log(gameObject.name);
if (isPlayingCount != 0) return;
isPlayingCount = levels.Length;
if (isExpanded) { ClickCloseLevels(); }
else { ClickExpandLevels(); }
}
void ClickExpandLevels()
{
isExpanded = true;
for (int i = 0; i < levels.Length; ++i)
{
levels[i].SetActive(true);
var background = levels[i].GetComponent<UILevel>().background;
background.DOColor(new Color(background.color.r, background.color.g, background.color.b, 1.0f), 0.5f);
var number = levels[i].GetComponent<UILevel>().number;
number.DOColor(new Color(number.color.r, number.color.g, number.color.b, 1.0f), 0.5f);
levels[i].transform.DOMove(levelPositions[i], 1.0f).onComplete = delegate { isPlayingCount--; };
}
}
void ClickCloseLevels()
{
isExpanded = false;
foreach (var level in levels)
{
var background = level.GetComponent<UILevel>().background;
background.DOColor(new Color(background.color.r, background.color.g, background.color.b, 0.0f), 0.5f);
var number = level.GetComponent<UILevel>().number;
number.DOColor(new Color(number.color.r, number.color.g, number.color.b, 0.0f), 0.5f);
level.transform.DOMove(transform.position, 1.0f).onComplete = delegate { level.SetActive(false); isPlayingCount--; };
}
}
}