浏览代码

Merge remote-tracking branch 'origin/Scene-Loading' into Scene-Loading

/main
Unknown 4 年前
当前提交
7c11a54d
共有 11 个文件被更改,包括 232 次插入229 次删除
  1. 149
      UOP1_Project/Assets/Scripts/Editor/GameSceneEditor.cs
  2. 56
      UOP1_Project/Assets/Scripts/Event Listeners/IntEventListener.cs
  3. 56
      UOP1_Project/Assets/Scripts/Event Listeners/VoidEventListener.cs
  4. 28
      UOP1_Project/Assets/Scripts/LevelEnd.cs
  5. 114
      UOP1_Project/Assets/Scripts/LocationLoader.cs
  6. 13
      UOP1_Project/Assets/Scripts/Scriptable Objects/Events/LoadEvent.cs
  7. 5
      UOP1_Project/Assets/Scripts/Scriptable Objects/Events/VoidGameEvent.cs
  8. 14
      UOP1_Project/Assets/Scripts/Scriptable Objects/SceneManagement/GameScene.cs
  9. 4
      UOP1_Project/Assets/Scripts/Scriptable Objects/SceneManagement/Location.cs
  10. 8
      UOP1_Project/Assets/Scripts/Scriptable Objects/SceneManagement/Menu.cs
  11. 14
      UOP1_Project/Assets/Scripts/StartGame.cs

149
UOP1_Project/Assets/Scripts/Editor/GameSceneEditor.cs


[CustomEditor(typeof(GameScene), true)]
public class GameSceneEditor : Editor
{
#region UI_Warnings
#region UI_Warnings
private const string noScenesWarning = "There are no scenes set for this level yet! Add a new scene with the dropdown below";
private const string noScenesWarning = "There are no scenes set for this level yet! Add a new scene with the dropdown below";
#endregion
#endregion
#region GUI_Styles
private GUIStyle headerLabelStyle;
#region GUI_Styles
#endregion
private GUIStyle headerLabelStyle;
// it holds a list of properties to hide from basic inspector
private static readonly string[] ExcludedProperties = { "m_Script", "sceneName" };
#endregion
// it holds a list of properties to hide from basic inspector
private static readonly string[] ExcludedProperties = {"m_Script", "sceneName"};
private string[] sceneList;
private GameScene gameSceneTarget;
private string[] sceneList;
private GameScene gameSceneTarget;
private void OnEnable()
{
gameSceneTarget = target as GameScene;
PopulateScenePicker();
InitializeGuiStyles();
}
public override void OnInspectorGUI()
{
EditorGUILayout.LabelField("Scene information", headerLabelStyle);
EditorGUILayout.Space();
DrawScenePicker();
DrawPropertiesExcluding(serializedObject, ExcludedProperties);
}
private void DrawScenePicker()
{
var sceneName = gameSceneTarget.sceneName;
EditorGUI.BeginChangeCheck();
var selectedScene = sceneList.ToList().IndexOf(sceneName);
private void OnEnable()
{
gameSceneTarget = target as GameScene;
PopulateScenePicker();
InitializeGuiStyles();
}
if (selectedScene < 0)
{
EditorGUILayout.HelpBox(noScenesWarning, MessageType.Warning);
}
public override void OnInspectorGUI()
{
EditorGUILayout.LabelField("Scene information", headerLabelStyle);
EditorGUILayout.Space();
DrawScenePicker();
DrawPropertiesExcluding(serializedObject, ExcludedProperties);
}
selectedScene = EditorGUILayout.Popup("Scene", selectedScene, sceneList);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(target, "Changed selected scene");
gameSceneTarget.sceneName = sceneList[selectedScene];
MarkAllDirty();
}
}
private void DrawScenePicker()
{
var sceneName = gameSceneTarget.sceneName;
EditorGUI.BeginChangeCheck();
var selectedScene = sceneList.ToList().IndexOf(sceneName);
private void InitializeGuiStyles()
{
headerLabelStyle = new GUIStyle(EditorStyles.largeLabel)
{
fontStyle = FontStyle.Bold,
fontSize = 18,
fixedHeight = 70.0f
};
}
if (selectedScene < 0)
{
EditorGUILayout.HelpBox(noScenesWarning, MessageType.Warning);
}
selectedScene = EditorGUILayout.Popup("Scene", selectedScene, sceneList);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(target, "Changed selected scene");
gameSceneTarget.sceneName = sceneList[selectedScene];
MarkAllDirty();
}
}
private void InitializeGuiStyles()
{
headerLabelStyle = new GUIStyle(EditorStyles.largeLabel)
{
fontStyle = FontStyle.Bold,
fontSize = 18, fixedHeight = 70.0f
};
}
/// <summary>
/// Populates the scene picker with scenes included in the game's build index
/// </summary>
private void PopulateScenePicker()
{
var sceneCount = SceneManager.sceneCountInBuildSettings;
sceneList = new string[sceneCount];
for (int i = 0; i < sceneCount; i++)
{
sceneList[i] = Path.GetFileNameWithoutExtension(SceneUtility.GetScenePathByBuildIndex(i));
}
}
/// <summary>
/// Populates the scene picker with scenes included in the game's build index
/// </summary>
private void PopulateScenePicker()
{
var sceneCount = SceneManager.sceneCountInBuildSettings;
sceneList = new string[sceneCount];
for (int i = 0; i < sceneCount; i++)
{
sceneList[i] = Path.GetFileNameWithoutExtension(SceneUtility.GetScenePathByBuildIndex(i));
}
}
/// <summary>
/// Marks scenes as dirty so data can be saved
/// </summary>
private void MarkAllDirty()
{
EditorUtility.SetDirty(target);
EditorSceneManager.MarkAllScenesDirty();
}
/// <summary>
/// Marks scenes as dirty so data can be saved
/// </summary>
private void MarkAllDirty()
{
EditorUtility.SetDirty(target);
EditorSceneManager.MarkAllScenesDirty();
}
}

