Forum rules - please read before posting.

Multiple Inventory boxes. Change visibility via script.

edited May 2016 in Technical Q&A
Hi everyone! 

Sorry for the noob question but how do you change the visibility property of an inventory box via scripting?

Thing is, I had just made a Unity UI menu which simulated tabs. It had the ability to move between the "inventory boxes" inside the menu (which were gameobjects with canvas grids and 8 child button/slots), by pressing the left and right keys(for keyboard and controller scheme). Took me a couple of hours to get it to work perfectly (navigation looped perfectly, even from the first and last element)...it took into account the available "inventoryboxes" in the menu by way of exposed arrays, meaning I could use the same scrip for as many "inventoryboxes" as I wanted and it would remain automatic (good because I'll have a lot of tabbed-like menus)... but when I went to connect it to AC, I found, to my dismay, that AC uses the button slots of a single canvas grid for all inventory boxes in the same menu... So, I have to redo the script...

I originally was Enabling and Disabling the inventory boxes (gameobjects) and changing the First Selected on the fly to create the effect, now by what I see, I'll have to change the visibility property of the inventory box. Ive seen it as an action in cut scenes and action lists, but I'm not sure how to do it in scripting. I'll probably have to discard the idea of a reusable script, too, cause I'm guessing you have to enter the specific name of the menu before changing the visibility?

Anyway, as usual, any advice will be greatly appreciated here.

Comments

  • edited May 2016
    Well, I've been trying to use the IsVisible property to see if that works, but it doesn't seem to do much... Here's more or less the code I'm using (by the way, I've stored the names of the inventory boxes in an array):

    //If player presses the Right key:
    // Cast the MenuInventoryBox element1, current
    AC.MenuElement myElement1 = AC.PlayerMenus.GetElementWithName (ACMenuName, MyInventoryBoxes[i]);
    AC.MenuInventoryBox myInventoryBox1 = (AC.MenuInventoryBox) myElement1;
    // Cast the MenuInventoryBox element2, next
    AC.MenuElement myElement2 = AC.PlayerMenus.GetElementWithName (ACMenuName, MyInventoryBoxes[i+1]);
    AC.MenuInventoryBox myInventoryBox2 = (AC.MenuInventoryBox) myElement2;
    //switch visibility
    myInventoryBox1.isVisible = false;
    myInventoryBox2.isVisible = true;

    Problem is, non of the inventory boxes seem to appear at all if even one is not set as "Visible". I expected that ticking only one of them would display only the contents of that one, but that's not what happens at all... and running the code seems to have no effect either. I mean, I've tried Debug.Log and it returns me True on the visibility of the current inventory box, but in the screen the menu still displays none of the inventory boxes, just an empty square where the slots should be. The slots only appear if all of them are ticked "Visible"... 

    Anyway, any help would be greatly appreciated...  I don't really want to have to create 5 menus, one for each inventory tab to simulate tabbing. There's still several other menus which will also have tabbed inventory boxes and it'll become long and messy If I have to clone all the menus...

    PS: Alternatively, is there a method to populate the item slots(buttons) on a unity UI manually?
  • After changing the visibility of an element manually, you also need to update the Menu's internal record of visible elements:

    AC.KickStarter.playerMenus.RecalculateAll ();

    Alternatively, you can do this with an ActionList asset, and run the asset file by calling it's Interact() function.
  • Thanks! I'll give it a try!

    PS: Ah, by the way, I didn't explain my feature request properly, I don't want one place to selected the sounds for each individual UI element, I want a section of the menu where you can select 4 to 6 sounds that will be use on all UI elements. Anyway, I posted a better explanation.
  • edited May 2016
    Umm... it didn't work... same thing, only if only one is not visible all disappear... by the way, is there any requirement for an inventory slot from Unity UI to work correctly? Currently the game object holding the slots/buttons only has a vertical layout group, is it mandatory to use a Grid layout? our do I have no other choice than to transfer all the logic into an Action list asset?...

    EDIT: Weird... Now I've noticed the menu is behaving strangely, even if the script isn't doing anything. The inventory box is basically frozen while the game is on Pause Mode, it only reacts when I unPause and pass the mouse over it... This, might relate to the problem I have with the First Selected. But there's more, I removed all other inventoryboxes and left only one to make sure and if I press up or down I can hear the elements scrolling but the cells remains empty. Then I tried changing the "When invisible" option, and I get different results, one shows all the cells but all empty, the other one shows only 4 cells with distorted scale, right in the center, but still empty. EDIT: Sometimes If I change the categories sometimes I get an item, but its the one with the shortest text, also I have a text label which shows the descriptions of the items and when it displays stuff the text is gigantic. Could it be that AC is scaling the text wrong? and because it gets too big for the cell it ends up not showing? I'm also using a button animation which just changes the color and the outline size a tiny bit, could using Animation be a problem too? EDIT: Disabled the animations and same thing, just a blank inventory box, all cells empty.
  • edited May 2016
    Okay... here's an update of what I have found out. also to be of note, as you probably already guessed, I'm using the inventory boxes to display the Item name Text, not the icons...

    1. First the inventory box only works when Not Paused... the contents simply doesn't update in pause mode...
    2. I fixed the strange issue which prevented the text to appear (from being too big) by simply adding the Text element into the buttons myself and setting the appropriate size and alignment.
    3. The label set to show an Item property ("Description" in this case) only shows stuff for a blink... unless you set a main image in the item, but then the pointer shows the image right at the center of the screen (where the pointer is locked...) and it just looks silly... also the options of when to display the property (in the label) are only good for mouse schemes...
    4. The highlighting animation not playing was me being silly, I forgot to set the animator to unscaled time. Also the gigantic text in the Description label, I had somehow ticked best fit.

    So that takes me to this questions:

    1. I'm not going to be dragging items out of the inventory, is there a way to get the inventory box to work in Pause mode?
    2. How do I reference the contents of a property by code so I can populate the description field when an object is highlighted/selected (without the mouse)? I don't want the text to disappear until the player presses down to go to the next item...
    3. how would I go about loading the main item image on a label or graphic element instead of it always appearing at the mouse pointer?? (I do have a space in the menu where I want to show the image but problem is getting the image there...)

    I'm sorry that this is getting a little complex... but all these menus are vital for the game to work right... anyway, any advice would be greatly appreciated...
  • edited May 2016
    UPDATE: I got the Tabbing to work. I think, I may have done something that caused a bug when I was creating the inventory boxes in the menu. When I tried to remake the inventory boxes (after deleting them for testing) they started to work right, taking separate buttons from the other inventory boxes.

    I can only imagine something in the way I did the process caused the issue (cause I really double checked and tripled checked I was dragging different slots to each inventory). My original UI Hierarchy looked something like this.

    Inventory box 1
    slot1
    slot2
    slot3
    ...
    Inventory box 2 (disabled)
    slot1
    slot2
    slot3
    ...
    Inventory box 3 (disabled)
    etc
    ...

    There are two things I think may have been the culprit, although I'll have to test it later if I have the time. First, all the slots had the same name, I'm not sure if that may have confused AC (although I doubt it) or that the other parents were disabled. Or second, but most important, I think, When I created the first inventory box in the menu manager, I set it up (text only, tied to category), made sure to leave all the slots empty, then copy pasted it one by one, renamed the copies and changed the categories tied to them, then when I tried to add the slots to each, they all instantly shared the same slots as the first inventory box. Later, when I instead created them one by one using the "new" button, they all worked as expected.

    Anyways, once the AC inventory boxes worked as expected, I went back to my original approach of disabling the object containing the inventory slots. That didn't work correctly, because starting the menu with the rest of the inventory slot groups disabled only loaded the contents of the first inventory box. So, I fixed it by adding a canvas group to all the game objects holding the slots, enabled them all, then instead of disabling the containers, I changed the alpha to 0 and the properties interactable and raycasting to false. And that did it. Also made sure that all the inventory boxes in AC menu manager were "Visible". Now tabbing works. hope that helps anyone else trying to make a tabbing system for Unity UI and AC.
  • I've answered questions 1) and 2) in your separate threads, but the currently-selected Inventory Item can be displayed in a separate InventoryBox element that has an Inventory box type of Display Selected.
  • Thanks, I'll give that a try!
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.