Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by aisenai · Apr 28, 2015 at 10:44 PM · guiiphonedepth

useGUILayout=false does not honor GUI.depth

Hi, running on iOS, setting useGUILayout to false and using GUI.depth broke the depth sorting. All is working fine (i.e. GUI widget are sorted correctly according to GUI.depth vlaues) if i enable useGUILayout again. It seems that this is a bug already discovered by others, anyone came out with a solution?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Apr 29, 2015 at 12:06 AM

It's not really a bug, just not documented proberly. Unity uses the Layout event for several things. One is to determine the drawing order. If you disable the layout event Unity can't determine in which order it should call the OnGUI methods. The order of the layour events is always the same. It's based on some internal order in which the instances have have been created.

For every event that is handled by OnGUI there will be a layout event right before the actual event. Of course only if those are enabled with useGUILayout = true. When you disable the layout event several things that depend on that event won't work anymore.

edit

Here's an example how you could manually distribute an OnGUI event the way you want:

 // GUIManager.cs
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public interface ICustomGUI
 {
     int GUIDepth{get;}
     void CustomOnGUI();
 }
 
 public class GUIManager : MonoBehaviour
 {
     #region singleton
     private static GUIManager m_Instance;
     public static GUIManager Instance
     {
         get{
             if (m_Instance == null)
             {
                 m_Instance = (GUIManager)FindObjectOfType(typeof(GUIManager));
                 if (m_Instance == null)
                     m_Instance = (new GameObject("GUIManager")).AddComponent<GUIManager>();
                 DontDestroyOnLoad(m_Instance.gameObject);
             }
             return m_Instance;
         }
     }
     #endregion singleton
     void Awake()
     {
         // This class only works when useGUILayout is false
         useGUILayout = false;
         m_Instance = this;
     }
 
     List<ICustomGUI> m_List = new List<ICustomGUI>();
     // Use this method to "register" a new instance
     public static void Add(ICustomGUI aInst)
     {
         Instance.m_List.Add(aInst);
     }
 
     private int Compare(ICustomGUI a, ICustomGUI b)
     {
         return b.GUIDepth.CompareTo(a.GUIDepth);
     }
 
     private void Prepare()
     {
         // remove items which are null
         for(int i = m_List.Count-1; i >=0; i--)
         {
             // the "else if" is for detecting Unity's "null" trick when an object is destroyed
             if (m_List[i] == null)
                 m_List.RemoveAt(i);
             else if (m_List[i].Equals(null))
                 m_List.RemoveAt(i);
         }
         // sort the list according to each "depth" value
         // The highest value will be the first one
         m_List.Sort(Compare);
     }
 
     private void OnGUI()
     {
         // if there's no item in the list, just leave
         if(m_List.Count == 0)
             return;
         Prepare();
         Event e = Event.current;
         if (e.type == EventType.Layout)
         {
             Debug.LogError("The GUIManager only works without layout event");
         }
         else if (e.type == EventType.Repaint)
         {
             // draw them from the highest depth to the lowest
             for(int i = 0; i < m_List.Count; i++)
                 m_List[i].CustomOnGUI();
         }
         else
         {
             // other events are send from lowest depth to highest
             for(int i = m_List.Count-1; i>=0; i--)
             {
                 m_List[i].CustomOnGUI();
                 // stop if the event has been eaten:
                 if (e.type == EventType.Ignore)
                     break;
             }
         }
     }
 }

And here's a sample script that uses the manager:

 //GUISampleScript.cs
 using UnityEngine;
 using System.Collections;
 
 public class GUISampleScript : MonoBehaviour, ICustomGUI
 {
     public int GUIDepth { get; set; }
     public int StartingGUIDepth;
 
     void Awake()
     {
         // important! This will register this instance so the manager knows about it.
         GUIManager.Add(this);
 
     }
     // Use "CustomOnGUI" instead of OnGUI
     public void CustomOnGUI ()
     {
         GUIDepth = StartingGUIDepth;
         Vector3 p = transform.position;
         if (GUI.Button(new Rect(p.x,p.y,100,50),gameObject.name))
         {
             Debug.Log(gameObject.name+" has been clicked");
         }
     }
 }

So a class that wants to use our CustomOnGUI mechanism has to implement the ICustomGUI interface. That means your class need to have those things:

  • a public "int" property GUIDepth

  • a public method called CustomOnGUI

  • the class need to call "GUIManager.Add(this)" somewhere in Awake / Start in order to have the CustomOnGUI method run.

Note: You don't need to attach the GUIManager somewhere. It will create an instance automatically which will be marked with DontDestroyOnLoad. So it's better to not manually attach the GUIManager script somewhere. If you do, make sure the scene where you placed the script in is only loaded once. If you load the scene again you will end up with two instances which would mess everything up.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aisenai · Apr 29, 2015 at 09:35 AM 0
Share

ok thx, so there's no way to force a predefined order on script when useGUIlayout if disabled?

avatar image Bunny83 · Apr 29, 2015 at 07:37 PM 0
Share

@aisenai: Well, i'm not sure if the usual Script Execution Order might help here. However it only can be used to order different classes. So if you have the same class several times i don't think there's a way around that.

Of course another option is to manually distribute the OnGUI call from a single class in the order you like. Though it might be a bit tricky to get everything right. $$anonymous$$eep in $$anonymous$$d that you usually want the Repaint event occure from back to front and all other events from front to back. This is important so the topmost gui stuff will receive the input first.

You could even optimise the events. If a script has already "eaten" an event, you don't have to call the following elements.

edit
I added an example GUI$$anonymous$$anager that might do what you want.

avatar image aisenai · Apr 30, 2015 at 01:07 PM 0
Share

many thx Bunny83, great explanatiorn and example! Btw I think that the usual script order execution facility just order Awake, OnEnable and Update methods, so it's not viable. I'll try your example, I'll have to edit it a bit since we're using some tricks on the event chain in order to control what a user can click on the GUI widget during the tutorial stage.

Thx again!

avatar image antoine-unity · Jul 04, 2016 at 02:08 PM 0
Share

GUI.depth should be taken into consideration even if useGUILayout is set to false. We have entered an issue for this : https://issuetracker.unity3d.com/issues/monobehaviour-dot-useguilayout-equals-equals-false-breaks-gui-dot-depth

Of course, implement your own GUI$$anonymous$$anager will give you greater control over the UI but the GUI system should be able to handle this situation :)

avatar image Bunny83 antoine-unity · Jul 04, 2016 at 03:37 PM 0
Share

How could GUI.depth be taken into consideration without the Layout event? You set the GUI.depth inside the OnGUI method and the depth controls the order in which OnGUI should be called. You can't change the order of the OnGUI calls after you invoked them already. That's only possible when you call it twice.

OnGUI is an immediate mode GUI. Things are drawn immediately when the code is executed. So you can't change any order afterwards. That's also the reason why GUI,Window doesn't work when you disable the Layout event. Unlike GUI,depth the GUI.Window documentation states this:

if $$anonymous$$onoBehaviour.useGUILayout is set to false then a call to GUI.Window will not have any effect, even though it is not a GUILayout function.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity Invoke method delay factor vary device to device 1 Answer

DrawTexture GUI iPhone 1 Answer

Detect when a touch has been captured by the GUI 3 Answers

GUI refresh time on platforms slower than Unity Editor 1 Answer

Optimizing OnGUI - Too many gui elements? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges