首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何才能使当再次启动协程时,它不会再次播放对话?

我如何才能使当再次启动协程时,它不会再次播放对话?
EN

Stack Overflow用户
提问于 2020-01-03 20:04:39
回答 1查看 62关注 0票数 1

在DepthOfField脚本中,我使用了一个公共静态标志来检查协程何时结束。

此标志:

代码语言:javascript
复制
public bool dephOfFieldFinished = false;

脚本的其余部分:我使用后处理和focalLength来创建一些模糊效果。

代码语言:javascript
复制
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中的协程何时结束,开始对话:它在另一个脚本中:

代码语言:javascript
复制
void Update()
    {
        if (dephOfField.dephOfFieldFinished == true)
        {
            PlayConversations.PlaySingleConversation(0);
            dephOfField.dephOfFieldFinished = false;
        }
    }

然后我在另一个场景中添加了一个带有更新和此方法BackToMainMenu的脚本。

代码语言:javascript
复制
private void Update()
    {
        if (InMainMenu == false)
        {
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                InMainMenu = true;
                BackToMainMenu();
            }
        }
    }

和:

代码语言:javascript
复制
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中重新启动协程,然后它还会再次启动会话:因为标志再次为真:

代码语言:javascript
复制
if (dephOfField.dephOfFieldFinished == true)

在不重新开始对话的情况下,如何使用相同的协程在退出键上创建模糊效果?

也许我不应该使用这个公共静态标志来检查协程是否已经完成,并以其他方式完成它?也许我应该在DepthOfField中添加另一个协程,仅供退出键使用?

EN

回答 1

Stack Overflow用户

发布于 2020-01-04 09:01:36

在您的情况下,对话仅基于景深已完成这一事实开始。据我所知,您需要在满足其他条件时启动它。所以你也应该检查其他条件,而不仅仅是景深。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59578333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档