简体   繁体   中英

How can I make that when starting the coroutine over again it will not play the conversation/s also over again?

In the DepthOfField script I'm using a public static flag to check when the coroutine is finished.

This flag :

public bool dephOfFieldFinished = false;

And the rest of the script : I'm using postprocessing and focalLength to create some blur effect.

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()
    {

    }
}

Then in some place later in the game I'm checking when the coroutine in the DepthOfField is finished start a conversation : It's in another script :

void Update()
    {
        if (dephOfField.dephOfFieldFinished == true)
        {
            PlayConversations.PlaySingleConversation(0);
            dephOfField.dephOfFieldFinished = false;
        }
    }

And then I added in another scene a script with Update and this method BackToMainMenu.

private void Update()
    {
        if (InMainMenu == false)
        {
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                InMainMenu = true;
                BackToMainMenu();
            }
        }
    }

And :

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

The idea is to create to make the blur effect each time the player is pressing the escape key back to the main menu. And this is working fine the problem is that it's starting the coroutine in the DepthOfField over again each time pressing the escap key and then it also start the conversation over again : Since the flag is true again :

if (dephOfField.dephOfFieldFinished == true)

How can I use the same coroutine to create the blur effect on the escape key without starting the conversation over again ?

Maybe I should not using this public static flag to check if the coroutine has finished or not and do it in some other way ? Maybe I should add another coroutine in the DepthOfField just for the escape key use?

In your case, conversation starts based only on the fact that depth of field was complete. As far as I understood, you need to start it when some other condition is met. So you should check for that other condition as well, not only for depth of field finished.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM