如何在保存游戏之前等待画布淡出完成



此脚本使画布组alpha在0和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;
}
}

并使用它:

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

我通过编辑器ui按钮onclick事件调用Save方法。

问题是在保存之前,我希望首先完成画布的淡出,然后在Save方法中制作其余的保存代码:

public void Save()
{
description.StartFading(false);

我想在完成StartFading之后再进行保存:

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

不知道怎么做。在Save方法中使用一段时间?

实际上有无数种方法可以解决这个问题,所以最简单的方法(如果您喜欢(是在更新中检查它。如果画布alpha为==0,则通过使用布尔值lets save"调用save一次;canSave"让我给你举个例子:

private void update() {
if( canvas.alpha == 0 && canSave ) {
Save();
}
}

或者你可以用一种有效的方式来做,为淡出面板函数做一个回调,比如:

public IEnumerator FadeCanvas(CanvasGroup canvasGroup, Direction direction, float duration, Action callBack ) {
// your code
// if you want to you can also wait for some seconds over here too
// like yield return new waitForSeconds(2);
// after that just simply write this line of code
callback();
}

所以现在当你调用fadebanel函数时,你可以这样调用它:

StartCoroutine( FadeCanvas( blah blah all the parameters and in last, () => { 
save();
} ) );

因此,当在FadeCanvas函数内部调用回调时,这将调用save函数本身,还有一些其他方法可以做到这一点,但现在,请尝试其中之一。

我希望这会有所帮助,我明确表示,希望这对你有用。

最新更新