How do I disable a Renderer on a UI object in 4.6

Hey everyone,

I have been told by my programmer friends that having completely alpha-d out objects in a scene is bad for optimization since the engine still has to render them. Currently, I typically just sett the alpha to 0, and am trying to fix that habit, but I can’t figure out how to disable rendering on a UI object.

I know this must be a super simple answer but I’m trying to disable a UI object from rendering. The object in question is an Image I have stored in code as “starsVisual” but when I try this code:

starsVisual.renderer.enabled=false;

I get this error:

MissingComponentException: There is no ‘Renderer’ attached to the “Stars” game object, but a script is trying to access it.

I am assuming that I need to use something else for 2D UI as opposed to the above code which is used for 3d objects in all of the examples I have found. Thanks.

4 Answers

4

Hi, I think there is no way to disable a single UI object without performance impact.
You can see the performance impact when enabling or disabling a whole UI window to hide it.
Also UI elements out of the screen continues to consume rendering time, so alpha and “out of the screen” are not solutions for UI elements.

What gave me a really good performance was to add a Canvas element to the main parent of the UI group that you want to hide (the Panel containing your window).
So setting the enable property to false will automatically stop rendering the whole group in just a single frame, also will appear again setting it to true.

Note that all of your scripts will continue executing normally.

Hi, I noticed that this approach results in touch events failing. Was this the situation in your case? Any advice? Thanks

What worked for me is instead of looking for CanvasRenderer, I looked for UnityEngine.EventSystems.UIBehaviour, the base class of the UI Components we use such as Image. And then from getting these, you can access the .enabled property.

Example:
UnityEngine.EventSystems.UIBehaviour[] allUI = gameObject.GetComponentsInChildren(); foreach(UnityEngine.EventSystems.UIBehaviour ui in allUI) { ui.enabled = false; //turn them off. }

Wow, thanks a lot. Did not expect the perfect answer for the topic here at the bottom. Works perfectly for me.

Try:
Using UnityEngine.UI;

A UI object in Unity 4.6 has a CanvasRenderer component and not Renderer. So you need to disable that. You can do it as:

starsVisual.GetComponent<CanvasRenderer>().enabled=false;

That's interesting. Just checked the docs. For some reason the CanvasRenderer does not inherit from Renderer. Will have to remember that one.

I tried that exact code and now am getting this error message: error CS1061: Type UnityEngine.CanvasRenderer' does not contain a definition for enabled' and no extension method enabled' of type UnityEngine.CanvasRenderer' could be found (are you missing a using directive or an assembly reference?)

Hmmm.... I was caught off guard by Unity there and did not realize that there is no enabled variable for Canvas Renderer unlike most of the components in Unity. So your best bet is to disable the game object itself as it looks like you are disabling the renderer on an object from a script on another object so disabling this object of whose canvas renderer you want to disable will be ok. Just do it like: starsVisual.SetActive(false); @BoredMormon we have one more thing to remember now. ;-)

I've always found enabling and disabling components to be hit and miss at best. Unity tends to be inconsistent on which components can be enabled. Normally when Unity is inconsistent there are good reasons I just don't understand yet. This may be a better method to disable the GameObject. starsVisual.SetActive(false); Note that this method messes with any children as well. One way to overcome is to have your graphics component on a separate child to the rest of the hierarchy. A side branch if you will.

Honestly, this might be a situation where it's not worth the trouble for a hit or miss solution. This particular scene isn't even remotely alpha heavy compared to my main scene, so I'm not going to sweat it too much. Still, I feel you guys gave me plenty of answers for my future optimization so thanks to everyone!