54 lines
1.1 KiB
C#
54 lines
1.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
public class MachineManager : MonoBehaviour
|
|
{
|
|
[Header("机器上的开关列表")]
|
|
public List<MachineSwitch> switches;
|
|
|
|
[Header("所有开关打开状态")]
|
|
public bool isOpen = false;
|
|
|
|
public GameObject myPagePrefab;
|
|
|
|
public GameObject currectPage;
|
|
public Transform pageParent;
|
|
|
|
private void OnEnable()
|
|
{
|
|
CheckAllSwitches();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
// 给每个开关订阅事件
|
|
foreach (var s in switches)
|
|
{
|
|
s.onSwitched.AddListener(CheckAllSwitches);
|
|
}
|
|
|
|
}
|
|
|
|
private void CheckAllSwitches()
|
|
{
|
|
|
|
isOpen = switches.Count > 0 && switches.All(s => s.isOn);
|
|
if (isOpen)
|
|
{
|
|
Debug.Log($"{name}:所有开关已打开!");
|
|
if (currectPage == null)
|
|
currectPage = Instantiate(myPagePrefab, pageParent);
|
|
|
|
}
|
|
else
|
|
{
|
|
if (currectPage != null)
|
|
{
|
|
Destroy(currectPage);
|
|
currectPage = null;
|
|
}
|
|
}
|
|
}
|
|
}
|