修复保存的bug | 提升性能 | 追加提示

This commit is contained in:
sin365 2024-09-21 23:40:38 +08:00
parent 21f27e76e4
commit b7302cca65
6 changed files with 423 additions and 329 deletions

23
Assets/DebugOpen.cs Normal file
View File

@ -0,0 +1,23 @@
using UnityEngine;
public class DebugOpen : MonoBehaviour {
public Debugger debugger;
void Awake()
{
debugger.gameObject.SetActive(false);
}
// Update is called once per frame
void Update () {
//按住上方向键+三角建再按下X键
if (Input.GetKey(KeyCode.JoystickButton3) && Input.GetKey(KeyCode.JoystickButton8))
{
if (Input.GetKeyDown(KeyCode.JoystickButton0))
{
debugger.gameObject.SetActive(true);
}
}
}
}

11
Assets/DebugOpen.cs.meta Normal file
View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f4bf72723ac1f6d4b857a876100cd015
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -38,7 +38,21 @@ public class HomeCanvasController : MonoBehaviour
public void LoadGameEvent() public void LoadGameEvent()
{ {
// 加载游戏存档 // 加载游戏存档
if (ResourceManager.Instance.GetGameArchiveStatus()) SceneManager.LoadScene("Level"); if (ResourceManager.Instance.GetGameArchiveStatus())
{
SceneManager.LoadScene("Level");
// UI 提示
GameManager.Instance.UIManager.ShowInfo("已读档!");
// 音频播放
GameManager.Instance.SoundManager.PlaySound(ESoundType.Effect, "Save");
}
else
{
// UI 提示
GameManager.Instance.UIManager.ShowInfo("未发现存档!");
GameManager.Instance.SoundManager.PlaySound(ESoundType.Effect, "No");
}
} }
/// <summary> /// <summary>

View File

@ -1,4 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
//using System.IO; //using System.IO;
using UnityEngine; using UnityEngine;
@ -65,6 +67,12 @@ public class ResourceManager : Singleton<ResourceManager>
private List<MapResourceInfo> _mapResourceInfoList = new List<MapResourceInfo>(); private List<MapResourceInfo> _mapResourceInfoList = new List<MapResourceInfo>();
private Dictionary<EResourceType, Dictionary<int, ResourceInfo>> _resourceInfoDic = new Dictionary<EResourceType, Dictionary<int, ResourceInfo>>(); private Dictionary<EResourceType, Dictionary<int, ResourceInfo>> _resourceInfoDic = new Dictionary<EResourceType, Dictionary<int, ResourceInfo>>();
#if UNITY_EDITOR
string SaveDataDirPath = Application.persistentDataPath;
#elif UNITY_PSP2
string SaveDataDirPath = "ux0:data/MoTaForPSVita";
#endif
string SaveDataFilePath => SaveDataDirPath + "/GameSaveData.json";
public ResourceManager() public ResourceManager()
{ {
@ -164,7 +172,12 @@ public class ResourceManager : Singleton<ResourceManager>
/// <returns>是否有存档</returns> /// <returns>是否有存档</returns>
public bool GetGameArchiveStatus() public bool GetGameArchiveStatus()
{ {
#if UNITY_EDITOR_WIN
return PlayerPrefs.HasKey("GameInfo"); return PlayerPrefs.HasKey("GameInfo");
#else
return System.IO.File.Exists(SaveDataFilePath);
#endif
} }
/// <summary> /// <summary>
@ -174,9 +187,14 @@ public class ResourceManager : Singleton<ResourceManager>
public void LoadGameArchive() public void LoadGameArchive()
{ {
// 判断存档是否存在 // 判断存档是否存在
#if UNITY_EDITOR_WIN
if (!PlayerPrefs.HasKey("GameInfo")) return; if (!PlayerPrefs.HasKey("GameInfo")) return;
// json 对象转 GameInfo // json 对象转 GameInfo
GameInfo gameInfo = JsonUtility.FromJson<GameInfo>(PlayerPrefs.GetString("GameInfo")); GameInfo gameInfo = JsonUtility.FromJson<GameInfo>(PlayerPrefs.GetString("GameInfo"));
#else
if (!System.IO.File.Exists(SaveDataFilePath)) return;
GameInfo gameInfo = JsonUtility.FromJson<GameInfo>(System.IO.File.ReadAllText(SaveDataFilePath));
#endif
// 加载存档 // 加载存档
_mapResourceInfoList = JsonUtility.FromJson<Serialization<MapResourceInfo>>(gameInfo.MapArchive).ToList(); _mapResourceInfoList = JsonUtility.FromJson<Serialization<MapResourceInfo>>(gameInfo.MapArchive).ToList();
// 加载背包信息 // 加载背包信息
@ -365,10 +383,38 @@ public class ResourceManager : Singleton<ResourceManager>
}; };
// 保存资源信息 // 保存资源信息
try
{
#if UNITY_EDITOR_WIN
PlayerPrefs.SetString("GameInfo", JsonUtility.ToJson(gameInfo)); PlayerPrefs.SetString("GameInfo", JsonUtility.ToJson(gameInfo));
#else
Debug.Log($"SaveDataDirPath->{SaveDataDirPath}");
Debug.Log($"Directory.Exists(SaveDataDirPath)->{Directory.Exists(SaveDataDirPath)}");
if (!Directory.Exists(SaveDataDirPath))
{
Debug.Log($"Directory.CreateDirectory(SaveDataDirPath)");
Directory.CreateDirectory(SaveDataDirPath);
}
string savestring = JsonUtility.ToJson(gameInfo);
Debug.Log($"savestring->{savestring}");
File.WriteAllText(SaveDataFilePath, savestring);
#endif
// UI 提示 // UI 提示
GameManager.Instance.UIManager.ShowInfo("游戏保存成功!"); GameManager.Instance.UIManager.ShowInfo("游戏已存档");
// 音频播放 // 音频播放
GameManager.Instance.SoundManager.PlaySound(ESoundType.Effect, "Save"); GameManager.Instance.SoundManager.PlaySound(ESoundType.Effect, "Save");
} }
catch
{
// UI 提示
GameManager.Instance.UIManager.ShowInfo("存档失败!");
GameManager.Instance.SoundManager.PlaySound(ESoundType.Effect, "No");
}
}
} }