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

Time:05-11

I have a saving system and today i'm using a canvas and text and just displaying the words "Saving Game" make it flickering for 3 seconds.

but what i want to do is that it will show the text "Saving Game" but the time will not be static 3 seconds or any other time i select but to be the time it's taking to save the game.

for example at the first time it's saving less stuff so it will take faster to save so the "Saving Game" should be display for a short time later in the game it will save more stuff so the saving time and the time to display "Saving Game" should be longer.

How can i know when the game is saving ? Maybe by somehow checking if the saved game file is 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));
        }
    }
}

In the bottom of the script i have two methods SaveWithTime that save the game automatic at specific points in the game and SaveWithTimeManual that save the game when i'm pressing a key.

In this two methods i'm using the canvas to display the "Saving Game" text.

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

And this class make the actual writing to the save game file :

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

How can i find the real time it's taking to save and while saving showing the text "Saving Game" ?

CodePudding user response:

Use Async/Threading, or in Unity's case, 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();
}
  • Related