参考代码如下:
一、灰度控制
[SerializeField]
Volume volume;
void Start()
{
VolumeComponent colorAdjustments;
VolumeProfile volumeProfile = volume.profile;
VolumeComponent[] coms = volumeProfile.components.ToArray();
for (int i = 0; i < coms.Length; i++)
{
if (coms[i].name.Contains("ColorAdjustments"))
{
colorAdjustments = coms[i];
break;
}
}
if (colorAdjustments) colorAdjustments.active = style == Style.Gray;
}
二、景深控制
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class DepthOfFieldCtrl : MonoBehaviour
{
[SerializeField]
Volume volume;
DepthOfField depthOfField;
MinFloatParameter minFloatParameter;
void Start()
{
VolumeProfile volumeProfile = volume.profile;
VolumeComponent[] coms = volumeProfile.components.ToArray();
for (int i = 0; i < coms.Length; i++)
{
if (coms[i].name.Contains("DepthOfField"))
{
depthOfField = coms[i] as DepthOfField;
if (depthOfField)
{
minFloatParameter = depthOfField.focusDistance;
break;
}
else
{
Debug.Log("U3DLog:DepthOfField is null.");
}
}
}
if (minFloatParameter == null)
{
Debug.Log("U3DLog:MinFloatParameter is null.");
}
}
void Update()
{
if (depthOfField)
{
SelObj selObj = SelObjManager.instance.currentSelObj;
if (selObj)
{
depthOfField.active = true;
if (minFloatParameter != null)
{
Vector3 localPos = Camera.main.transform.InverseTransformPoint(selObj.transform.position);
minFloatParameter.value = Mathf.Abs(localPos.z);
}
}
else
{
depthOfField.active = false;
}
}
}
}