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 ?
CodePudding user response:
There are countless ways to solve it actually so the most easiest (if you prefer) way to do it would be to check it in update if the canvas alpha is == 0 then call save once by having a boolean lets save "canSave" let me show you an example actually:
private void update() {
if( canvas.alpha == 0 && canSave ) {
Save();
}
}
or you can do it in an efficiant way by making a callback for fadeout panel function like:
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();
}
So now when you call the fadepanel function you can call it this way:
StartCoroutine( FadeCanvas( blah blah all the parameteres and in last, () => {
save();
} ) );
so this will call save function itself when the callback is called inside your FadeCanvas function, and there are some other ways to do so too but for now try one of them I hope it helps and I made my self clear, and hopefully this should work for you.