I am trying to duplicate the options in this video to add a toggle to turn on/off post processing in my game:
I've created a custom action and action list which runs from a toggle button in my options menu. It doesn't work though and I'm not sure what I'm doing wrong. Is there a better way to do this or is there an error in my script? The post processing volume is attached to my main camera and working fine otherwise.
using System.Collections;********
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
namespace AC
{
[System.Serializable]
public class PostProcessingController : Action
{
public override string Title => "PostProcessingPixelation";
public override ActionCategory Category => ActionCategory.Custom;
public override string Description => "Turns on or off Pixelation";
[SerializeField] private PostProcessVolume _postProcessVolume;
private PixelationEffect _pixelationEffect;
private void Start()
{
_postProcessVolume.profile.TryGetSettings(out _pixelationEffect);
}
public void PixelationEffectOnOff(bool value)
{
_pixelationEffect.active = value;
}
}
}
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
Actions do not have Start functions - but instead have Run functions. See this tutorial for a guide to writing custom Actions.
However, you may not necessarily need one here. The best way to deal with graphic options is to store the value in a Global Bool Variable with its Link to property set to Options Data. That way, its value will be loaded automatically when the game starts, and you can use Toggle elements / Variable Actions to get/set its value.
You'd then just need a script to turn the post-processing on or off based on this Bool variable's value:
Attach this to an empty GameObject, make it a prefab, then remove from the scene.
Then, whenever you change the option's value, use the Object: Call event Action to run this prefab's UpdateValue function.
Thank you, I've given that a try but I'm not seeing the "UpdateValue" option under Object: Call Event action.
Sometimes get an error "The script needs to derive from mono behavior".
I made a video following your suggestion, I'm not sure what I am doing wrong.
https://youtu.be/A1u-5VNWd0M
Sorry, that's my fault. I started writing the code as a custom Action, but switched to a MonoBehaviour without updating the class type.
I've corrected the script above - give it another try. When using the Action, you'll first need to click "+" to create a new event slot. Then drag/drop the prefab (with the script attached) into this slot, and the UpdateValue field should then be available in the drop-down that appears to the right.
Alright, I'm able to add the UpdateValue function to the action but am getting an error when trying the menu in game.
"NullReferenceException: Object reference not set to an instance of an object
PostProcessingController.UpdateValue () (at Assets/Script/PostProcessingController.cs:16)"
You'll need to create a Global Bool variable, link it to Options Data, and then enter its name into the component's Inspector. By default, this is named DoPostProcessing - have you added and assigned this?
Alright, that was the piece I was missing! It works now. Thank you so much for your help.
So far I have tried changing the "Link to" under the global variable with no luck.
Keep "Link to" as Options Data - it'll keep the value persistent.
As the Post Processing object is local to the scene, though, you'll need to apply the variable's value to the new instance when opening a scene. This should just be a case of calling the above script's UpdateValue function in the same way as before in your OnStart / OnLoad cutscenes.
That worked! As always you are extremely helpful!