x


Can I get a prefab to render in front of GUI buttons created in script?

I just want to do a fade to white between a menu button click and the level load.

So I'm using a flat 2x2px white texture as a prefab and resizing it using a script on instantiation.

I can get the white fade to render in front of statically placed objects (by playing with Z transform values), but my menu buttons are still showing over the fade.

Any way to get the white fade in to also cover the scripted menu buttons? Hope my explanation is clear.

Cheers.

more ▼

asked Apr 26 '13 at 06:21 PM

TomMakesGames gravatar image

TomMakesGames
35 4 7 10

You could hide the buttons.

Apr 26 '13 at 06:27 PM biohazard2u

Put your GUI objects inside an empty Game Object, then, attach following C# script to it:

public class TurnButtonsOnOFF : MonoBehaviour {

    void Start () {
       turnChildrenOn();     
    }

    void turnChildrenOn () {
       foreach (Transform child in transform) {
            child.gameObject.SetActiveRecursively(true);
       }
    }

    void turnChildrenOff () {
       foreach (Transform child in transform) {
            child.gameObject.SetActiveRecursively(false);
       } 
    }  
}

Now, manually disable the buttons object in the inspector.

That way, whenever you want to hide the buttons, all you have to do is call the script function turnChildrenOff, and vice versa.

Apr 26 '13 at 06:53 PM biohazard2u

The question is about OnGUI code, not game objects. It also wouldn't look good if you just turned the buttons off before fading to white.

Apr 26 '13 at 07:17 PM Eric5h5

Any OnGui code has to go in a GameObject... If you don’t want to turn them off, you can modify their alpha. Perhaps adding a Guistyle and changing the alpha value on normal/hover... :)

Apr 26 '13 at 07:26 PM biohazard2u

Or just use DrawTexture, as I mentioned in my answer. Also you can just use GUI.color to change alpha, you don't need to add styles or anything.

Apr 26 '13 at 07:35 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

3D objects can never appear in front of OnGUI. Either don't use OnGUI, or else do the fade using OnGUI also (such as GUI.DrawTexture).

more ▼

answered Apr 26 '13 at 06:41 PM

Eric5h5 gravatar image

Eric5h5
103k 50 166 622

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5109
x1828
x549
x250

asked: Apr 26 '13 at 06:21 PM

Seen: 397 times

Last Updated: Apr 29 '13 at 11:49 AM