Hey, so here is what I am trying to do, couldn't find a similar answer anywhere:
I subscribed my script to OnStartSpeech_Alt. I retrieve a desired menu when the script starts, and a desired MenuElement (in this case, MenuLabel). This is all over a Unity UI prefab.
So when OnStartSpeech_Alt runs, I try to duplicate the MenuElement for each different speech.GetSpeakingCharacter() as they show up. I try doing this with the element.DuplicateSelf(false, false) .
In the script, it duplicates alright, but it doesn't show anywhere in Scene. The duplicate comes with no ParentMenu in it, and when I try to add it to the proper Menu.elements nothing changes.
It feels like I am missing some AttachToMenu() function somewhere. At the moment, the way around I found was to get the element.uiText.gameObject and go full Unity, but that feels very wrong. ![]()
So, to sum it up, how can I duplicate a MenuElement in scene by script?
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
Welcome to the community, @Senkh.
Duplicating an Element class merely creates a new class instance. If the Menu itself is linked to Unity UI, it won't duplicate the linked components it's associated with.
This operation is intended for use when copying an entire Menu - not single elements within it. When duplicating individual elements in a UI canvas, you have the added difficulty of having to re-assign and connect new Constant ID values to the new UI elements - which is how AC retains connections between elements and UI objects.
Can you explain what you're fundamentally trying to do, in terms of what effect you're trying to achieve in-game? Perhaps an alternative approach can be found.
Thanks Chris for the fast response!
Yeah, so what I am trying to do is to manage some dialog behaviors akin to group meeting. In our game, dialog scrolls up, in a similar way to messengers, so the player can re-read past messages. We have a custom menu for that.
The way we are doing it now is we subscribed to OnStartConversation and OnStartSpeech, and as character jam in the conversation we instantiate new unity labels inside the unityUI prefab. That works, but as you mentioned the gameobjects remain unlinked with elements in the menu system.
The real problem arises when we try to do the same with other elements, let's say the dialoglists. At the moment we are thinking of populating the menu using a customAction to retrieve the conversations before the menu is turned on (so we can use the OverrideConversation of dialogList). We were hopping we would be able to create new dialoglists at runtime, but couldn't find a way to do that. So the way we are doing it now is we have a bunch of dialoglists in the menu that remain disabled until the required time. This is ofc becoming a bit troublesome.
Is there some guide on how to create and link menu Elements at runtime? Is that even a possibility? Thanks again for the support!
It should be possibe, yes.
If you've (separately) duplicated both a Unity UI element, and its AC element counterpart, you can edit the element's class values to re-link it.
In the case of multi-slot elements such as a DialoguList, you have to iterate through the uiSlots array, followed by a call to recalculate it's appearance:
Though, it might be more trouble than it's worth. It's not stricty necessary to rely on AC's own elements for the display of AC logic blocks.
In the case of a Conversation, for example, you can extract a Conversation's list of available options and display the labels in a custom UI, unlinked from AC. To run a dialogue option, you can also do so through a custom click handler, made to invoke RunOption.
Hmm, interesting. Ok, so if I wanted to duplicate a portrait inside a Menu, would I be able to do it by something more or less like this?
Looks OK - but again, you don't need to strictly map a Unity UI element to an AC menu if you're handling it all through script.
From the Speech parameter provided by the OnStartSpeech_Alt event, you can grab the character's portrait texture directly, and apply it to your Image: