Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by $$anonymous$$ · May 10, 2017 at 08:12 AM · uicanvasissuesorting order

Canvas override sorting issue.

Hello guys! I have a problem with sorting Images. I know that you can change order with chaning position in hierarhy, but i should set as child my panel between background and other panels to different gameobjects all the time, so i just added 2 canvases and enabled override sorting, and works like a charm, but whenever i disable/enable object, sorting doesnt work anymore, only way to change this in runtime change sorting order number manually/via scripting. alt text How can i fix this? Or why does this happen?

1.jpg (483.2 kB)
Comment
Add comment · Show 4
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 RobAnthem · May 10, 2017 at 08:22 AM 0
Share

You could always add a call to the sorting in your OnEnable() of the canvas. Though to be honest, you should never have more than one canvas in the same space-type, it just doesn't make sense when your entire GUI can and should exist in a single canvas.

avatar image $$anonymous$$ RobAnthem · May 10, 2017 at 08:26 AM 0
Share

I agree with you, i wanted to have single canvas, but how would you change order of sprites if chaning pos in hierarhy is problematic?

avatar image RobAnthem $$anonymous$$ · May 10, 2017 at 08:32 AM 0
Share

You could create a series of empty gameobject and use them as a dynamic layering system, childing whatever is intended to be in the front to the front object, and the rest to the various back layers. Even create a Layer script, so you can keep awareness of when objects need to be moved to a lower layer or up a layer.

As an example, lets say this is our Layer class

 public class Layer : $$anonymous$$onoBehaviour
 {
     public GameObject childElement;
 }

And this was our GUIELement class attached to each Panel like Inventory, Questlog, etc.

 public GUIElement : $$anonymous$$onoBehaviour
 {
     void OnEnable()
     {
         LayerSystem.instance.$$anonymous$$oveToTop(this.gameObject);
     }
 }

And this would be our LayerSystem

 public class LayerSystem : $$anonymous$$onoBehaviour
 {
     public static LayerSystem instance;
     public Layer[] layers;
     public Canvas mainCanvas;
     void Awake()
     {
        instance = this;
        if (mainCanvas == null)
       {
          mainCanvas = FindObjectOfType<Canvas>();
       }
     }
     public void $$anonymous$$oveToTop(GameObject element)
     {
          if (layers[layers.Length -1].childElement != null)
          {
             if (!layers[layers.Length -1].childElement.activeInHierarchy)
             {
                 layers[layers.Length -1].childElement.transform.parent = mainCanvas.transform;
             }
             else
             {
                 $$anonymous$$oveDown(layers[layers.Length -1].childElement, layers.Length -2);
             }
             layers[layers.Length -1].childElement = element;
             element.transform.SetParent(layers[layers.Length -1].transform);
          }
     }
     public void $$anonymous$$oveDown(GameObject element, int i)
     {
          if (i >= 0)
          {
             if (layers[i].childElement != null)
             {
                 if (!layers[i].childElement.activeInHierarchy)
                 {
                     layers[i].childElement.transform.parent = mainCanvas.transform;
                 }
                 else
                 {
                     $$anonymous$$oveDown(layers[i-1].childElement);
                 }
             }
             layers[i].childElement = element;
             element.transform.SetParent(layers[i].transform);
          }
         else
        {
           element.transform.SetParent(mainCavas.transform);
        }
     }
 }

Sorry if it seems a bit convoluted, just wanted to make sure it was ironclad.

avatar image Paul-van-der-Laan · May 20, 2017 at 07:00 PM 0
Share

I have the except the same issue. With my world space UI, it looks okay from the start. But whenever I disable and enable the canvas, z-sorting is not correct any more, even when I add a lot of z-space between the elements.

I am using 5.6.0f3 .

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by HuntNight · May 20, 2017 at 08:47 AM

Have the same problem. I have soleved it this way:

 // Use this for initialization
 void Start () {
     GetComponent<Canvas>().sortingOrder++;
     GetComponent<Canvas>().sortingOrder--;
 }
 
 // Update is called once per frame
 void Update () {
     
 }

}

Comment
Add comment · 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

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

110 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What sets Canvas.renderOrder and why would it not respect the sorting order? 0 Answers

UI object drawing order is different between Unity Editor and Standalone version 1 Answer

4.6 UI sort order in fornt of IMGUI/OnGUI possible? 2 Answers

Transform to RectTransform Conversion 1 Answer

Using Canvas/Gui with splitscreen(several cameras) 0 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