Search Unity

Question DocumentReadyEvent? InitializedEvent?

Discussion in 'UI Toolkit' started by PixelLifetime, May 11, 2020.

  1. PixelLifetime

    PixelLifetime

    Joined:
    Mar 30, 2017
    Posts:
    90
    Currently the only way I have found to get any data during initialization/creation about
    VisualElement
    width or height is through this event, which is fine.
    Code (CSharp):
    1. this._dialogueGraphView.contentContainer.RegisterCallback<GeometryChangedEvent>(
    2.     callback: (@event) =>
    3.     {
    4.         Vector2 size = new Vector2(x: 150.0f, y: 150.0f);
    5.  
    6.         Vector2 position = new Vector2(
    7.             x: this._dialogueGraphView.contentContainer.layout.width - size.x - marginValue,
    8.             y: this._dialogueGraphView.contentContainer.layout.height - size.y - marginValue
    9.         );
    10.  
    11.         Debug.Log(this._dialogueGraphView.contentContainer);
    12.  
    13.         miniMap.SetPosition(newPos: new Rect(position: position, size: size));
    14.     }
    15. );
    Is there an event that lets you do the same but is called just one time? I can do this
    this._dialogueGraphView.contentContainer.UnregisterCallback
    and get the required behaviour but it would be silly to reinvent the wheel. I can create a wrapper for this as a synthetic sugar but if there is such event directly then it would be a smell code.

    If it doesn't affect performance and isn't something that would take much time, it would be nice to have a an event like that for a
    VisualElement
    . As for
    DocumentReadyEvent
    - this can be simulated just by using the top tree element
    InitializedEvent
    , so there would be no need for it.

    Nevertheless, if there is no such event, I would gladly create a wrapper for it.
     
  2. ErnestSurys

    ErnestSurys

    Joined:
    Jan 18, 2018
    Posts:
    94
  3. PixelLifetime

    PixelLifetime

    Joined:
    Mar 30, 2017
    Posts:
    90
    Thank you! I will have to test if the data is there upon attachment, though. I have seen this event, though, I am not sure if it's the thing I am looking for. But it's hard to tell from the name what Panel actually means, why not VisualElement?

    "descendent of a panel" - seems it needs to inherit from the panel. https://docs.unity3d.com/ScriptReference/UIElements.CustomStyleResolvedEvent.html - this seems to be a better fit for what I am trying to achieve, I believe there is a better way. I should have dug deeper into the documentation.
     
  4. ErnestSurys

    ErnestSurys

    Joined:
    Jan 18, 2018
    Posts:
    94
    VisualElement won't be rendered if it has no context - panel, and will not have its size calculated until it is attached to one. You may think of the editor window as a panel but it is not really that simple.
    If you look into the UI Toolkit source code you will find that Unity uses this event for any kind of initialization logic.
     
    MaxHerrmann and PixelLifetime like this.