如何指示游戏何时保存到文件然后在屏幕上显示某些内容?

How to indicate when the game is saving to file and then show something on screen?

我有一个保存系统,今天我正在使用 canvas 和文本,只是显示“保存游戏”字样使其闪烁 3 秒。

但我想做的是它会显示文本“正在保存游戏”,但时间不会是静态的 3 秒或我 select 的任何其他时间,而是要保存的时间游戏。

例如,第一次保存的东西较少,所以保存速度会更快,所以“正在保存游戏”应该在游戏后期显示一小段时间,这样会保存更多的东西,这样可以节省时间和显示“保存游戏”的时间应该更长。

我怎么知道游戏何时保存?也许通过某种方式检查保存的游戏文件是否 busy/locked ?

    public IEnumerator SaveWithTime()
    {
        yield return new WaitForSeconds(timeToStartSaving);

        if (objectsToSave.Count == 0)
        {
            Debug.Log("No objects selected for saving.");
        }

        if (saveManual == false && objectsToSave.Count > 0)
        {
            Save();
            StartCoroutine(fadeInOutSaveGame.OverAllTime(savingFadeInOutTime));
        }
    }

    public IEnumerator SaveWithTimeManual()
    {
        yield return new WaitForSeconds(timeToStartSaving);

        if(objectsToSave.Count == 0)
        {
            Debug.Log("No objects selected for saving.");
        }

        if (saveManual && objectsToSave.Count > 0)
        {
            Save();
            StartCoroutine(fadeInOutSaveGame.OverAllTime(savingFadeInOutTime));
        }
    }
}

在脚本的底部,我有两种方法 SaveWithTime 可以在游戏的特定点自动保存游戏,SaveWithTimeManual 可以在我按下某个键时保存游戏。

在这两种方法中,我使用 canvas 来显示“保存游戏”文本。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FadeInOutSaveGameText : MonoBehaviour
{
    public Canvas canvas;
    public float fadingSpeed;

    private bool stopFading = false;
    private const float THRESHOLD = 0.01F;

    // Start is called before the first frame update
    void Start()
    {
        //StartCoroutine(OverAllTime(5f));
    }

    IEnumerator CanvasAlphaChangeOverTime(Canvas canvas, float duration)
    {
        float alphaColor = canvas.GetComponent<CanvasGroup>().alpha;

        while (true)
        {
            alphaColor = (Mathf.Sin(Time.time * duration) + 1.0f) / 2.0f;
            canvas.GetComponent<CanvasGroup>().alpha = alphaColor;

            // only break, if current alpha value is close to 0 or 1
            if (stopFading && Mathf.Abs(alphaColor) <= THRESHOLD)//if (stopFading && (Mathf.Abs(alphaColor) <= THRESHOLD || Mathf.Abs(alphaColor - 1) <= THRESHOLD))
            {
                break;
            }

            yield return null;
        }
    }

    public IEnumerator OverAllTime(float time)
    {
        stopFading = false;

        StartCoroutine(CanvasAlphaChangeOverTime(canvas, fadingSpeed));

        yield return new WaitForSeconds(time);

        stopFading = true;
    }
}

这 class 实际写入游戏存档文件:

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 content = "";
        string fileName = Path.Combine(SAVE_FOLDER, "savegame.txt");
        if (File.Exists(fileName))
        {
            content = File.ReadAllText(fileName);
        }
        else
        {
            Debug.Log("Save game file is not exist" +
                " either the file has deleted or the game has not saved yet.");
        }

        return content;
    }
}

我如何才能找到保存所花费的实时时间,并在保存时显示文本“正在保存游戏”?

使用 Async/Threading,或者在 Unity 的情况下,Coroutines

bool isSaving;

public void SaveGame(){
    StartCoroutine(SaveGameCoroutine());
}

private IEnumerator SaveGameCoroutine(){
    if (isSaving){
        yield break; // or something else
    }

    ShowSavingCanvas();
    isSaving = true;

    SaveGame();

    isSaving = false;
    HideSavingCanvas();
}