You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Jeremy/Assets/Scripts/SceneSwitcher.cs

85 lines
1.9 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SceneSwitcher : MonoBehaviour
{
// make singleton
public static SceneSwitcher Instance { get; private set; }
private void Awake()
{
SetInstance();
}
private void OnValidate()
{
SetInstance();
}
private void SetInstance()
{
if (Instance == null)
{
Instance = this;
}
else
{
Debug.LogError("SceneSwitcher Instance already exists.");
}
}
[SerializeField]
private GameObject[] _deactivateChildrenOf;
public void SwitchScene(int sceneIndex)
{
for (var i = 0; i < transform.childCount; i++)
{
transform.GetChild(i).gameObject.SetActive(i == sceneIndex);
}
foreach (var toDeactivate in _deactivateChildrenOf)
{
foreach (Transform child in toDeactivate.transform)
{
child.gameObject.SetActive(false);
}
}
}
public void SwitchToNext()
{
for (var i = 0; i < transform.childCount; i++)
{
if (transform.GetChild(i).gameObject.activeSelf)
{
var nextIndex = i + 1;
if (nextIndex >= transform.childCount)
{
nextIndex = 0;
}
SwitchScene(nextIndex);
return;
}
}
}
public List<(string name, int index)> GetScenes()
{
var scenes = new List<(string name, int index)>();
for (var i = 0; i < transform.childCount; i++)
{
var child = transform.GetChild(i);
scenes.Add((child.name, i));
}
return scenes;
}
public GameObject GetGameObject(int sceneIndex)
{
return transform.GetChild(sceneIndex).gameObject;
}
}