Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

So... how do you actually place uGUI elements in code ?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Kasse, Sep 12, 2014.

  1. Kasse

    Kasse

    Joined:
    Dec 20, 2013
    Posts:
    18
    Hey guys.

    So with the "release" of the new GUI i've been trying to implement an inventory in it. For far far longer than i care to admit.

    I just seriously can't figure out how to do it. I want to create a window, in code, and then create the slots, also in code, and attach them to the window. However easy that seems to be, i'm obviously too stupid to do it. And it's really starting to annoy me.

    I mean, i can infact create the window, even place it in the center of the screen. But i just can't seem to figure out how to place those damned anchors. Or how to get any actual values for that matter. Like... is there a way to actually get a position (in pixel) of the corners or edges of a recttransform ? Because if so, i can't find it. Everything i tried just returned weird crap i don't know how to handle.

    Obviously i'd like my slots to be inside the window, so it would actually be rather helpful to know it's position...

    I just really can't get my head around this whole anchor stuff. I was never good with GUIs but of what i did, 100% dealt with actual pixels, instead of this whole 0-1 buisness :/

    So could any of you guys just walk me through this ? I'm at the point where i can't even tell what i know and what i don't know, because everytime i think i figured something out it becomes disfunctional as soon i try something more :[

    Do i need to place those anchors and base the position on that ? Or should i place the window and then place the anchors based on that ? Does that even matter ?
    Sigh... the whole unity "let's throw every possible variable this object could have into it"-thing also doesn't really help. It's so frustrating to figure out what's actually part of a new class you haven't seen before. But that's just me rambling...


    Anyway, if anyone knows of some kind of tutorial that specifically talks about the code site of things, i'd really like to see that. Those basic "how to do stuff in the editor" tutorials really don't do it for me :/

    Oh and btw... is there a build in way to drag windows ? Or even images/buttons (items) for that matter ?
     
  2. d34thst4lker

    d34thst4lker

    Joined:
    Aug 4, 2013
    Posts:
    124
    I would like to know this stuff too. I'm trying to dynamically load a certain amount of buttons onto a panel but not sure how to do that. I'm thinking this needs to be done programmatically but even if I were to instantiate an object through code and add a button script to it. That doesn't help me position them correctly.

    It would be great to have some answers to this.
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    In the Dialogue System, quest log windows dynamically add quests in code. I followed the tips in Creating UI elements from scripting, but I'm afraid I also had to follow those "how to do stuff in the editor" tutorials. First I set up a prefab, which is a panel with some buttons and text labels, using auto layout. Auto layout was the key for me. Then I put that inside a scroll rect that also uses auto layout. I used Vertical Layout Groups, but for your inventory you'd probably want to use a Grid Layout. That said, I'll confess that I was pulling my hair out for a week trying to figure out exactly how to do the auto layout. But in the end it works nicely (minus a scrollbar bug that should hopefully be fixed in the next beta).

    I haven't watched them, but 3DBuzz also has a new UI tutorial. The first part is free. You have to pay to watch the second part, which I think covers coding and auto layout. I don't know if this inventory tutorial series would be any help or not either, but I thought I'd mention it.

    Melang has been curating a Useful 4.6 Scripts Collection. It includes Riolis' UIWindowBase for draggable windows.
     
  4. Kasse

    Kasse

    Joined:
    Dec 20, 2013
    Posts:
    18
    Thanks. The link to the script collection was rather useful, though i found it myself before i read this. Also it's not really all that helpful for the code itself, since that's rather trivial. But it gave me a direction, namely the new interfaces. I just wish there was more docu on all this new stuff.

    I've also figured out how to place stuff by doing something like this:
    Code (CSharp):
    1.                
    2. window.sizeDelta =newVector2(horizontalSlots * slotPrefab.sizeDelta.x, verticalSlots * slotPrefab.sizeDelta.y);
    3. window.position =newVector2(Screen.width *0.5f,Screen.height *0.5f);
    4.  
    5. window.anchorMin =newVector2(((window.position.x /Screen.width)-(window.sizeDelta.x *0.5f)/Screen.width),((window.position.y /Screen.height)-(window.sizeDelta.y *0.5f)/Screen.height));
    6.  
    7. window.anchorMax =newVector2(((window.position.x /Screen.width)+(window.sizeDelta.x *0.5f)/Screen.width),((window.position.y /Screen.height)+(window.sizeDelta.y *0.5f)/Screen.height));
    It's obviously rather annoying to always convert stuff around. I'm sure there is a better solution, though i haven't found it yet. A few helper or even extention methods will solve it quite nicely though.
     
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    I get the feeling that their direction is more toward using auto layout. Maybe this is why their docs are a bit skimpy on this.

    BTW, I just read this in the FAQ:

    They mentioned this in the "Creating UI elements from scripting" page, but I missed it.
     
  6. Kasse

    Kasse

    Joined:
    Dec 20, 2013
    Posts:
    18
    Well those auto layouts don't seem to work all that well though. I tried the grid one and it's terrible. At least as far as i can tell. You seem to have little control over how it does things which makes it just completly unusable to me. But then again, i haven't found the time to look at that closely.