56
UOP1_Project/Assets/Scripts/Event Listeners/IntEventListener.cs


/// </summary>
public class IntEventListener : MonoBehaviour
{
public IntGameEvent intGameEvent;
public IntEvent OnEventRaised;
public IntGameEvent intGameEvent;
public IntEvent OnEventRaised;
private void OnEnable()
{
//Check if the event exists to avoid errors
if (intGameEvent == null)
{
return;
}
intGameEvent.eventRaised += Respond;
}
private void OnEnable()
{
//Check if the event exists to avoid errors
if (intGameEvent == null)
{
return;
}
intGameEvent.eventRaised += Respond;
}
private void OnDisable()
{
if (intGameEvent == null)
{
return;
}
intGameEvent.eventRaised -= Respond;
}
private void OnDisable()
{
if (intGameEvent == null)
{
return;
}
intGameEvent.eventRaised -= Respond;
}
public void Respond(int value)
{
if (OnEventRaised == null)
{
return;
}
OnEventRaised.Invoke(value);
}
}
public void Respond(int value)
{
if (OnEventRaised == null)
{
return;
}
OnEventRaised.Invoke(value);
}
}

56
UOP1_Project/Assets/Scripts/Event Listeners/VoidEventListener.cs


public class VoidEventListener : MonoBehaviour
{
public VoidGameEvent voidGameEvent;
public UnityEvent OnEventRaised;
public VoidGameEvent voidGameEvent;
public UnityEvent OnEventRaised;
private void OnEnable()
{
//Check if the event exists to avoid errors
if (voidGameEvent == null)
{
return;
}
voidGameEvent.eventRaised += Respond;
}
private void OnEnable()
{
//Check if the event exists to avoid errors
if (voidGameEvent == null)
{
return;
}
voidGameEvent.eventRaised += Respond;
}
private void OnDisable()
{
if (voidGameEvent == null)
{
return;
}
voidGameEvent.eventRaised -= Respond;
}
private void OnDisable()
{
if (voidGameEvent == null)
{
return;
}
voidGameEvent.eventRaised -= Respond;
}
public void Respond()
{
if (OnEventRaised == null)
{
return;
}
OnEventRaised.Invoke();
}
}
public void Respond()
{
if (OnEventRaised == null)
{
return;
}
OnEventRaised.Invoke();
}
}

28
UOP1_Project/Assets/Scripts/LevelEnd.cs


using UnityEngine;
using UnityEngine;
{
public LoadEvent onLevelEnd;
public GameScene[] locationsToLoad;
public bool showLoadScreen;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
onLevelEnd.Raise(locationsToLoad, showLoadScreen);
}
}
{
public LoadEvent onLevelEnd;
public GameScene[] locationsToLoad;
public bool showLoadScreen;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
onLevelEnd.Raise(locationsToLoad, showLoadScreen);
}
}
}

114
UOP1_Project/Assets/Scripts/LocationLoader.cs


