How to Make In-Game Context Menu ?

Hello, I want to make custom in-game context menu. For example, I have building, when I select the building and on right click, it will show options I inserted like “Demolish”,“Change color”,“Upgrade”,etc. But the problem here is I don’t know how to handle UI components. If would help me, I will be very appreciate!

Look into raycast and button click. You connect these two together then enable a hidden menu. Pie menues in Blender are the coolest.

1 Like

So if i understand you correctly your trying to wrap your head around how to communicate between the UI and your player/target. There are a million ways to do this, but i’ll break down a simple example of how to do it that will hopefully give you some insight and get you started.

Take the building in your example, first the user right clicks the building. So first you would probably do a ray cast into the scene, and detect what object was hit, and if it was a valid object.

Now you need to determine what items to show for the object. One method of doing this is using events(either c# or UnityEvents). For the sake of simplicity, we’ll pretend we have a singleton UnitManager component that we can access through an static instance, and that the UnitManager has a c# event called UnitSelected that other components can subscribe to. Also, make sure the event has a parameter of whatever you want to pass in as data(in this case most likely the game object that was picked).

When you right click the unit, you set it in your UnitManager as the selected unit, and you raise the UnitSelected event, passing in the selected unit as the parameter to the event function. Your UI components which are subscribed to said event can then choose to enable or disable there button scripts/menus based on whether the selected unit has the properties that the button requires.

So if you have a “Demolish” button for buidlings, and the game object passed into the UnitSelected event doesn’t have a building component on it(or maybe it does but the canBeDemolished bool is unchecked), the demolish button could respond however it sees fit.

Hopefully that made any sense :stuck_out_tongue: Cheers

2 Likes