_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/ListPanel.cs
2024-11-24 00:01:23 +08:00

89 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ListPanel : BasePanel
{
public static ListPanel instance;
public Button returnBtn;
public Toggle toggle1; // 第一个 Toggle
public Toggle toggle2; // 第二个 Toggle
public GameObject panel1; // 第一个面板
public GameObject panel2; // 第二个面板
public Transform tog1;
public Transform tog2;
public Transform parent1;//蒙板父物体
public Transform parent2;
public Transform Orparent1;//原始父物体
public Transform Orparent2;
public override void Start()
{
base.Start();
instance = this;
returnBtn.onClick.AddListener(OnClickReBtn);
// 初始化 Toggle 的监听事件
toggle1.onValueChanged.AddListener(OnToggle1Changed);
toggle2.onValueChanged.AddListener(OnToggle2Changed);
// 初始化面板状态,根据 Toggle 的初始值
UpdatePanels();
}
// 第一个 Toggle 的变化事件
private void OnToggle1Changed(bool isOn)
{
if (isOn)
{
SetMarsk(2);
panel1.SetActive(true);
panel2.SetActive(false);
}
}
// 第二个 Toggle 的变化事件
private void OnToggle2Changed(bool isOn)
{
if (isOn)
{
SetMarsk(1);
panel1.SetActive(false);
panel2.SetActive(true);
}
}
// 根据当前 Toggle 的状态初始化面板
private void UpdatePanels()
{
panel1.SetActive(toggle1.isOn);
panel2.SetActive(toggle2.isOn);
}
void OnDestroy()
{
// 取消监听事件,避免内存泄漏
toggle1.onValueChanged.RemoveListener(OnToggle1Changed);
toggle2.onValueChanged.RemoveListener(OnToggle2Changed);
}
public void OnClickReBtn()
{
HidePanel();
}
public void SetMarsk(int i)
{
if (i==1)
{
tog1.SetParent(parent1);
tog2.SetParent(Orparent2);
}
else
{
tog1.SetParent(Orparent1);
tog2.SetParent(parent2);
}
}
}