public class LocationLoader : MonoBehaviour
{
public GameScene[] mainMenuScenes;
[Header("Loading Screen")]
public GameObject loadingInterface;
public Image loadingProgressBar;
public GameScene[] mainMenuScenes;
[Header("Loading Screen")]
public GameObject loadingInterface;
public Image loadingProgressBar;
[Header("Load Event")]
//The load event we are listening to
[SerializeField] private LoadEvent _loadEvent = default;
[Header("Load Event")]
//The load event we are listening to
[SerializeField] private LoadEvent _loadEvent = default;
//List of the scenes to load and track progress
private List<AsyncOperation> _scenesToLoadAsyncOperations = new List<AsyncOperation>();

private GameScene _activeScene;
private void OnEnable()
{
_loadEvent.loadEvent += LoadScenes;
}
private void OnEnable()
{
_loadEvent.loadEvent += LoadScenes;
}
private void OnDisable()
{
_loadEvent.loadEvent -= LoadScenes;
}
private void OnDisable()
{
_loadEvent.loadEvent -= LoadScenes;
}
private void Start()
{
LoadMainMenu();
}
private void Start()
{
LoadMainMenu();
}
private void LoadMainMenu()
{
LoadScenes(mainMenuScenes, false);
}
private void LoadMainMenu()
{
LoadScenes(mainMenuScenes, false);
}
/// <summary> This function loads the scenes passed as array parameter </summary>
public void LoadScenes(GameScene[] locationsToLoad, bool showLoadingScreen)

}
}
public void UnloadScenes()
{
if(_ScenesToUnload != null)
{
for (int i = 0; i < _ScenesToUnload.Count; ++i)
{
//Unload the scene asynchronously in the background
SceneManager.UnloadSceneAsync(_ScenesToUnload[i]);
}
}
_ScenesToUnload.Clear();
}
public void UnloadScenes()
{
if (_ScenesToUnload != null)
{
for (int i = 0; i < _ScenesToUnload.Count; ++i)
{
//Unload the scene asynchronously in the background
SceneManager.UnloadSceneAsync(_ScenesToUnload[i]);
}
}
_ScenesToUnload.Clear();
}
/// <summary> This function checks if a scene is already loaded </summary>
/// <summary> This function checks if a scene is already loaded </summary>
public bool CheckLoadState(String sceneName)
{
if (SceneManager.sceneCount > 0)
{
for (int i = 0; i < SceneManager.sceneCount; ++i)
{
Scene scene = SceneManager.GetSceneAt(i);
if (scene.name == sceneName)
{
return true;
}
}
}
return false;
}
public bool CheckLoadState(String sceneName)
{
if (SceneManager.sceneCount > 0)
{
for (int i = 0; i < SceneManager.sceneCount; ++i)
{
Scene scene = SceneManager.GetSceneAt(i);
if (scene.name == sceneName)
{
return true;
}
}
}
return false;
}
/// <summary> This function update loading progress </summary>
/// <summary> This function update loading progress </summary>
IEnumerator TrackLoadingProgress()
{

loadingInterface.SetActive(false);
}
public void ExitGame()
{
Application.Quit();
Debug.Log("Exit!");
}
public void ExitGame()
{
Application.Quit();
Debug.Log("Exit!");
}
}

13
UOP1_Project/Assets/Scripts/Scriptable Objects/Events/LoadEvent.cs


[CreateAssetMenu(fileName = "LoadGameEvent", menuName = "Game Event/Load")]
public class LoadEvent : ScriptableObject
{
public UnityAction<GameScene[], bool> loadEvent;
public void Raise(GameScene[] locationsToLoad, bool showLoadingScreen)
{
if (loadEvent != null) loadEvent.Invoke(locationsToLoad, showLoadingScreen);
}
}
public UnityAction<GameScene[], bool> loadEvent;
public void Raise(GameScene[] locationsToLoad, bool showLoadingScreen)
{
if (loadEvent != null)
loadEvent.Invoke(locationsToLoad, showLoadingScreen);
}
}

5
UOP1_Project/Assets/Scripts/Scriptable Objects/Events/VoidGameEvent.cs


[CreateAssetMenu(fileName = "VoidGameEvent", menuName = "Game Event/Void")]
public class VoidGameEvent : ScriptableObject
{
public UnityAction eventRaised;
public UnityAction eventRaised;
if(eventRaised != null) eventRaised.Invoke();
if (eventRaised != null)
eventRaised.Invoke();
}
}

14
UOP1_Project/Assets/Scripts/Scriptable Objects/SceneManagement/GameScene.cs


public class GameScene : ScriptableObject
{
[Header("Information")]
public string sceneName;
public string shortDescription;
[Header("Information")]
public string sceneName;
public string shortDescription;
[Header("Sounds")]
public AudioClip music;
[Range(0.0f, 1.0f)]
public float musicVolume;
[Header("Sounds")]
public AudioClip music;
[Range(0.0f, 1.0f)]
public float musicVolume;
}

4
UOP1_Project/Assets/Scripts/Scriptable Objects/SceneManagement/Location.cs


[CreateAssetMenu(fileName = "NewLocation", menuName = "Scene Data/Location")]
public class Location : GameScene
{
[Header("Location specific")]
public int enemiesCount;
[Header("Location specific")]
public int enemiesCount;
}

8
UOP1_Project/Assets/Scripts/Scriptable Objects/SceneManagement/Menu.cs


public enum Type
{
Main_Menu,
Pause_Menu
Main_Menu,
Pause_Menu
[Header("Menu specific")]
public Type type;
[Header("Menu specific")]
public Type type;
}

14
UOP1_Project/Assets/Scripts/StartGame.cs


public class StartGame : MonoBehaviour
{
public LoadEvent onPlayButtonPress;
public GameScene[] locationsToLoad;
public bool showLoadScreen;
public LoadEvent onPlayButtonPress;
public GameScene[] locationsToLoad;
public bool showLoadScreen;
public void OnPlayButtonPress()
{
onPlayButtonPress.Raise(locationsToLoad, showLoadScreen);
}
public void OnPlayButtonPress()
{
onPlayButtonPress.Raise(locationsToLoad, showLoadScreen);
}
}
正在加载...
取消
保存