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.)

23326-radialmenu.jpg

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

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 *= 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_, normalButtons*);
else*
GUI.DrawTexture(ringRects, selectedButtons*);
}
}
}*_