Hello, I prepared the code below as an example so that it is possible to verify the problem in a simple way.
Just have a camera in your scene, and add this code to it.
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
[RequireComponent(typeof(Camera))]
public class Codigo : MonoBehaviour {
public GameObject ms_volumeObject;
public Volume ms_volumeComponent;
public VolumeProfile ms_volumeProfile;
private void ConfigureMainCamera() {
//Enable PostProcessingRenderer
UniversalAdditionalCameraData camData = this.transform.GetComponent<UniversalAdditionalCameraData>();
camData.renderPostProcessing = true;
}
private void GenerateVolumeObjectAndVolumeComponent() {
//Volume Object
GameObject newSphereVolume = new GameObject("VolumeObject");
SphereCollider _colliderInstance = newSphereVolume.AddComponent<SphereCollider>();
_colliderInstance.isTrigger = true;
newSphereVolume.transform.parent = Camera.main.transform;
newSphereVolume.transform.localPosition = Vector3.zero;
newSphereVolume.transform.localRotation = Quaternion.identity;
ms_volumeObject = newSphereVolume;
//Volume Component
ms_volumeComponent = ms_volumeObject.AddComponent<Volume>() as Volume;
ms_volumeComponent.isGlobal = false;
ms_volumeComponent.blendDistance = 0;
ms_volumeComponent.weight = 1;
ms_volumeComponent.priority = 0;
ms_volumeComponent.profile = ms_volumeProfile;
ms_volumeComponent.enabled = true;
}
private void GenerateNewVolumeProfile() {
if (ms_volumeComponent != null) {
ms_volumeProfile = ScriptableObject.CreateInstance<VolumeProfile>();
ms_volumeProfile.Add(typeof(DepthOfField), false);
ms_volumeComponent.profile = ms_volumeProfile;
ms_volumeComponent.sharedProfile = ms_volumeProfile;
}
}
void Start() {
ConfigureMainCamera();
GenerateVolumeObjectAndVolumeComponent();
GenerateNewVolumeProfile();
//
VolumeProfile profile = ms_volumeComponent.sharedProfile;
if (!profile.TryGet<DepthOfField>(out var depth)) {
depth = profile.Add<DepthOfField>(false);
}
DepthOfFieldModeParameter _bokeh = new DepthOfFieldModeParameter(DepthOfFieldMode.Bokeh);
depth.mode = _bokeh;
}
}
As soon as the scene is started, a new Volume is created, adding DepthOfView, and the values are changed … everything seems to work fine, but it is not the case.
The simple act of changing the mode of DepthOfField through the code, makes it no longer work … I can change the properties of the profile generated through the inspector, but it does not show any results.