Hi,
I was wondering how can one make a pie-shaped GUI such as the one below?
The obvious method would be to present the image using GUI.DrawTexture()
, and then do geometry calculations whenever a mouse is clicked within the image. But that seems like a hard and non-visual way, so I was wondering if someone has any better ideas.
One idea I had was that when I create the texture I would be able to “mark” the different buttons in the texture and then have unity recognize which section was clicked… But I have no idea on how to do this.
Any help or other ideas would be appreciated.
I would attempt to solve this problem by creating separate pictures of all the pressable buttons so that when overlaid, they line up. Then in unity I would store all these pictures in an array or list and on each mouse click inside their Rect I would GetPixel() of all the Texture2Ds in my list and check which one had a non-transparent alpha at that point and perform the logic associated with that button/Texture2D.
untested code in C#:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NewBehaviourScript : MonoBehaviour {
Rect s = new Rect(0,0, Screen.width, Screen.height);
List<Texture2D> buttons = new List<Texture2D>();
// Update is called once per frame
void OnGUI () {
foreach(Texture2D element in buttons)
GUI.Label(s,element);
if(Input.GetMouseButtonDown(0) && s.Contains(Input.mousePosition)){
Vector3 mousePosition = Input.mousePosition;
foreach(Texture2D element in buttons)
if(element.GetPixel(mousePosition.x,mousePosition.y).a > 0){
/**
* Perform button logic here, perhaps a huge switch-case or a fancy Command-design pattern
**/
}
}
}
}
If memory serves, Input mousePosition (0,0) is the bottom left, while Screen (0,0) is top left, so you might have to do some translating…
Good luck!
There’s an implementation in the asset store (Pie menus) for $10, or you could port the algorithms from the jQuery Radmenu Plugin, which is open source…