public void SaveTest()
{
savingText.GetComponent<Text>().text = "Saving !";
Save();
savingTextLogInfo.GetComponent<Text>().text = "";
}
我想以某种方式在调用 Save() 方法之前和 Save() 方法之后启动一个计时器或其他东西,以将它花费的时间分配到 saveTextLogInfo 文本中。
我在游戏中的某些地方调用 SaveTest,所以每次我希望它首先重置时间结果,然后再次计算执行 Save 方法所需的时间。
我可以将哪些其他日志记录信息分配给 saveTextLogInfo 文本?我想尝试找出为什么只在构建时将游戏冻结一毫秒甚至一秒。我在编辑器中没有任何错误或问题,但在构建中,游戏在保存时会在短时间内冻结。
这是保存方法:
public void Save()
{
SaveGame saveGame = new SaveGame();
saveGame.saveObjects = new List<SaveObject>();
for (int i = 0; i < objectsToSave.Count; i++)
{
SaveObject saveObject = new SaveObject();
saveObject.transformSaver = new TransformSaver();
Debug.Log($"{i}");
Debug.Log($"{objectsToSave[i].name}");
saveObject.gameObjectUniqueID = objectsToSave[i].GetComponent<GenerateGuid>().uniqueGuidID;
var x = objectsToSave[i].GetComponents<Component>();
var stateQueryComponent = x.Where(component => component is IStateQuery).ToList();
List<KeyToValue> componentsState = new List<KeyToValue>();
foreach (var z in stateQueryComponent)
{
var w = z as IStateQuery;
componentsState.Add(new KeyToValue(w.UniqueId.ToString(), w.GetState()));
}
saveObject.transformSaver.position = objectsToSave[i].transform.position;
saveObject.transformSaver.rotation = objectsToSave[i].transform.rotation;
saveObject.transformSaver.scaling = objectsToSave[i].transform.localScale;
saveObject.componentsState = componentsState;
saveGame.saveObjects.Add(saveObject);
}
string json = JsonUtility.ToJson(saveGame);
SaveSystem.Save(json);
}
和 SaveSystem :
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public static class SaveSystem
{
private static readonly string SAVE_FOLDER = Application.dataPath + "/save_";
public static void Init()
{
if (!Directory.Exists(SAVE_FOLDER))
{
Directory.CreateDirectory(SAVE_FOLDER);
}
}
public static void Save(string saveString)
{
string fileName = Path.Combine(SAVE_FOLDER, "savegame.txt");
File.WriteAllText(fileName, saveString);
}
public static string Load()
{
string fileName = Path.Combine(SAVE_FOLDER, "savegame.txt");
string content = File.ReadAllText(fileName);
return content;
}
}