GameManager.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using UnityEngine;
  2. public class GameManager : MonoBehaviour
  3. {
  4. private GameData data;
  5. private SaveManager saveManager;
  6. private TimeManager timeManager;
  7. private Material skybox;
  8. private Color globalLightColor;
  9. private Quaternion globalLightRotation;
  10. private GameObject player;
  11. // Start is called once before the first execution of Update after the MonoBehaviour is created
  12. void Start()
  13. {
  14. //Get the player
  15. player = GameObject.Find("Player");
  16. if (player == null) return;
  17. //Get components
  18. saveManager = GetComponent<SaveManager>();
  19. timeManager = GetComponent<TimeManager>();
  20. skybox = RenderSettings.skybox;
  21. globalLightColor = timeManager.globalLight.color;
  22. globalLightRotation = timeManager.globalLight.transform.rotation;
  23. data = saveManager.LoadGame();
  24. if (data != null)
  25. {
  26. SetData();
  27. }
  28. else
  29. {
  30. data = new GameData();
  31. }
  32. }
  33. // Update is called once per frame
  34. void Update()
  35. {
  36. if (Input.GetKeyDown(KeyCode.Space))
  37. {
  38. GetData();
  39. saveManager.SaveGame(data);
  40. }
  41. }
  42. void GetData()
  43. {
  44. data.playerPosition = player.transform.position;
  45. #region DaylightCycle
  46. data.minutes = timeManager.Minutes;
  47. data.hours = timeManager.Hours;
  48. data.skybox = skybox;
  49. data.globalLightColor = globalLightColor;
  50. data.globalLightRotation = globalLightRotation;
  51. #endregion
  52. }
  53. void SetData()
  54. {
  55. player.transform.position = data.playerPosition;
  56. #region DaylightCycle
  57. timeManager.Minutes = data.minutes;
  58. timeManager.Hours = data.hours;
  59. skybox = data.skybox;
  60. globalLightColor = data.globalLightColor;
  61. RenderSettings.fogColor = data.globalLightColor;
  62. globalLightRotation = data.globalLightRotation;
  63. #endregion
  64. }
  65. }