Hi there!
I have a circular menu (like a context menu to choose weapons or spells from) and I managed to make the menu itself work quite fine.
However, the way it is currently, its always visible as soon as I run the scene. Obviously I want to be able to toggle it when I press a button on the keyboard (like R or I or something) and I’m kinda stuck on how I should go about it. In other projects I used the “makeActive” way, but I don’t quite know where to implement that.
I’m relatively new to coding (only been doing it for the past 2 years or so and I started with JavaScript and only switched to C# a few months ago) so any help will be appreciated.
I enclosed my code for the circular menu.
Thanks in Advance
Rhylen
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CircMenu : MonoBehaviour
{
public List<MenuButton> buttons = new List<MenuButton>();
private Vector2 MousePosition;
private Vector2 fromVector2M = new Vector2(0.5f, 1.0f);
private Vector2 centerCircle = new Vector2(0.5f, 0.5f);
private Vector2 toVector2M;
public int menuItems;
public int CurMenuItem;
private int OldMenuItem;
// Use this for initialization
void Start()
{
menuItems = buttons.Count;
foreach(MenuButton button in buttons)
{
button.sceneImage.color = button.NormalColor;
}
CurMenuItem = 0;
OldMenuItem = 0;
}
// Update is called once per frame
void Update()
{
GetCurrentMenuItem();
if (Input.GetButtonDown("Fire1"))
ButtonAction();
}
public void GetCurrentMenuItem()
{
MousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
toVector2M = new Vector2(MousePosition.x / Screen.width, MousePosition.y / Screen.height);
float angle = (Mathf.Atan2(fromVector2M.y - centerCircle.y, fromVector2M.x - centerCircle.x) - Mathf.Atan2(toVector2M.y - centerCircle.y, toVector2M.x - centerCircle.x)) * Mathf.Rad2Deg;
if (angle < 0)
angle += 360;
CurMenuItem = (int) (angle / (360/menuItems));
if (CurMenuItem != OldMenuItem)
{
buttons[OldMenuItem].sceneImage.color = buttons[OldMenuItem].NormalColor;
OldMenuItem = CurMenuItem;
buttons[CurMenuItem].sceneImage.color = buttons[CurMenuItem].HighlightedColor;
}
}
public void ButtonAction()
{
buttons[CurMenuItem].sceneImage.color = buttons[CurMenuItem].PressedColor;
if (CurMenuItem == 0)
print("You have pressed the first button");
}
}
[System.Serializable]
public class MenuButton
{
public string name;
public Image sceneImage;
public Color NormalColor = Color.white;
public Color HighlightedColor = Color.grey;
public Color PressedColor = Color.blue;
}