Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by phxvyper · Jun 16, 2014 at 06:32 AM · c#guimenuguilayout

GUILayout Context Menu on Right Click - Not Working!

This is a Unity*2D* Project. I am using OnMouseOver() for right clicks.

I've created a GUILayout Context Menu which is supposed to pop up when the user right clicks on an object.

Here is the code for the OnGUI() function in the module which is attached to the object i am right clicking on:

 void OnGUI() {
     GUI.skin.button.normal.background = ContextMenuItemTexture;
     GUI.skin.button.hover.background = ContextMenuItemTexture;
     GUI.skin.button.active.background = ContextMenuItemTexture;
     if (ShowContextMenu) {
         GUILayout.BeginArea(new Rect(ContextMenuPosition.x,
             ContextMenuPosition.y,
             ContextMenuItemTexture.width,
             ContextMenuItems.Count <= 5f ? ContextMenuItems.Count : 5f));
         {
             GUILayout.BeginScrollView(scrollPosition);
             {
                 foreach (KeyValuePair<ContextMenuAction, string> item in ContextMenuItems) {
                     if (GUILayout.Button(item.Value)) {
                         item.Key();
                         ShowContextMenu = false;
                     }
                 }
             }
             GUILayout.EndScrollView();
         }
         GUILayout.EndArea();
     }
 }
 

There is 1 item in the ContextMenuItems dictionary. The problem is that a GUILayour Button is not showing up at all, even before i changed the background texture to a texture i provided. No default button showed up, or anything.

The ContextMenuPosition is calculated in the Update() method like so:

 if (RightClicked) {
     ShowContextMenu = true;
     ContextMenuPosition.x = Input.GetAxis("Mouse X");
     ContextMenuPosition.y = Input.GetAxis("Mouse Y");
 }

Here are the OnMouse____() Methods used:

 void OnMouseDown() {
     LeftClicked = true;
 }
 
 void OnMouseUp() {
     LeftClicked = false;
 }
 
 void OnMouseOver() {
     if (Input.GetMouseButton(1)) {
         RightClicked = true;
     } else if (!Input.GetMouseButton(1)) {
         RightClicked = false;
     }
 }

What is wrong with my GUI code? How can i make the context menu appear? Keep in mind that this is NOT attached to a camera, but to an object that the user right clicks on.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Maerig · Jun 16, 2014 at 09:30 AM

Hey ! In OnGUI, I think you simply forgot to multiply this value

 ContextMenuItems.Count <= 5f ? ContextMenuItems.Count : 5f

by

 ContextMenuItemTexture.height

I hope that enough will fix it !

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Game Menus WIth Keyboard Input Control 1 Answer

Can you use GUIStyle with GUILayout? 0 Answers

GUILayout not working for me C# 1 Answer

Is it possible to make EditorGuiLayout.ObjectField(Texture2D) look like ObjectField(String,Texture2D)? 2 Answers

C sharp menu problem with bottons 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges