forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneSwitcher.cs
More file actions
99 lines (83 loc) · 2.19 KB
/
Copy pathSceneSwitcher.cs
File metadata and controls
99 lines (83 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using Unity.Entities;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneSwitcher : MonoBehaviour
{
[Serializable]
public class SceneConfig
{
public string SceneName;
public int CustomDuration;
}
public int SceneSwitchInterval = 5;
public float TimeUntilNextSwitch = 0.0f;
public int CurrentSceneIndex = 0;
public bool EntitiesDestroyed = false;
public SceneConfig[] SceneConfigs;
// Use this for initialization
void Start ()
{
DontDestroyOnLoad(this);
LoadNextScene();
}
private void DestroyAllEntitiesInScene()
{
var entityManager = World.Active.GetExistingManager<EntityManager>();
var entities = entityManager.GetAllEntities();
entityManager.DestroyEntity(entities);
entities.Dispose();
EntitiesDestroyed = true;
}
private void LoadNextScene()
{
var sceneCount = SceneManager.sceneCountInBuildSettings;
var nextIndex = CurrentSceneIndex + 1;
if (nextIndex >= sceneCount)
{
Quit();
return;
}
var nextScene = SceneUtility.GetScenePathByBuildIndex(nextIndex);
TimeUntilNextSwitch = GetSceneDuration(nextScene);
CurrentSceneIndex = nextIndex;
SceneManager.LoadScene(nextIndex);
EntitiesDestroyed = false;
}
private int GetSceneDuration(string scenePath)
{
foreach (var scene in SceneConfigs)
{
if (!scenePath.EndsWith(scene.SceneName +".unity"))
continue;
if (scene.CustomDuration <= 0)
continue;
return scene.CustomDuration;
}
return SceneSwitchInterval;
}
private void Quit()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
// Update is called once per frame
void Update ()
{
TimeUntilNextSwitch -= Time.deltaTime;
if (TimeUntilNextSwitch > 0.0f)
return;
if (!EntitiesDestroyed)
{
DestroyAllEntitiesInScene();
}
else
{
DestroyAllEntitiesInScene();
LoadNextScene();
}
}
}