• 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
2
Question by Grieve_physics · Mar 09, 2014 at 12:49 AM · guimenuselectradialradial menu

Radial Menu - Mouse Selection

Hi All,

I have a basic radial menu(Pictured). I am looking to be able to select each 'button' via the direction the mouse is dragged. (The red line in the picture acts as how it could be selected.)

alt text

Situation:

While Middle Mouse button is held down radial menu appears. User will then drag mouse in the direction of the option. Upon release of MMB, the area in the same direction as the mouse will be selected.

Need help with

How do I find where the mouse location is being dragged to? Alternately is there a better option for a radial menu I have not considered?

Thankyou in advance Radial Menu from: http://forum.unity3d.com/threads/159868-Radial-Menu

radialmenu.jpg (29.6 kB)
Comment
Add comment · Show 3
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
avatar image Burnman001 · Apr 03, 2014 at 08:41 PM 0
Share

I tried to run this script and I get errors. Is this a c# script? I'm only a poor designer so please help me. :)

avatar image Burnman001 · Apr 03, 2014 at 08:47 PM 0
Share

Never mind, it was java, now it works. Oops!

avatar image Burnman001 · Apr 03, 2014 at 08:51 PM 0
Share

$$anonymous$$e again, looks good but does it do anything else? Do I need a programmer to finish this or can I add it myself, if it's simple to do.

1 Reply

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

Answer by robertbu · Mar 09, 2014 at 02:34 AM

Here is some demonstration code that uses GUI to display the buttons. Attach the script to an empty game object. Then initialize all the variables. There are two arrays, normalButtons and selectedButtons. You need to drag and drop the textures for the radial buttons into these arrays with 'selectedButtons' being the selected state for each of the buttons in the normalButtons array. 'center' is the pixel center of the whole display. 'radius' is the distance from the center to the center of each radial buttons.

 #pragma strict
 
 var center : Vector2 = Vector2(500,500); // position of center button
 var radius = 125;  // pixels radius to center of button;
 var centerButton : Texture;  
 var normalButtons : Texture[];
 var selectedButtons : Texture[];
 
 private var ringCount : int; 
 private var centerRect : Rect;
 private var ringRects : Rect[];
 private var angle : float;
 private var showButtons = false;
 private var index : int;
 
 function Start () {
     ringCount = normalButtons.Length;
     angle = 360.0 / ringCount;
     
     centerRect.x = center.x - centerButton.width  * 0.5;
     centerRect.y = center.y - centerButton.height * 0.5;
     centerRect.width = centerButton.width;
     centerRect.height = centerButton.height;
     
     ringRects = new Rect[ringCount];
     
     var w = normalButtons[0].width;
     var h = normalButtons[0].height;
     var rect = new Rect(0,0,w, h);
     
     var v = Vector2(radius,0);
     
     for (var i = 0; i < ringCount; i++) {
         rect.x = center.x + v.x - w * 0.5;
         rect.y = center.y + v.y - h * 0.5;
         ringRects[i] = rect;
         v = Quaternion.AngleAxis(angle, Vector3.forward) * v;
     }
 }
 
 function OnGUI() {
     var e = Event.current;
     
 if (e.type == EventType.MouseDown && centerRect.Contains(e.mousePosition)) {
     showButtons = true;
     index = -1;
 }    
         
     if (e.type == EventType.MouseUp) {
         if (showButtons) {
             Debug.Log("User selected #"+index);
         }
         showButtons = false;
     }
         
     if (e.type == EventType.MouseDrag) {
         var v = e.mousePosition - center;
         var a = Mathf.Atan2(v.y, v.x) * Mathf.Rad2Deg;
         a += angle / 2.0;
         if (a < 0) a = a + 360.0;
 
         index = (a / angle);
     }
     
     GUI.DrawTexture(centerRect, centerButton);
 
     if (showButtons) {
         for (var i = 0; i < normalButtons.Length; i++) {
             if (i != index) 
                 GUI.DrawTexture(ringRects[i], normalButtons[i]);
             else
                 GUI.DrawTexture(ringRects[i], selectedButtons[i]);
         }
     }
 }

 
Comment
Add comment · Show 3 · 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
avatar image Grieve_physics · Mar 09, 2014 at 05:41 AM 0
Share

Absolute champion!

Not only did you answer the question I had, you managed to provide a solution to my next mini task (Texture's, etc.)

Can't up vote just yet, but will be sure to come back when I have the 15 karma :)

avatar image robertbu · Mar 09, 2014 at 05:50 AM 0
Share

I just change line 44 by adding the 'centerRect.Contains()' so the radial buttons will only come up if the center is clicked. Glad it worked for you.

avatar image Rxanadu · Jun 30, 2014 at 02:45 AM 0
Share

The script isn't working for me. I placed it on my main camera, but it won't show up anywhere. I tried messing around with the center variable, and the best I could get was having it show up in the upper-left corner of the screen.

What am I missing?

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Centering Radial Menu 0 Answers

Game Menus WIth Keyboard Input Control 1 Answer

an UP-TO-DATE menu tutorial 1 Answer

Creating a Main Menu: 3 Answers

Bringing window to back GUI.BringWindowToBack() 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges