How to wait for the canvas fade in out to finish before saving the game?
this script make a canvas group alpha to change between 0 and 1 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Description : MonoBehaviour
{
public Canvas canvas;
public AnimationCurve animationCurve;
public float fadingSpeed = 5f;
public TMP_InputField _inputField;
public enum Direction { FadeIn, FadeOut };
private CanvasGroup canvasGroup;
void Start()
{
if (canvas == null) canvas = GetComponent<Canvas>();
canvasGroup = canvas.GetComponent<CanvasGroup>();
if (canvasGroup == null) Debug.LogError("Please assign a canvas group to the canvas!");
if (animationCurve.length == 0)
{
Debug.Log("Animation curve not assigned: Create a default animation curve");
animationCurve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
}
}
public void StartFading(bool InOut)
{
if (canvasGroup != null)
{
if (InOut)
{
StartCoroutine(FadeCanvas(canvasGroup, Direction.FadeIn, fadingSpeed));
}
else
{
StartCoroutine(FadeCanvas(canvasGroup, Direction.FadeOut, fadingSpeed));
}
}
}
public IEnumerator FadeCanvas(CanvasGroup canvasGroup, Direction direction, float duration)
{
var startTime = Time.time;
var endTime = Time.time + duration;
var elapsedTime = 0f;
if (direction == Direction.FadeIn) canvasGroup.alpha = animationCurve.Evaluate(0f);
else canvasGroup.alpha = animationCurve.Evaluate(1f);
while (Time.time <= endTime)
{
elapsedTime = Time.time - startTime;
var percentage = 1 / (duration / elapsedTime);
if ((direction == Direction.FadeOut)) // if we are fading out
{
canvasGroup.alpha = animationCurve.Evaluate(1f - percentage);
}
else
{
canvasGroup.alpha = animationCurve.Evaluate(percentage);
}
yield return new WaitForEndOfFrame();
}
if (direction == Direction.FadeIn) canvasGroup.alpha = animationCurve.Evaluate(1f);
else canvasGroup.alpha = animationCurve.Evaluate(0f);
_inputField.readOnly = false;
}
}
and using it :
using UnityEngine;
using System.Collections;
using System.IO;
public class SavingGame : MonoBehaviour
{
public int resWidth = 1920;
public int resHeight = 1080;
public SaveLoad saveLoad;
public Description description;
private static int countName;
private void Start()
{
countName = 0;
string[] dirs = Directory.GetDirectories(Application.persistentDataPath + "\\" + "Saved Screenshots",
"*.*", SearchOption.TopDirectoryOnly);
if(dirs.Length > 0)
{
countName = dirs.Length;
}
}
public static string ScreenShotName(int width, int height)
{
return string.Format("{0}/Saved Screenshots/SaveSlot{1} SavedGameSlot_{2}x{3}_{4}/SavedGameSlot_{1}x{2}_{3}.png",
Application.persistentDataPath,
countName,
width, height, System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
}
void Update()
{
if (Input.GetKeyDown("k"))
{
description.StartFading(true);
}
}
public void Save()
{
description.StartFading(false);
string filename = ScreenShotName(resWidth, resHeight);
string directory = Path.GetDirectoryName(filename);
Directory.CreateDirectory(directory);
ScreenCapture.CaptureScreenshot(filename);
StartCoroutine(saveLoad.SaveWithTime(directory, Path.GetFileNameWithoutExtension(filename) + ".savegame.txt"));
countName++;
}
}
I'm calling the Save method through the editor ui button onclick event.
the problem is before saving i want first the fading out of the canvas to be finished and then making the rest of the saving code in the Save method :
public void Save()
{
description.StartFading(false);
i want that after finished the StartFading then to make the saving :
string filename = ScreenShotName(resWidth, resHeight);
string directory = Path.GetDirectoryName(filename);
Directory.CreateDirectory(directory);
ScreenCapture.CaptureScreenshot(filename);
StartCoroutine(saveLoad.SaveWithTime(directory, Path.GetFileNameWithoutExtension(filename) + ".savegame.txt"));
countName++;
not sure how to do it. using a while maybe in the Save method ?
Comments
Post a Comment