Forum rules - please read before posting.

Unity UI inventory and new script

I've created a new inventory with Unity UI and I've integrated it into AC.
I do not know if I'm following the right path: I prefer not to have the ShiftLeft and ShiftRight buttons so I've built two inventory, one with the grid on one row and the other on two rows.
I open the inventory through a button and using the ActionList I check the number of carrying items and I show the correct one.
Could this be a right way?

Now, however, I need to check different aspects while the inventory is open, for example, disable the button that opened it (and re-enable it when the inventory is closed), close the inventory with a click out of it, eventually change the items icons on mouse over (I would like to extend the features provided by AC).
With Wintermute I was able to get all this with scripts but Unity is new to me and I have to figure out how to do it.
Where should i put the script that controls all this and how to run it?
In my trials and reading on the forum I experienced the creation of an Empty Object in the scene to which I attached a simple script that reads inventory items number and displays them in the console.
Then I created an ActionList on the button that opens the inventory and it works regularly.
I noticed however that I have to use Object -> Send message with Message to send = Custom and Method name = "my function name" because if I use Object -> Call event I can not set the correct parameters. Why?
Basically, when someone has some time, could tell me in detail what are the steps to run a script that stays active all the time when the inventory is open perhaps with a small example?
I just need to understand how to start, then with the Scripting Guide I should be able to walk alone.
Thanks.

Comments

  • Parameters can't be set in the Object: Call event Action because of an unfortunate limitation of Unity's event system when displayed in custom editors.

    Menus can be assigned ActionLists than run when turned on and off to help initialise other parts of your game - for example, to turn off other buttons etc.  Just be sure to set their When running fields to Run In Background so that they don't interfere with the GameState.

    For greater control through script, you can rely on custom events to run code when e.g. mouse-overing elements.  Section 12.2 of the Manual, as well as this tutorial, covers their use, while Menu-specific events are listed in Section 11.4.  Here's an example of OnMouseOverMenu:

    using UnityEngine;
    using System.Collections;
    using AC;

    public class MenuEventTest : MonoBehaviour
    {
       
        private void OnEnable ()
        {
            EventManager.OnMouseOverMenu += ElementOver;
        }
       
       
        private void OnDisable ()
        {
            EventManager.OnMouseOverMenu -= ElementOver;
        }


        private void ElementOver (AC.Menu _menu, MenuElement _element, int _slot)
        {
            if (_element != null)
            {
                if (_slot > 0)
                {
                    Debug.Log ("Menu: " + _menu.title + ", Element: " + _element.title + ", Slot: " + _slot);
                }
                else
                {
                    Debug.Log ("Menu: " + _menu.title + ", Element: " + _element.title);
                }
            }
            else
            {
                Debug.Log ("Menu: " + _menu.title);
            }
        }
       
    }

  • edited October 2017
    Thanks Chris.
    I have attached this script (still incomplete) to a GameObject and it seems to work.

    public void Update() {
      Menu inventoryMenu = PlayerMenus.GetMenuWithName ("Inventory1_5");
        if(inventoryMenu.IsEnabled()) {
          if(!inventoryMenu.IsPointInside(KickStarter.playerInput.GetInvertedMouse())) {
            if(Input.GetMouseButtonDown(1)) {
              inventoryMenu.TurnOff();
            }
            else if(Input.GetMouseButtonDown(0)) {
              if(KickStarter.runtimeInventory.SelectedItem == null) {
                inventoryMenu.TurnOff();
              }
            }
          }
        }
      }
    }

    But this way the script is always active even when the inventory is closed.
    If I rename the Update function in "MyFunction" I can call it through an ActionList with Send Message but it will be called once.
    Is there a way to keep it active?
    Supposing that there is, when do I close the inventory, should not it be necessary to stop it?
    PS
    Is it possible to put the *Menu InventoryMenu* out of the function to prevent it being instantiated by each frame? Obviously I'm wrong but the compiler always returns an error if it is not directly in the function.

  • Try IsOn instead of IsEnabled.  It shouldn't be running if the Menu is closed.  How is it closing, and what's its "Appear type" setting?

    You can make a private variable to move the Menu inventoryMenu out of the function:

    Menu inventoryMenu;

    void Start ()
    {
      Menu inventoryMenu = PlayerMenus.GetMenuWithName ("Inventory1_5");

    }


    Did you try the event I suggested?  It avoids the need for checks every Update.
  • Try IsOn instead of IsEnabled.  It shouldn't be running if the Menu is closed.  How is it closing, and what's its "Appear type" setting?

    Even with IsOn () it behaves in the same way.
    But why should not the script be active with the closed Inventory? It is attached to a GameObject in the scene, so I wondered if it was possible to start the script just after opening the inventory and stopping it by closing the inventory.
    At this time if I put in function "Debug.Log (" active ")" before "if (inventoryMenu.IsEnabled ())" the console always reports the active script even with the closed inventory, even just started the game.

    I open the inventory by clicking on a button, then I disable the button and the only way to close the inventory is by clicking out of it with the left button (without item).
    I close it only with TurnOff () and I show it with TurnOn(), Appear Type is Manual.

    You can make a private variable to move the Menu inventoryMenu out of the function

    I've tried everywhere and in any way, even as suggested by you, but the editor tells me "The name inventoryMenu does not exist in the current context".

    Did you try the event I suggested? 
    It works fine and I thought I would use it to change icons and other things, but it looks like it can not be used to intercept the click out of the inventory (to close it). I'm wrong?

  • If the Menu is off, it should be that the IsEnabled check is enough to prevent further code running.

    Is your UI prefab or in-scene?  You should find that, when the UI is off, the root Canvas GameObject is disabled - is that the case?  I'll look to see if there's an issue, but you could try placing the script on the Canvas instead of a separate GameObject so that it too only runs when the Canvas is enabled.

    I'm afraid not much advice can be given for a script error without any context - please post the full code you're using in e.g. pasteall.org

    The event can't detect "off clicks", but you can check the mouse enters anything else (including a Menu area with no Elements).
  • I'll look to see if there's an issue
    There is not, probably I explained badly.
    No code is executed after IsEnabled when the menu is closed. I wanted to avoid having Update function active during the game (while the menu is closed). Even if it is limited to checking whether the menu is open it would be a useless waste of resources.
    But yes, placing the script on the canvas does exactly as I wanted! Thanks.

    For the code I am certainly mistaken for the statement.
    The script contains no more than I posted.

    using System.Collections;
    using UnityEngine;
    using AC;

    public class TurnOffInventoryWithClickOut : MonoBehaviour {

    Menu inventoryMenu;

    void Start () {
    Menu inventoryMenu =  PlayerMenus.GetMenuWithName ("Inventory1_5");
    }
    void Update() {
                [code posted above]
            }
    }

    The console returns * Object reference not set ... *

  • Apologies - there should be no "Menu" in Start:

    inventoryMenu = PlayerMenus.GetMenuWithName ("Inventory1_5");
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.