Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question UI Toolkit Device Orientation Event

Discussion in 'UI Toolkit' started by PregOfficial, Apr 12, 2021.

  1. PregOfficial

    PregOfficial

    Joined:
    Feb 9, 2021
    Posts:
    3
  2. sebastiend-unity

    sebastiend-unity

    Unity Technologies

    Joined:
    Nov 9, 2015
    Posts:
    183
    Hi,
    I think you could register to GeometryChangedEvent on the window's rootVisualElement (you would want to hook that into your [AttachTo/DetachFrom]PanelEvent callbacks, to limit callback calls to the lifetime of the window's panel):

    Code (CSharp):
    1.    public class MyElement : VisualElement
    2.    {    
    3.       public MyElement()
    4.       {
    5.          this.RegisterCallback<AttachToPanelEvent>(OnAttachToPanel);
    6.          this.RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel);
    7.       }
    8.  
    9.       private void OnAttachToPanel(AttachToPanelEvent e)
    10.       {
    11.          panel.GetRootVisualElement().RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);
    12.       }
    13.  
    14.       private void OnDetachFromPanel(DetachFromPanelEvent e)
    15.       {
    16.          panel.GetRootVisualElement().UnregisterCallback<GeometryChangedEvent>(OnGeometryChanged);
    17.       }
    18.  
    19.       private void OnGeometryChanged(GeometryChangedEvent e)
    20.       {
    21.          // check for width/height ratio changes, etc
    22.          if (ratioHasChanged)
    23.          {
    24.             // Do something when layout changes
    25.          }
    26.       }
    27.       [...]
    28. }
    Would that do the trick or you are looking for something more specific?
     
  3. PregOfficial

    PregOfficial

    Joined:
    Feb 9, 2021
    Posts:
    3
    Thanks for your reply, the problem with the geometry changed event is that it' not getting called if you switch from Landscape Left to Landscape Right, because the panel size does not change.
    If you switch from Landscape to Portrait your example works, but my app only allows Landscape Left and Right.
     
  4. sebastiend-unity

    sebastiend-unity

    Unity Technologies

    Joined:
    Nov 9, 2015
    Posts:
    183
    Right. So it seems we don't send these events all the way to the UI Toolkit event system yet. I will start a thread with the team. Thanks!
     
  5. sebastiend-unity

    sebastiend-unity

    Unity Technologies

    Joined:
    Nov 9, 2015
    Posts:
    183
    PregOfficial likes this.
  6. najaa3d

    najaa3d

    Joined:
    Jan 22, 2022
    Posts:
    29
    Question: How does (or will) the UI Toolkit handle run-time device rotations? (Android and iOS) We our existing User rotates their device from Portrait to Landscape, our Xamarin Forms UI smoothly animates this rotation. How will it look with UI Toolkit? (both now, and in the future -- and how far into the future)

    -- Background --
    Our 2D/3D Map App is currently using Xamarin Forms and UrhoSharp for the graphics. We are considering switching over to Unity3D. Our app is very UI intensive, and fluidly supports device rotations.

    Since our entire project is written in C#, and the basis of UrhoSharp and Unity are similar enough, and the XAML and UXML are also similar -- we think this might be a feasible port.

    I believe the recent advances in UI ToolKit (which largely works like XAML) has opened the doors wide for more Xamarin apps to convert to Unity. Xamarin's "native control" approach was a bad idea - and is bugged. We'd prefer graphics-rendered controls, as is done with Unity now, and also, Unity itself allows us to extend our 3D view's graphics rendering considerably, and for the rest of our rendering, Unity is a much better choice than UrhoSharp. (C# wrapping of URHO3D)