Forum rules - please read before posting.

Move UI Menu

Is it possible to move a UI Menu through scripts?
I've tried several available functions but none of them seem to work. As for an AC menu no problem but I can not do the same thing with an identical menu but with Source = Unity UI. The same functions I use with AC Menu can not be used with UI Menu but after several tests I would like to know if it is possible.

Comments

  • edited October 2017
    I got a look at menu.cs and for example I saw that SetCentre is also for UI Unity. I checked all the conditions and they are present for my inventory. In my case the routine is limited to:

    Vector3 targetPositionUI = new Vector3 (_position.x, _position.y, rectTransform.transform.position.z)
    rectTransform.transform.position = targetPositionUI

    But what happens is nothing.
    My menu appears in the default location but I can not move it in any way.
    Maybe I have to instantiate it differently?
    I use MoveItemMenu = PlayerMenus.GetMenuWithName ("CursorOnItem") and working with a menu Source = AC I read its position on the screen and I move it by getting animations but the same menu rebuilt with Unity appears on the screen but there is no way to read its properties and to act on it.
    Any suggestions?

    [edit]
    I just tried to insert directly into script:

    Vector3 targetPositionUI = new Vector3 (100f, 100f, MoveItemMenu.rectTransform.transform.position.z);
    MoveItemMenu.rectTransform.transform.position = targetPositionUI;
    Debug.Log (targetPositionUI);

    The log reports 100.0, 100.0, 0.0
    This could mean that the value of z has been read correctly or is it just a default value?
    In any case, the menu did not move.
  • Please provide more details when asking questions - in this case, a shot of the Menu's full list of properties in the Menu Manager, the UI's Canvas, and one of the UI in the scene for context.

    How a Menu is positioned is dependent upon its Position type field.  If it's set to Manual, then it can be controlled through script - however other settings assume AC has control and it is updated accordingly.  If you set it to e.g. Follow Cursor, and remove your own scripts, does it correctly move?

    Is your "RectTransform" correctly assigned?  It should be a direct child of the Canvas that spans the total area of the UI - see the provided default UI prefabs for examples.

    The Menu script's SetCentre function can be used to reposition UI menus, but if the Canvas' Render Mode is set to World Space then SetCentre3D must be used instead.  Generally speaking, the scripting guide can be used to learn the available functions - here is the Menu script's entry.
  • I discovered what interfered. My main UI menu needs some layout components that prevented moving the menu.
    But now I came across a behavior that apparently has no explanation, really too weird for me.
    For two days I tried in in every way to overlay two identical UI menus using the SetCentre () and GetSlotCentre () functions, but I did not succeed, so I did a test that would seem resolutely.
    I have removed all the code from my script using only this:
    (For the two menus Appear type and Position type = Manual)

    myscreenSize = AdvGame.GetMainGameViewSize ();
    Vector2 myPosHide = new Vector2 (400f, 180f);
    MoveItemMenuHide.SetCentre (myPosHide);
    slotCentreHide = MoveItemMenuHide.GetSlotCentre (myInventoryBoxHide, 0);
    Vector2 myPos = new Vector2 (slotCentreHide.x, myscreenSize.y - slotCentreHide.y);
    MoveItemMenu.SetCentre (myPos);
    slotCentre = MoveItemMenu.GetSlotCentre (myInventoryBox, 0);
    Debug.Log (" slotcentrehide " + slotCentreHide);
    Debug.Log (" slotcentre " + slotCentre);
    Debug.Log (" screensize " + myscreenSize);

    slotCentreHide = 400.00, 528.0
    slotCentre = 400.0, 528.0
    myscreensize = 1259.0, 708

    The two menus overlap perfectly

    image

    But I need to make the MoveItemMenu menu appear exactly on an inventory item and to do this I use the MoveItemMenuHide menu set to appear onHotspot.
    Then I read the coordinates of this menu and I give it to the second menu that is set as manual. This way I should have my menu exactly on a item and since its Position type = Manual I can move it by simulating an animation.
    Just comment two lines and change the setting of the first menu

    (First menu: Appear type = While Inventory Selected and Position type = On Hotspot)
    (Second menu: Appear type and Position type = Manual)
    code:
    myscreenSize = AdvGame.GetMainGameViewSize ();
    //Vector2 myPosHide = new Vector2 (400f, 180f);
    //MoveItemMenuHide.SetCentre (myPosHide);
    slotCentreHide = MoveItemMenuHide.GetSlotCentre (myInventoryBoxHide, 0);
    Vector2 myPos = new Vector2 (slotCentreHide.x, myscreenSize.y - slotCentreHide.y);
    MoveItemMenu.SetCentre (myPos);
    slotCentre = MoveItemMenu.GetSlotCentre (myInventoryBox, 0);
    Debug.Log (" slotcentrehide " + slotCentreHide);
    Debug.Log (" slotcentre " + slotCentre);
    Debug.Log (" screensize " + myscreenSize);

    slotCentreHide = 585.5, 575.7
    slotCentre = 585.5, 575.7
    myscreensize = 1259.0, 708

    As you can see the position of the first menu coincides with the location of the second menu but the visual effect is another:

    image

    Reducing the screen size by approximately half the offset increases:

    image

    Do you have any idea?



  • edited October 2017
    I did another experiment, I manually moved a menu to the coordinates that result from the log.
    The position corresponds to that of the menu, so the two SetCentre and GetSlotCentre functions overlap perfectly. This should mean that the problem arises from the wrong coordinates returned from the first line, slotCentreHide = MoveItemMenuHide.GetSlotCentre (myInventoryBoxHide, 0) which I get with Position type = On Hotspot. When I click on the item my menu should be overlying on the same item and therefore the coordinates other than the returned ones
    I specify that the slot size of the two menus is the same as that of the main inventory and the pivot at 0.5, 0.5
    Canvas' Render Mode is set to Screen Space but differently from the tutorial Creating an Inventory bar in Unity UI, Grid is child of Panel.
    I've tried to look for AC scripts but I did not find the relative function.

  • edited October 2017
    I'm afraid I'm really not sure which menu is which, here, or what it is you're fundamentally you're trying to achieve.  I feel you've skipped over those details too briefly for me to understand.  Please try to explain more clearly the basics of the setup.

    It looks to me like the smaller window doesn't give a bigger offset - just the same offset in exact pixels.

    Be aware that you're not limited to using AC's SetCentre function.  If the Appear type is Manual, you can manipulate the UI's RectTransform components directly through script.

    The SetCentre function will centre the RectTransform on a given point - which may differ visually from the rounded square that's visible in your screenshots.  To ease debugging, add a grey image or something to the Menu's RectTransform boundary so that we can see more clearly where the boundary and centre of the Menu lies.

    I would also suggest writing a Debug statement to output the position of the mouse cursor, so that you can see the co-ordinates any given point on the screen at runtime.

    But to summarise, so far as I can tell, the code works when the "hidden" menu's position is Manual, but there's an offset when "On Hotspot".  Is that right?  If so, at what point are you calling the code?  Every frame?
  • edited October 2017
    But to summarise, so far as I can tell, the code works when the "hidden" menu's position is Manual, but there's an offset when "On Hotspot".  Is that right?

    Correct

    If so, at what point are you calling the code?  Every frame?

    No, through an ActionList, Standard interactions -> Use on inventory.

    Be aware that you're not limited to using AC's SetCentre function.  If the Appear type is Manual, you can manipulate the UI's RectTransform components directly through script.

    I know, I've tried that too.
    But as I said the problem arises when I place the small hidden menu on the item using On Hotspot. 
    If the returned coordinates are wrong, then they have an unexpected offset, all the following code can not work.
    I need to know the coordinates of the clicked item to be able to place on it a small menu that contains the same item. At the click of the player I hide the item clicked and I display the small menu so I can animate it as if it was the clicked item. I have to use this way because it does not seem it is possible to move the item directly from its inventory.
    For this reason I use a second small menu that appears through the On Hotspot on the clicked item. It only serves me to find the coordinates on the screen of the clicked item, then I close the hidden menu and I assign these coordinates to the second small menu which can be moved by creating an animation.
    But, as you can see, the small hidden menu that I use as a reference, despite having the same size of the item, does not return the same coordinates as the inventory item.
    Two questions: is there another way to find the coordinates on the screen of a clicked item?
    But even if there was, I would like to understand why this offset, in which part of your code could I look?
    My two small menus are identical to inventory slots, pivot is to center, if one of these menus is set as While Inventory Selected and On Hotspot and your code reads the center of the slot and assigns it to the menu I do not see why there is an offset.
  • Hi Chris, I made a short video to give you the idea of what I want to do.
    In this video everything works well but the two small menus are source = AC.
    I would like to use UI menus as the main inventory to avoid having to manually resize them in the editor menu.
    In this video I make the following steps:
    I open the inventory
    I go on an item and click it
    my script starts
    the small hidden menu is set as While Inventory Selected and On Hotspot
    At the click I read the coordinates of this menu, whose center is superimposed on that of the item, I close it and I hide the item (setting -> Hide from menu)
    I assign these coordinates to a second identical menu that replaces the item without the player noticing it
    then I do a little animation by placing the false item where it should be
    I close this menu and I use KickStarter.cursorManager.inventoryHandling = InventoryHandling.ChangeCursorAndHotspotLabel to display the Cursor (optional) that was disabled.

    video: 



  • edited October 2017
    I found the direct way to get the coordinates of the clicked item.
    Within a custom event:

    Vector3 slotPos = _element.GetRectTransform (_slot).position;

    it returns exactly the center of the clicked item in screen coordinates.

    Now I no longer need to use the small hidden menu set as On Hotspot but I will simply place the small menu to be animated at these coordinates.
    But the offset mystery remains...
  • Are you forcing the aspect ratio in the Settings Manager?
  • No, it is not selected.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.