GUI Pie

I am trying to make buttons that are rotated around a point on the screen, using a “GUI skin”.

The problems I am facing:

  • The buttons must only be clickable if the cursor is over and opaque part of the texture
  • You must not be able to select multiple buttons at once, since Unity selects Multiple buttons if they are overlapping.

Here are some images describing what happens:

This is what it looks like before you put your cursor over a button

This is what it looks like when the cursor is over the 1, 6 and 5 buttons

Here is the code for the GUI:

using UnityEngine;
using System.Collections;

public class GUIUtilities : MonoBehaviour {
    private float rotAngle;
    private Vector2 pivotPoint;
	public string currentMenu;
	
	public int buttonCount = 8;
	public int buttonSeperation = 5;
	public string[] buttonNames;
	public GUISkin guiSkin;
	
    void OnGUI() {
		
		GUI.skin = guiSkin;
		rotAngle = 360 / buttonCount;
        pivotPoint = new Vector2(Screen.width / 2, Screen.height / 2);
		
		for (int i = 0; i < buttonNames.Length; i++) {//middle cannot be "<=" has to be "<" , otherwise error
			GUIUtility.RotateAroundPivot(rotAngle, pivotPoint);//already adds on the rotation each i
	        if (GUI.Button(new Rect(Screen.width / 2 - (GUI.skin.button.fixedWidth/2), Screen.height / 2 - (GUI.skin.button.fixedHeight - buttonSeperation), GUI.skin.button.fixedWidth, GUI.skin.button.fixedHeight), buttonNames[i].ToString(), "button"))
				currentMenu = buttonNames[i].ToString();
		}
    }
}

This is the image I used for the Example:

1427553–75556–$Untitled-1.psd (178 KB)

Unfortunately, when having non-rectangle-shaped elements, you cannot rely on GUI.Button method.

You’ll have to do your own calculations.

  1. First, you should calculate if the mouse is over any of these “buttons”. You do that by examining the mouse position in relation to your pie slices (Event.mousePosition).

  2. Instead of the GUI.Button, you should use the GUIStyle.Draw for rendering any of the slices in their appropriate state.

And yes, you should have 1 style per slice defined.