Search Unity

Bug Wrong Positions for IMGUIContainer and ImmediateModeElement in Runtime

Discussion in 'UI Toolkit' started by TotallyRonja, Jul 3, 2020.

  1. TotallyRonja

    TotallyRonja

    Joined:
    Oct 1, 2016
    Posts:
    9
    As the title suggests I tried both the IMGUIContainer as well as make a custom ImmediateModeElement and with both of them I couldn't get the coordinates to work. it's as if they're "off" by 20 pixels in the y axis? Though even that isn't consistent as I move the elements around so I ended up giving up.
    I used version "0.0.4-preview" of the runtime module in Unity 2020.1.0b14.

    As a example I used this code as the onGUIHandler:

    Code (CSharp):
    1. Rect drawRect = new Rect();
    2. drawRect.y = -20;
    3. drawRect.size = new Vector2(100, 100);
    4. GUI.DrawTexture(drawRect, Texture2D.whiteTexture);
    And it ended up looking like this (the IMGUIContainer has a black background so you can see how a part of the rect was cut off even though it should still be on the container):

    upload_2020-7-3_18-15-20.png

    Now that I'm already making a thread I also want to mention another hurdle I stumbled upon:
    I couldn't make a custom element to show textures (any texture, not just Texture2d, I needed RenderTextures), not just because of the aforementioned bug, but also because it seemed impossible to serialize a reference to a complex datatype like a texture in a straightforward way?

    All of that being said I really like the tool so far, especially how clean the editor is and how it doesn't fight against you in the way that many HTML editors do. It's just a clean way of conveying you the data in the text files and giving you a preview. Excited to see it developed further, bugs fixed and the documentation expanded!!
     
  2. TotallyRonja

    TotallyRonja

    Joined:
    Oct 1, 2016
    Posts:
    9
    After recreating my project with ugui I ran into the error again and it comes from dealing with the fact that for some coordinate systems y=0 is at the top and for some its at the bottom. So my 20 pixel offset came from the top menu bar being 20 pixels high.
    I assume for the ImmediateModeElement its possible to undo the error with some smart math, but since the imgui plane in the IMGUIContainer is set in the wrong position and clips all your stuff I don't think its possible to use that one without this being fixed unless its in the middle of the screen(like a space filling context).
     
  3. AlexandreT-unity

    AlexandreT-unity

    Unity Technologies

    Joined:
    Feb 1, 2018
    Posts:
    377
    You should avoid using those as much as possible (especially IMGUIContainer which, honestly, should just NOT be exposed). What is it that you're trying to do exactly? The ImmediateModeElement should be a last resort, only used when the Mesh API is insufficient. Did you have a look at the latter?
     
  4. TotallyRonja

    TotallyRonja

    Joined:
    Oct 1, 2016
    Posts:
    9
    I was trying to render a RenderTexture into UI (for procedurally generated image stuff, most obvious other path was to use background textures and those only take Texture2D)
    I can see now that the MeshGenerationContext.Allocate does allow passing in a Texture, so that might also allow me to write a custom image component that allows setting Rendertextures from code. (I think the other 2 APIs seemed less scary to me because they build on older, well documented workflows)

    I also still can't see how I can easily reference serialized Objects (Like textures or scriptableobjects) via a UxmlAttributeDescription so I'm stuck with setting the variable during runtime(which is fine in my case).
     
  5. AlexandreT-unity

    AlexandreT-unity

    Unity Technologies

    Joined:
    Feb 1, 2018
    Posts:
    377
    In that case you should simply use the existing "Image" element. You can assign a RenderTexture to its image property.

    Are they assets or generated at runtime?
     
  6. TotallyRonja

    TotallyRonja

    Joined:
    Oct 1, 2016
    Posts:
    9
    I completely missed the image element, thank you so much that's all I needed!!

    and for the UxmlAttributeDescription I imaged having a reference to a asset in my file system. The obvious solutions of using a path or guid seem to need the UnityEditor namespace.
     
  7. AlexandreT-unity

    AlexandreT-unity

    Unity Technologies

    Joined:
    Feb 1, 2018
    Posts:
    377
    Last edited: Jul 8, 2020