Search Unity

ICanvasElement LayoutComplete not actually complete

Discussion in 'UGUI & TextMesh Pro' started by CDF, Feb 9, 2017.

  1. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    What event/callback can I use to know that a Canvas has completed its full layout?

    Simply registering a ICanvasElement and responding to the LayoutComplete is not enough.
    CanvasUpdateRegistry.IsRebuildingLayout is still true.

    so... Let's say I was spawning some objects in that LayoutComplete method and disabling them.

    CanvasUpdateRegistry complains:

    "Trying to remove element from rebuild list while we are already inside a rebuild loop. This is not supported."

    Ok fine, I don't want to disable something in the rebuild loop, which is why I'm doing it in LayoutComplete. Maybe we need another method LayoutActuallyCompleteNow :cool:
     
  2. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    For now I'm using a custom yield instruction:

    Code (CSharp):
    1. public class WaitForLayoutComplete : CustomYieldInstruction {
    2.  
    3.     public override bool keepWaiting {
    4.  
    5.         get { return CanvasUpdateRegistry.IsRebuildingLayout();    }
    6.     }
    7. }
    8.  
    9. public class MyObject : MonoBehaviour {
    10.  
    11.     public GameObject prefab;
    12.  
    13.     IEnumerator Start() {
    14.  
    15.         yield return new WaitForLayoutComplete();
    16.  
    17.         GameObject instance = Instantiate(prefab);
    18.         instance.SetActive(false);
    19.     }
    20. }
     
    a436t4ataf and KUDr like this.
  3. studentutu

    studentutu

    Joined:
    Oct 22, 2017
    Posts:
    121
    register into it via
    CanvasUpdateRegistry.RegisterCanvasElementForGraphicRebuild(this);
    CanvasUpdateRegistry.RegisterCanvasElementForLayoutRebuild(this);