What I want to do is to save the current state of the Coroutine. For example if the Coroutine is in the middle then save the current state of it by time or duration and also save the state of the post processing focalLength value.
And when loading back continue the Coroutine from his last saved point don't start it over but ocntinue the Coroutine and also set the last saved focalLength value so it will continue from where it was saved.
If for example the Coroutine finished and saved after finished then when loading load after it finished.
The idea is to save and load and if needed to continue the Coroutine state and the focalLength post processing effect value.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.PostProcessing;
public class DepthOfField : MonoBehaviour
{
public UnityEngine.GameObject player;
public PostProcessingProfile postProcessingProfile;
public bool dephOfFieldFinished = false;
public LockSystem playerLockMode;
private Animator playerAnimator;
private float clipLength;
private Coroutine depthOfFieldRoutineRef;
private DepthOfFieldModel.Settings depthOfField;
private DepthField state;
void Start()
{
if (depthOfFieldRoutineRef != null)
{
StopCoroutine(depthOfFieldRoutineRef);
}
playerAnimator = player.GetComponent();
AnimationClip[] clips = playerAnimator.runtimeAnimatorController.animationClips;
foreach (AnimationClip clip in clips)
{
clipLength = clip.length;
}
DepthOfFieldInit(clipLength);
state = new DepthField();
}
public void DepthOfFieldInit(float duration)
{
depthOfField = postProcessingProfile.depthOfField.settings;
depthOfField.focalLength = 300;
StartCoroutine(changeValueOverTime(depthOfField.focalLength, 1, duration));
postProcessingProfile.depthOfField.settings = depthOfField;
}
public IEnumerator changeValueOverTime(float fromVal, float toVal, float duration)
{
playerLockMode.PlayerLockState(true, true);
float counter = 0f;
while (counter < duration)
{
var dof = postProcessingProfile.depthOfField.settings;
counter += Time.deltaTime;
float val = Mathf.Lerp(fromVal, toVal, counter / duration);
dof.focalLength = val;
postProcessingProfile.depthOfField.settings = dof;
state.focalLength = val;
yield return null;
}
playerAnimator.enabled = false;
dephOfFieldFinished = true;
depthOfFieldRoutineRef = null;
}
public struct DepthField
{
public float focalLength;
public bool playerCameraLockState;
}
public void Save()
{
SaveGame.Save("depthoffieldstate", state);
}
public void Load()
{
depthOfField = postProcessingProfile.depthOfField.settings;
DepthField state = SaveGame.Load("depthoffieldstate");
depthOfField.focalLength = state.focalLength;
postProcessingProfile.depthOfField.settings = depthOfField;
}
}
The save and loading to a file is working I'm using them in my project in other places.
The problem is how to get and save the coroutine state.
I tried first to save and load on the focalLength value. And I checked with a break point and see in the Save the value of focalLength is 1 and when loading it also 1 but it never change the value and the effect in the game when loading.
Also not sure how to save the coroutine state duration and continue it if needed.
The problem is with the loading for now it's not settings/effecting changing the value to 1 in the editor when the game is running.
I messed it up.
Again the saving and loading is working I'm saving to a binary file and reading from it.
The problem is how to get store and save the coroutine and the focalLength.
↧