Search Unity

Is there a way to add an image to my UI from the editor (and not via UIElements.Image)

Discussion in 'UI Toolkit' started by niuage, Jun 3, 2021.

  1. niuage

    niuage

    Joined:
    Nov 17, 2019
    Posts:
    123
    I'm using Unity 2021.2.

    I know that there's an Image property in the Background section, but that's not quite an image right? It's still a background, meaning that if we set the size of the VisualElement to 100%, and height to auto, it will make the image disappear, as the element is actually empty from a content point of view.

    So let's say I want to add a simple logo in my menu, do I need to create it via code?

    ---

    For now I ended up using a css trick with a padding-top set to a percentage and changing the size of the background to fit inside its container.
     
    Last edited: Jun 3, 2021
  2. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    This won't work for you?
    https://docs.unity3d.com/ScriptReference/UIElements.Image.html

    [edit] Just to clarify, you can still use it in the UXML even if it is not shown in the UI Builder's available controls. Just insert it where it needs to be in the UXML and Builder will update with it. Although I see there are no properties exposed so you would have to set the actual texture or sprite via C#.
     
    Last edited: Jun 3, 2021
    niuage likes this.
  3. niuage

    niuage

    Joined:
    Nov 17, 2019
    Posts:
    123
    I was kind of hoping that I was missing something. Images are a common element in UIs, so having to manually update the UXML, and then again having to set the image via a script sounded like a very weird limitation to me :'(

    Thanks for your answer.
     
    MrLucid72 likes this.
  4. Well, you're using an alpha software with a preview package. Nothing is done in these yet. They will add the Image component to the UI Builder at some point, I'm pretty sure.
     
  5. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    650
    Somewhat old post but there is still no image in the "Libary" menu of the UI Builder. Actually you just have to add one, then you can copy&paste it. But the Inspector doesn't show the fields for the image, so you have to access it from code. Plus point of images: you can add sprites. Pretty useful when making an inventory item editor window.
     
    Deleted User likes this.
  6. Gerark87

    Gerark87

    Joined:
    Feb 2, 2013
    Posts:
    9
    There must be a way to add it to the Library. I can't believe it's not there yet.
     
  7. celinedrules

    celinedrules

    Joined:
    Jan 31, 2013
    Posts:
    17
    There is a really easy way to add it. Itr will then show up in the library under the project tab.

    Code (CSharp):
    1.  
    2. using UnityEngine.UIElements;
    3.  
    4. public class MyImage : Image
    5. {
    6.     public new class UxmlFactory : UxmlFactory<MyImage, Image.UxmlTraits>{}
    7.  
    8.     public MyImage()
    9.     {
    10.        
    11.     }
    12. }
    13.  
     
    t2g4 and reza_b_mirzaei like this.
  8. t2g4

    t2g4

    Joined:
    Nov 13, 2018
    Posts:
    46
    Thank you! So strange they didn't add Image Element to the UI Builder Library.
     
  9. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    The reason it was never added to the UI Builder library is because the UI Builder does not support custom USS properties. So even though you could add the Image in the UI Builder to your document, you wouldn't be able to set its image property. It's something we're still figuring out.
     
  10. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    988
    Is this available now?
     
    Last edited: Oct 25, 2023
  11. gersonbellodegoes

    gersonbellodegoes

    Joined:
    Jun 17, 2018
    Posts:
    2
    Do you know how to add a "binding path" from a sprite variable and change the image accordingly?