forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleSceneSwitch.cs
More file actions
119 lines (100 loc) · 3.45 KB
/
Copy pathSimpleSceneSwitch.cs
File metadata and controls
119 lines (100 loc) · 3.45 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System.Collections;
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;
using Unity.Scenes;
using UnityEngine.SceneManagement;
public class SimpleSceneSwitch : MonoBehaviour
{
public float scale = 1f;
public bool useInputButton = false;
public void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
public void Start()
{
if(SceneManager.GetActiveScene().buildIndex ==0)
{
NextScene();
}
}
void Update()
{
if(useInputButton)
{
if (Input.GetButtonDown("Fire1"))
{
NextScene();
}
if (Input.GetButtonDown("Fire2"))
{
PrevScene();
}
}
}
void OnGUI()
{
GUI.skin.label.fontSize = Mathf.RoundToInt ( 16 * scale );
GUI.color = new Color(1, 1, 1, 1);
float w = 410 * scale;
float h = 90 * scale;
GUILayout.BeginArea(new Rect(Screen.width - w -5, Screen.height - h -5, w, h), GUI.skin.box);
GUILayout.BeginHorizontal();
GUIStyle customButton = new GUIStyle("button");
customButton.fontSize = GUI.skin.label.fontSize;
if(useInputButton)
{
GUILayout.Label("Press Fire1 / Fire2 to switch scene",GUILayout.Height(50 * scale));
}
else
{
if(GUILayout.Button("\n Prev \n",customButton,GUILayout.Width(200 * scale), GUILayout.Height(50 * scale))) PrevScene();
if(GUILayout.Button("\n Next \n",customButton,GUILayout.Width(200 * scale), GUILayout.Height(50 * scale))) NextScene();
}
GUILayout.EndHorizontal();
int currentpage = SceneManager.GetActiveScene().buildIndex;
int totalpages = SceneManager.sceneCountInBuildSettings-1;
GUILayout.Label( currentpage + " / " + totalpages + " " + SceneManager.GetActiveScene().name );
GUILayout.EndArea();
}
public void NextScene()
{
int sceneIndex = SceneManager.GetActiveScene().buildIndex;
int loadIndex = sceneIndex+1;
if (loadIndex >= SceneManager.sceneCountInBuildSettings)
{
loadIndex = 1;
}
SwitchScene(loadIndex);
}
public void PrevScene()
{
int sceneIndex = SceneManager.GetActiveScene().buildIndex;
int loadIndex = sceneIndex-1;
if (loadIndex <= 0)
{
loadIndex = SceneManager.sceneCountInBuildSettings-1;
}
SwitchScene(loadIndex);
}
public void SwitchScene(int loadIndex)
{
var world = World.DefaultGameObjectInjectionWorld;
var entityManager = world.EntityManager;
var entities = entityManager.GetAllEntities();
for (int i = 0; i < entities.Length; i++)
{
string ename = entityManager.GetName(entities[i]);
bool isSubscene = entityManager.HasComponent<SubScene>(entities[i]);
bool isSceneSection = entityManager.HasComponent<SceneSection>(entities[i]);
//Runtime generated entities requires manual deletion,
//but we need to skip for some specific entities otherwise there will be spamming error
if( ename != "SceneSectionStreamingSingleton" && !isSubscene && !isSceneSection && !ename.Contains("GameObject Scene:") )
{
entityManager.DestroyEntity(entities[i]);
}
}
SceneManager.LoadScene(loadIndex);
}
}