- Home /
Changing OnGUI Texture to be behind another (GUI Rendered off of same script, different instance)
I looked into GUI.depth but it looks like it has to be in a different script's OnGUI function to work. I am also using GUI Styles. Also looked into Texture Game Objects but that defeats the purpose of the Style
Is there any hope of getting one GUI Component to be in front of another if it's in the same script and without GUITexture GOs?
Answer by Jamora · Jul 03, 2013 at 06:12 AM
If I understood you correctly, you want to have control over which of your gui elements are drawn and when they're rendered? That is certainly possible. I'm currently working with a gui construct much like this:
using UnityEngine;
using System.Collections;
public abstract class AbstractGUI : MonoBehaviour {
protected bool displayGUI = true;
/**
* Sets displaying mode
**/
public void SetToDisplay(bool display){
displayGUI = display;
}
/**
* Called from the GUIManager's OnGUI.
**/
public void DisplayGUI(){
if (displayGUI)
ShowContents();
}
/**
* Contains the implementation of the GUI
**/
protected abstract void ShowContents();
}
All GUI elements inherit this abstraction.
I have a gui manager on a GameObject, who keeps a List of AbstractGUI which contains all instances of GUIs currently needed (the scripts(=instances) are added to the manager's GameObject). It goes though this list in its OnGUI(), giving me full control over which element gets shown and in what order.
I'll give you the credit-- I literally JUST finished coding something along these lines...
I made 10 different scripts called GUIDepth_1, 2, 3, (10) and I have all of the "Depth 1 components" in a list together, and every OnGUI a foreach iterates the "DrawComponent()" method.
Each script has a different GUILayer. Nothing is actually "called" unless allowed by the individual component.
Your way sounds easier in the long run. $$anonymous$$ine is easier when you're desperate and spent a few weeks working solely on GUI only to have your dreams crushed in the last 10 $$anonymous$$utes of coding.
Remember to be careful with foreach. You can't do any changes to the list while iterating thru it. I made a coroutine that yield return new WaitForEndOfFrame(); before applying changes.
Answer by Quillicit · Jul 03, 2013 at 05:28 AM
I believe GUI.depth does in fact only work from other scripts. If you want one texture drawn on top of the other within the same OnGUI, the last thing drawn is what goes on top.
Thanks for your response.
Right now I'm creating all of my GUI objects from instances of the same base script called GUIComponent. When the script is set to "active" it's Rendered, else it is just skipped over.
Is that level of organization an impossible dream, or is there some way to make it happen?
to clarify this answer, elements are drawn in the order they are in the script:
void OnGUI(){
//1st drawn element. This is behind everything else.
//2nd drawn element, is in front of the 1st.
//etc.
//Last element is on top of everything.
}
Yeah, and to clarify what I said:
10 GUIComponent scripts, each with OnGUI getting called 2x+ each frame == impossible to put one in front of another, even when utilizing all of the available GUI Code?
I was making dinky projects before this so I never really went deep into making multiple menus, and menu-ception so layers were never even needed.
Think I found a workaround with holding this same methodology... Sadly going to have to make another 10+ scripts...
After ~a year of using Unity, this is honestly the most ridiculous thing that I've found so far... guess I should feel lucky?
Also, will post my workaround if it works and there are others out there who can't bear the pain of writing dozens of scripts.
----- Ugh, refreshed the page by accident and posted an answer, my bad.
Glad you figured it out. It sounds like you can still do what you were initially looking to do, unless you want to shuffle individual textures from separate scripts. $$anonymous$$enu-ception it up.