How to modify variables from scripts in standard package?

Hi,

I have a main camera attached with lots of Image Effects, enabled from standard packages, such as CameraMotionBlur, SSAO, and Depth of Field to say a few.

My problem is, I cannot get the script objects (Ex.: GetComponent(DepthOfFieldScatter)), therefore I cannot modify their variables nor turning on/off the scripts.

However, it is completely fine with SSAOEffect. The fact that I found is, SSAOEffect script is a child of MonoBehaviour, while others’ parent class is PostEffectsBase. If I have scripts or attach a script from the package with MonoBehaviour, yes it can be accessed normally.

Any idea?

Thanks.

EDIT: It might be very helpful if anyone who follow this thread could try on their own if they’re willing to:

  • Use Unity Pro/Pro trial 4.x (4.3 to be exact)
  • Create a new project and add Image Effects (Pro only) package.
  • Add SSAOEffect (Add Component → Image Effects → Rendering → Screen Space Ambient Occlusion)) and add DepthOfFieldScatter (Add Component → Image Effects → Camera → Depth of Field (Lens Blur, Scatter, DX11)) to main camera.
  • Try to access both scripts in your own behavior script.

There is an easy way:

//C#
using UnityStandardAssets.ImageEffects;

//Javascript:
import UnityStandardAssets.ImageEffects;

Then access the scripts normally (With object.GetComponent(Effect) )

The scripts that you want to access are javascript, there are “issues” with manipulating them from c#.

I found that if I ran the following code:

void Start () {
    Component[] components = GameObject.Find("Main Camera").GetComponents<Component>();
    foreach (Component c in components) {
        Debug.Log(c.GetType());
    }
}

Then I received the following output:

20572-output.png

So the components are real and accessible, with a type that can be recognised at runtime.

From here I see two options:

1) Reflection

We can use reflection to manipulate the component.

For example:

using System.Reflection;

// ....

void Start () {
    Component dofs = GameObject.Find("Main Camera").GetComponent("DepthOfFieldScatter");
    FieldInfo fi = dofs.GetType().GetField("focalLength");
    fi.SetValue(dofs, 99.0f);
}

2) SendMessage

We can use SendMessage() to call methods of the component. This is not immediately useful in this case but can be made to work by adding setters to the target scripts.

void Start () {
    Component dofs = GameObject.Find("Main Camera").GetComponent("DepthOfFieldScatter");
    dofs.SendMessage("SetFocalLength", 99.0f)
}