Sorry for this probably trivial question, but I just can't figure out how to disable the functionality of AC menu buttons by script.
I tried a lot of things, like
AC.MenuButton SomeButton = PlayerMenus.GetElementWithName ("Main Menu", "Some Button") as SomeButton;
SomeButton.SetUIInteractableState (false);
etc...
Any hint would be greatly appreciated.
Thanks!
Jan
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
Is the Menu being rendered with Unity UI or AC, and what is it you're trying to do, exactly? Disable the interactivity, or the visibility?
Calling SetUIInteractableState should work if the Menu uses Unity UI (see the Scripting Guide), but not for AC. Bear in mind that this'll be set automatically when entering/exiting Cutscene mode, however.
Scripting with any menu is generally much easier when using Unity UI, so convert your menu to that if you haven't already. If you then attach a Canvas Group component to your Button, you can control its interactivity by setting the Canvas Group's Interactable field. Since AC won't be controlling this, there won't be a conflict and you can manipulate it to override AC whenever you need to.
To get the Canvas object of a Menu:
Just wanted to let you know that your idea with CanvasGroup worked like a charm. Stupid me, that this didn't come to my mind myself. But thank you 1000x!
Hi, I'd like to use this function but have a few questions.
If I get the Canvas object of a Menu with :
Canvas myCanvas = PlayerMenus.GetMenuWithName ("menuName").canvas;
That means I can use
myCanvas.SetUIInteractableState = false/true
??If I want to get specifically the menu element, would it be:
Canvas myCanvas = PlayerMenus.GetElementWithName("menuName", "menuElementName").canvas;
??And to be able to use it, do I have to create a script with those lines, add the script to the menu canvas component and then use the Object: Call event Action, and create an event that refers to the canvas and calls the appropriate function in the component?
Now, as an alternative I found in the scripting guide, the function
AC.MenuElement.isClickable
, does it work similarly toSetUIInteractableState
? If this is something you'd recommend, then would you kindly advise on how to create a custom action to change menu elements to clickable and non clickable?Thanks so much for your help!
What's the context? To disable all elements within your UI, or just one in particular?
My above mention of accessing the Canvas was as a means to simplify the process by using a Canvas Group component's Interactable field. If you're dealing with the original issue of affecting only a particular Button element, you don't need to deal with "canvas" at all.
To get a specific menu element, you can call:
You then need to cast it to the appropriate subclass. In the case of a Button element:
From here, you can then call SetUIInteractableState:
Hi. Is it ok to continue this topic here even if it's a few years old or should I create a new one?
My goal is to temporarily disable specific buttons from a menu. Following your suggestion:
MenuElement walk_element = PlayerMenus.GetElementWithName("IconBar", "Walk");
MenuButton WalkButton = (MenuButton)walk_element;
WalkButton.SetUIInteractableState(false);
I attached the script to a prefab and used the Object: Call event in a action list when needed. But unity is throwing this error:
InvalidCastException: Specified cast is not valid.
As an alternative, I tried:
MenuElement walk_element = PlayerMenus.GetElementWithName("IconBar", "Walk");
MenuButton WalkButton = walk_element as MenuButton;
WalkButton.SetUIInteractableState(false);
But a different problem arises, the WalkButton is returned as Null:
NullReferenceException: Object reference not set to an instance of an object
I also tried:
MenuElement walk_element = PlayerMenus.GetElementWithName("IconBar", "Walk");
UnityEngine.UI.Button WalkButton = walk_element.GetObjectToSelect().GetComponent<UnityEngine.UI.Button>();
WalkButton.interactable = false;
This makes the button non interactable, but seems to be reset when I open the menu, since my menu Appear Type is Mouse Over.
I'd like to disable a few buttons and keep them disabled until I manually set them back to normal in the action list.
Greatly appreciate any advice.
Is it possible the element involved is a MenuInteraction, not a MenuButton?
If you're looking to disable several at once - and prevent any reset - use a Canvas Group as mentioned above.
If you attach a CanvasGroup to the parent of the element's UI object, you can use:
Thanks! it's working as intended now.