0
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;
    }
}
4

2 回答 2

5

你想用Stopwatch

var stopwatch = new Stopwatch();

stopwatch.Start();
// do some processing here

stopwatch.Stop();

Console.WriteLine($"Total time {stopwatch.ElapsedMilliseconds} ms");

您可以让他们使用 重置秒表stopwatch.Reset()

如果您希望进行更强大的基准测试,我会查看https://github.com/dotnet/BenchmarkDotNet 该工具可以针对不同的 NET 目标运行您的代码,并且更加灵活和可配置。

于 2020-11-21T19:42:28.870 回答
0

只使用C#,没有外部引用,太简单了!

public static Principal() {
   DateTime StartDate = DateTime.Now;
   Secondary();
   Console.WriteLine($"{StartDate} - time elapsed in seconds: {(DateTime.Now - StartDate).TotalSeconds}");
}

public static Secondary() {
    System.Threading.Thread.Sleep(5000);
}
于 2020-11-21T20:17:38.163 回答