Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- using UnityEngine.SceneManagement;
- namespace shakagamii.Game {
- //Реализуем шаблонный класс Singlton`a
- public class Singleton<T> where T : class, new() {
- private static readonly Lazy<T> _instance = new Lazy<T>(() => new T());
- public static T Instance => _instance.Value;
- public bool HasInstance => Instance != null;
- }
- //Реализуем простой Singlton менеджер
- //для одного значенияздоровья персонажа
- public class PlayerDataManager : Singleton<PlayerDataManager> {
- public static Action<float> OnHealthChanged = default;
- private float _healthAmount;
- public static float HealthAmount {
- get => Instance._healthAmount;
- set {
- Instance._healthAmount = Mathf.Clamp(value, 0f, 100f);
- OnHealthChanged?.Invoke(Instance._healthAmount);
- }
- }
- }
- //И используем его так
- //В одной сцене заносим данные, и переходим в другую
- public class Scene01SceneChangeScript : MonoBehaviour {
- public float PlayerHealAmount = 100f;
- public IEnumerator Start() {
- PlayerDataManager.HealthAmount = PlayerHealAmount;
- yield return new WaitForSeconds(2f);
- SceneManager.LoadSceneAsync("Scene02");
- }
- }
- //Во второй следовательно эти данные тянем обратно.
- public class Scene02SceneChangeScript : MonoBehaviour {
- public float PlayerHealAmount;
- public IEnumerator Start() {
- PlayerHealAmount = PlayerDataManager.HealthAmount;
- yield return null;
- }
- }
- //Реализуем менеджер на основе MonoBehaviour
- //И используем DontDestroyOnLoad для того
- //чтобы объект не уничтожался при переходе меж сценами
- public class PlayerDataMonoManager : MonoBehaviour {
- public static Action<float> OnHealthChanged = default;
- [SerializeField]
- private static float _healthAmount;
- public void Awake() {
- DontDestroyOnLoad(this);
- }
- public static float HealthAmount {
- get => _healthAmount;
- set {
- _healthAmount = Mathf.Clamp(value, 0f, 100f);
- OnHealthChanged?.Invoke(_healthAmount);
- }
- }
- }
- public class PlayerData {
- public static Action<float> OnHealthChanged = default;
- private static float _healthAmount = 100f;
- public static float HealthAmount {
- get => _healthAmount;
- set {
- _healthAmount = Mathf.Clamp(value, 0f, 100f);
- OnHealthChanged?.Invoke(_healthAmount);
- }
- }
- }
- //Реализуем наше так называемое ядро игры
- public class Core {
- private Core _instance;
- private MonoCore _mono;
- public static PlayerData PlayerData;
- private Core(MonoCore mono) {
- _mono = mono;
- PlayerData = new PlayerData();
- }
- public Core Create(MonoCore mono) {
- return _instance ?? _instance = new Core(mono);
- }
- }
- //Реализуем инициализацию ядра
- //Помещаем скрипт на любой объект сцены SceneCore
- public class MonoCore : MonoBehaviour {
- public Core Core;
- public void Awake() {
- Core = Core.Create(this);
- }
- }
- //Скрипт на сцене 1, тянем дату, записыываем новое значение
- //ждем пару секунд, переходим на следующую сцену
- public class Scene01SceneChangeScript : MonoBehaviour {
- public PlayerData PlayerData;
- public IEnumerator Start() {
- PlayerData = Core.PlayerData;
- PlayerData.HealthAmount = 99f;
- yield return new WaitForSeconds(2f);
- StartScene02();
- }
- public void StartScene02() {
- AsyncOperation loadOperation = SceneManager.LoadSceneAsync("Scene02", LoadSceneMode.Additive);
- loadOperation.allowSceneActivation = true;
- loadOperation.completed += (operation) => SceneManager.UnloadSceneAsync("Scene01");
- }
- }
- //Во второй следовательно эти данные тянем обратно.
- public class Scene02SceneChangeScript : MonoBehaviour {
- public PlayerData PlayerData;
- public IEnumerator Start() {
- PlayerData = Core.PlayerData;
- yield return null;
- }
- }
- //Просто кидаете файл с кодом в любую папку, и
- //он будет автоматически срабатывать при запуске редактора
- //или изменении списка сцен в настройках
- [InitializeOnLoad]
- public class PlayModeStartSceneSetup {
- public const float START_SCENE_INDEX = 0f;
- static PlayModeStartSceneSetup() {
- SceneListChanged();
- EditorBuildSettings.sceneListChanged += SceneListChanged;
- }
- static void SceneListChanged() {
- if (EditorBuildSettings.scenes.Length == 0) return;
- SceneAsset scene = AssetDatabase.LoadAssetAtPath<SceneAsset>(EditorBuildSettings.scenes[START_SCENE_INDEX].path);
- EditorSceneManager.playModeStartScene = scene;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement