- Home /
In game right click menu
Hi,
I want to create an in-game right click menu. I want it to look like Windows right click menu. Also the buttons in the menu must be dynamically created since the menu will change with respect to clicked object. I tried using panels and instantiating button prefabs under it but I couldn't place it in the desired position (top left of the menu must be on the mouse position). Any suggestions or tutorials?
What's the problem with positioning the menu with the mouse position? Try changing the pivot or parenting the menu to a game object.
There are plenty of tutorials. If you're looking for one that specifically addresses your problem, then probably no. UI Canvas tutorial talks about positioning, that might help if you haven't watched it already.
I know these tutorials, they are basics but they didn't help. The problem with positioning is, top left of the menu must be on the mouse position. Pivoting and parenting didn't work or I couldn't make it.
Answer by killerstreak · Dec 03, 2015 at 10:45 AM
There are many ways you could do this really, but basically you could just get the position of the mouse with something like; Input.mousePosition. Then use that position to well, position whatever you want the menu to be made out of.
I suggest you could try creating the menu dynamically with for example Ongui(); You should look into those (especially because they are very easily manipulated by script and therefore viable in loads of situations).
Not actual working code just demonstrating what I mean, e.g.
var mousepos = Input.mousePosition;
GUI.Button(new Rect(mousepos.x (+25*), mousepos.y (-15*), 50, 30), "Click") ;
//*Note, I am not sure if the x and y coordinates are the center of the box, or the top left corner, but if they are you should add and substract 25px and 15px from the mousepose respectively, which is simply half the width and length of the box. If that is not the case then don't do that.
this will create a button that is 50px by 30px with the text Click in it. The corners located at the position of the mouse. I could go into more detail how all that works, but really, you can figure that out yourself, good luck! :)
Your answer
Follow this Question
Related Questions
Button onClick events will not be triggered 1 Answer
Button highlighted a pushable without touching it 0 Answers
How to use options in the Dropdown menu UI? 2 Answers
Menu button in game isn't working 0 Answers
Help With Adding Buttons To Panel 4 Answers