首页 > 解决方案 > 如何将焦点长度值设置回后处理设置?

问题描述

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(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()
    {
        DepthField state = SaveGame.Load<DepthField>("depthoffieldstate");
        depthOfField.focalLength = state.focalLength;
    }
}

在底部,我添加了一个结构和两个函数 Save 和 Load。在 Save 中,它正在更新状态,而在保存时,它正在保存当前状态,因此,例如,如果 focusLength 值为 1,它将在状态下将 focusLength 值保存为 1。

但是在 Load 我看到的状态下,focalLength 值为 1 但它并没有在游戏中改变它。我认为我需要以某种方式更新设置,就像我在协程的 while 循环中所做的那样:

postProcessingProfile.depthOfField.settings = dof;

但我不确定如何在 Load 函数中执行此操作。

编辑 :

我做了一些测试并使用了一个断点,我在 Load 函数中看到 depthOfField 中的 focusLength 值为 1,但是当游戏仍在运行时我回到编辑器,我在 CC 配置文件中看到了focusLength 的值仍然是 299,我尝试在 Inspector 的编辑器中使用鼠标更改手动 focusLength 值将其拖动到左侧,但我不能,它似乎被锁定了。无法更改值。

public void Load()
    {
        DepthField state = SaveGame.Load<DepthField>("depthoffieldstate");
        depthOfField.focalLength = state.focalLength;
        postProcessingProfile.depthOfField.settings = depthOfField;
    }

这是在加载功能中进行更改后游戏运行时CC后处理配置文件设置的屏幕截图:

CC 后处理配置文件设置

这是我在 Player Camera 对象中使用它的屏幕截图:我将它与景深脚本一起使用:

玩家相机景深CC

如果游戏没有运行,我可以更改 CC 配置文件值,它确实会更改模糊效果,但是当游戏运行时我不能。

标签: c#unity3d

解决方案


好吧,您将其分配给该字段depthOfField

只需尝试做与以前相同的事情,例如 inDepthOfFieldInit并将其应用于后处理

postProcessingProfile.depthOfField.settings = depthOfField;

afaik 它是一个与类如此不同的结构,任何更改都与后处理无关,但必须在每次更改设置后再次应用。


推荐阅读