Search Unity

Lets Talk About View State

Discussion in 'UGUI & TextMesh Pro' started by Mikeysee, Oct 7, 2014.

  1. Mikeysee

    Mikeysee

    Joined:
    Oct 14, 2013
    Posts:
    155
    Okay so if you are like me then you have been struggling since 4.6 beta came with how to manage view state in your views.

    Say you have a popup in your game that has multiple pages:

    upload_2014-10-7_17-47-6.png

    upload_2014-10-7_17-47-26.png

    Now the problem is how to you toggle between the states?

    The obvious way is to use gameobject.setActive() but this seems to cause all kinds of problems with the unity UI not to mention a really nasty crash which wipes out the editor entirely.

    The other way is to use canvas groups:

    upload_2014-10-7_17-49-45.png

    But this seems to block clicks to other states no matter what combination of intractable / blocks raycast you set.

    The third way is to use separate canvas' then enable or disable them:

    upload_2014-10-7_17-51-36.png

    The problem is you must now add a GraphicsRaycaster before anything in that state can receive clicks. If you do this then you are back to the problem of the state blocking clicks. *sigh*

    So im looking for a discussion / guidance on how to handle view state correctly in the new UI..
     
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Works fine here.
    Create a panel manager script like this:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class GuiManager : MonoBehaviour
    6. {
    7.  
    8.     public GameObject pnl_PageOne;
    9.     public GameObject pnl_PageTwo;
    10.     public GameObject pnl_PageThree;
    11.     public GameObject pnl_PageFour;
    12.     public GameObject btn_Next;
    13.  
    14.     void Start()
    15.     {
    16.         Application.runInBackground = true;
    17.         btn_Next.GetComponent<Button>().onClick.AddListener(() => btn_Next_Click());
    18.     }
    19.  
    20.     void btn_Next_Click()
    21.     {
    22.         if (pnl_PageOne.activeSelf)
    23.         {
    24.             pnl_PageOne.SetActive(false);
    25.             pnl_PageTwo.SetActive(true);
    26.         }
    27.         else if (pnl_PageTwo.activeSelf)
    28.         {
    29.             pnl_PageTwo.SetActive(false);
    30.             pnl_PageThree.SetActive(true);
    31.         }
    32.         else if (pnl_PageThree.activeSelf)
    33.         {
    34.             pnl_PageThree.SetActive(false);
    35.             pnl_PageFour.SetActive(true);
    36.         }
    37.         else
    38.         {
    39.             pnl_PageFour.SetActive(false);
    40.             pnl_PageOne.SetActive(true);
    41.         }
    42.     }
    43. }
    44.  
     
  3. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    ???? we are not aware of this. We are using it ourselves without issue. Please log a bug if you are seeing something different!
     
  4. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Hmm, I did not test this on B20 though. I'm still on B19 at the moment.
     
  5. Mikeysee

    Mikeysee

    Joined:
    Oct 14, 2013
    Posts:
    155
    I wrote about the Beta20 crash problem in this post: http://forum.unity3d.com/threads/beta-20-crash-bug.272003/

    Unfortunately I was unable to reproduce this in a simple example. In a complex example I can get this to happen every single time.

    I wrote a class called a "View State Controller"

    upload_2014-10-8_6-54-19.png

    It lets you assign game objects then switch between them, it makes developing menus and other state based views really simple:

    upload_2014-10-8_6-54-52.png

    The problem seems to happen when you use this setActive true/fase method when they are nested within each other at runtime if you create and add a gameobject to something that has only just been set active within a parent that has only just been set active then it crashes. Or thats what it seems.
     
  6. Mikeysee

    Mikeysee

    Joined:
    Oct 14, 2013
    Posts:
    155
    I have updated the bug with some info from the error log. It looks like the problem is around:

    (0x009DD47C) c:\buildagent\work\d63dfc6385190b60\runtime\ui\canvas.cpp (767 + 0x3): UI::Canvas::UpdateEventIndexesRecursive + 0x2c
     
  7. Mikeysee

    Mikeysee

    Joined:
    Oct 14, 2013
    Posts:
    155
    Hmm after a few hours of testing it looks like you need to use Start() rather than Awake() to set up your initial state.. this was not the case pre beta 20