在DepthOfField脚本中,我使用了一个公共静态标志来检查协程何时结束。
此标志:
public bool dephOfFieldFinished = false;脚本的其余部分:我使用后处理和focalLength来创建一些模糊效果。
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<Animator>();
AnimationClip[] clips = playerAnimator.runtimeAnimatorController.animationClips;
foreach (AnimationClip clip in clips)
{
clipLength = clip.length;
}
DepthOfFieldInit(300, clipLength);
state = new DepthField();
}
public void DepthOfFieldInit(float focalLength, float duration)
{
depthOfField = postProcessingProfile.depthOfField.settings;
depthOfField.focalLength = focalLength;
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.counterDuration = counter;
state.focalLength = val;
yield return null;
}
playerAnimator.enabled = false;
dephOfFieldFinished = true;
depthOfFieldRoutineRef = null;
}
public struct DepthField
{
public float focalLength;
public float counterDuration;
}
public void Save()
{
}
public void Load()
{
}
}然后,在游戏后面的某个地方,我检查DepthOfField中的协程何时结束,开始对话:它在另一个脚本中:
void Update()
{
if (dephOfField.dephOfFieldFinished == true)
{
PlayConversations.PlaySingleConversation(0);
dephOfField.dephOfFieldFinished = false;
}
}然后我在另一个场景中添加了一个带有更新和此方法BackToMainMenu的脚本。
private void Update()
{
if (InMainMenu == false)
{
if (Input.GetKeyDown(KeyCode.Escape))
{
InMainMenu = true;
BackToMainMenu();
}
}
}和:
public static void BackToMainMenu()
{
var depthOfField = GameObject.Find("Player Camera").GetComponent<DepthOfField>();
depthOfField.DepthOfFieldInit(300, 3);
Cursor.lockState = CursorLockMode.Confined;
Time.timeScale = 0f;
SceneManager.LoadScene(MainMenuScene.name, LoadSceneMode.Additive);
InMainMenu = true;
}这个想法是为了在每次玩家按下退出键回到主菜单时产生模糊效果。这工作得很好,但问题是,每次按下escap键时,它都会在DepthOfField中重新启动协程,然后它还会再次启动会话:因为标志再次为真:
if (dephOfField.dephOfFieldFinished == true)在不重新开始对话的情况下,如何使用相同的协程在退出键上创建模糊效果?
也许我不应该使用这个公共静态标志来检查协程是否已经完成,并以其他方式完成它?也许我应该在DepthOfField中添加另一个协程,仅供退出键使用?
发布于 2020-01-04 09:01:36
在您的情况下,对话仅基于景深已完成这一事实开始。据我所知,您需要在满足其他条件时启动它。所以你也应该检查其他条件,而不仅仅是景深。
https://stackoverflow.com/questions/59578333
复制相似问题