Zartox29
Zartox29

Reputation: 105

SWAT 4 Context Menu in Unity


I would like to make an interaction system that use a small context menu to choose what to do. The same menu you command bots in SWAT4. you can use the scroll wheel to select the option and press the Left mouse button to validate.

If you have not played SWAT4 the menu look as follow:

Swat4

What I have: When I aim at an object close enough, a little menu (panel with buttons) opens (SetActive(true)), but I made it with the editor. I don't see how to change the number of buttons and their positions on the fly, since every interactable object can have a number of options.

Here's the way I made the menu right now (I cant use the scroll wheel or mouse in it actually):

The menu I have so far

Objects example:
A door as these options: Open, LockPick, Blowing it
A pile of cash have only one option: Take...

What I would like:
A way to create a custom menu while in game with a number of options I can change, options name that changes, scroll up to select above option, scroll down to select under and left click to validate.

Conclusion:
The way I tried with the basic menu setup is not a good way. I don't know if I can use OnGui() for this kind of stuff. And if OnGui() is the correct way to do this how can I accomplish the result I showed in the first picture.

Does anyone have an idea of how I could realize that menu ?

Upvotes: 0

Views: 464

Answers (1)

Zartox29
Zartox29

Reputation: 105

It's Done :) (Thanks Foggzie)

If someone ever thinks of doing this type of menu at runtime here is how i did:

First, i set up my menu:

1)Create a panel.
2)Add a vertical layout group to it (or horizontal...).
3)Add a content size fitter to it, it's going to resize our panel automatically.

Then i added some buttons as child of the panel:

And deactivate them...

I won't show you the code, but i'll explain it a bit:

First, i have a C# script for every interactable object. This class derives from Iinteractable (an interface containing variables and an Interact function). One of these variables is a List of PossibleInteractions (which is an enum with all the interactions possible for this object).

On my player i have a C# script, containing a List of all my buttons. And in the Update:

I check if the player is in range and if he is looking to my object. If it's the case, i check the List of PossibleInteractions. If the "Open" Interaction is in this list, i activate my button (which is on my List). Everything is automatically resized (thanks to the content fitter and the layout group).

Since i can't validate my answer until tomorrow i'll continue posting advancement...

Here is the (still early) result: https://i.sstatic.net/Voo56.jpg

2nd part: https://i.sstatic.net/KHNFw.jpg

I hope it will help someone :).

Upvotes: 1

Related Questions