This question is asked in the context of image effect shader implementations in Unity.
Looking through the scripts for image effects in the standard assets package, I notice that initialization is generally done in the CheckResource() function.
The following is a snippet of code from the BloomAndFlares
image effect.
public override bool CheckResources()
{
CheckSupport(false);
screenBlend = CheckShaderAndCreateMaterial(screenBlendShader, screenBlend);
lensFlareMaterial = CheckShaderAndCreateMaterial(lensFlareShader, lensFlareMaterial);
vignetteMaterial = CheckShaderAndCreateMaterial(vignetteShader, vignetteMaterial);
separableBlurMaterial = CheckShaderAndCreateMaterial(separableBlurShader, separableBlurMaterial);
addBrightStuffBlendOneOneMaterial = CheckShaderAndCreateMaterial(addBrightStuffOneOneShader, addBrightStuffBlendOneOneMaterial);
hollywoodFlaresMaterial = CheckShaderAndCreateMaterial(hollywoodFlaresShader, hollywoodFlaresMaterial);
brightPassFilterMaterial = CheckShaderAndCreateMaterial(brightPassFilterShader, brightPassFilterMaterial);
if (!isSupported)
ReportAutoDisable();
return isSupported;
}
I am probably wrong in assuming that this is essentially the Start()
or Awake()
functions for PoseEffectsBase
, but I decided to try implementing something simillar.
public override bool CheckResources()
{
saturationPassMat = CheckShaderAndCreateMaterial(saturationPassFilter, saturationPassMat);
if (!isSupported)
ReportAutoDisable();
return isSupported;
}
Unlike the standard asset scripts, CheckResources()
does not seem to be called consistently for me. Unity console throws UnassignedReferenceExceptions
every frame, for the object saturationPassMat
.
When is the CheckResources()
function called? Should it be used for initialization just like Start()
?