- Home /
Change cameras image effects with button press
HI all,
New to Unity and trying to get a script that will allow image effects attached to the main camera to be cycled through with button presses. At the moment I have different effects that are attached to different cameras that I can cycle through(the cameras) but it would be more efficient to have them all on the one camera object so that when I do modifications to the camera, I only have to do it once. I have looked at the following reference: Code:
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Update() {
OtherScript otherScript = GetComponent<OtherScript>();
otherScript.DoSomething();
}
}
but cant seem to work it out. I have a BloomEffect and a EdgeDetectEffect script that I want to use. Any advice
Cheers
Answer by mattcscz · Jan 04, 2015 at 12:40 PM
If you look at the script they are using namespaces, so you are able to do "using UnitySampleAssets.ImageEffects;" Then you can correctly reference the BloomAndFlares without any extra hastle.
Example:
using UnityEngine;
using System.Collections;
using UnitySampleAssets.ImageEffects;
public class ChangeCamEffects : MonoBehaviour {
public GameObject MainCam;
public void DoStuff() {
MainCam.GetComponent<BloomAndFlares> ().bloomIntensity = 1;
}
}
Hope this helps.
I had the issue the other day, and there was no up-to-date answer. I'm sure this would help people having the issue now, giving comments like yours won't =)
Thank you. I was trying for several hours yesterday trying to get access to my shader scripts, and simply adding the namespace like you suggested did exactly what I wanted.
Answer by asafsitner · Sep 12, 2011 at 09:54 AM
I had a similar problem. The first thing you need to do is move all the Image Effects scripts that you use to \Assets\Plugins folder
(make one if you don't have it already). That way the .js files can be accessed from the .cs files (for instance, c# won't recognize BloomAndLensFlares as a class until you do and so you can't pass it as a component to the OtherScript variable). After you've done that, all you have to do is indeed store BloomAndLensFlares (or any other image effect for that matter) in a variable (preferably cache it at the Start() or Awake() methods for more efficient code)and later change the 'enabled' property of the component as needed. For example:
using UnityEngine;
using System.Collections;
using Assets.Scripts;
public class ToggleBloom : MonoBehaviour
{
private BloomAndLensFlares _bloomToggle;
void Start()
{
_bloomToggle = transform.GetComponent("BloomAndLensFlares") as BloomAndLensFlares;
}
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown("space"))
{
_bloomToggle.enabled = !_bloomToggle.enabled;
}
}
}
Now you'll probably want to store the effects in an array and iterate over them as you press the button, but that's the general idea.
Hope that helps!
Cheers, Works a treat.
Yeah, I admit I was lost with why it wouldnt recognize the c# classes.
Using that class you created, am I correct in that I can access all variables/properties of the BloomAndLensFlare class? So to use a button press to change the intensity would be something like:
if(Input.Get$$anonymous$$eyDown("I"))
{
_bloomToggle.bloomIntensity++;
}
Thanks Again
Apologies for the late response. Yes, you are correct in that you can access all the properties of the BloomAndLensFlares class. You might also want to mark the answer as accepted if/when it's satisfactory so it won't appear on the 'unanswered' list.
Thanks again, got it workin just as I wanted.
Also, can't for the life of me work out how to mark the question answered. Any help?
Should be a little circle with a 'v' on it on the left side, where the little hands are. If you click it it should turn green and that's it. Been a while since I asked a question here ^^'
i moved to plugins but still gives me same error what to do? is there a way to write the same script in javascript im sure i wont have this problem
Your answer
Follow this Question
Related Questions
Improving the look of Unique/User Planned Scenes 0 Answers
How to make camera position relative to a specific target. 1 Answer
Change color of Crease Image Effect 2 Answers
How to switch between variations of the same image effect, or save image effect settings? 1 Answer
Adding an image effect at run time with script usingUnity 5 1 Answer