// This function takes in a RadialMenu present in the scene and reads its properties, // then uses these properties to populate the window with that menu's current settings private void DisplayMenuToUpdate(RadialMenu menu) { var menuButtons = menu.GetComponentsInChildren <RadialButton>(); var menuCursor = menu.GetComponentInChildren <MenuCursor>(); var menuToggle = menu.GetComponent <MenuToggle>(); _menuName = menu.name; _buttonStyle = menu.buttonStyle; _numberOfButtons = menuButtons.Length; _labelDisplay = menuCursor.labelDisplayOption; // If all button colors match for (var i = 0; i < menuButtons.Length; i++) { var rendererOne = menuButtons[i].GetComponent <Renderer>(); var currentMatColor = rendererOne.sharedMaterial.color; if (i != menuButtons.Length - 1) { var rendererTwo = menuButtons[i + 1].GetComponent <Renderer>(); var nextMatColor = rendererTwo.sharedMaterial.color; if (currentMatColor != nextMatColor) { _buttonsMatch = false; break; } } _buttonsMatch = true; } _sharedButtonColor = menuButtons[0].GetComponent <Renderer>().sharedMaterial.color; _sharedHighlightColor = menuCursor.highlightMat.color; _selectionButton = menuCursor.selectionButton; _menuToggle = menuToggle.toggle; _menuToggleButton = menuToggle.toggleButton; _handAttachPoint = menu.GetComponent <AttachToAnchor>().attachPoint; _playSoundOnClick = menuCursor.playSound; if (_playSoundOnClick) { _onClickSound = menuCursor.clickAudio; } _hapticHand = menuCursor.hapticHandOption; if (_hapticHand != EHapticHand.NoHaptics) { _hapticIntensity = menuCursor.hapticIntensityOption; } _buttonMats = new Material[menuButtons.Length]; for (var i = 0; i < menuButtons.Length; i++) { _buttonLabels[i] = menuButtons[i].name; _buttonIcons[i] = menuButtons[i].GetComponentInChildren <Image>().sprite; _buttonMats[i] = menuButtons[i].GetComponent <Renderer>().sharedMaterial; if (!_buttonsMatch) { _buttonColors[i] = _buttonMats[i].color; } } }
public void OnGUI() { this.maxSize = new Vector2(500f, 1000f); // Find all Radial Menus currently in scene var currentMenus = FindObjectsOfType <RadialMenu>(); foreach (var menu in currentMenus) { if (_menusInScene != null && !_menusInScene.Contains(menu)) { _menusInScene.Add(menu); } } if (currentMenus.Length > 0) { if (_menusInScene != null) { foreach (var menu in _menusInScene.ToList().Where(menu => !currentMenus.Contains(menu))) { _menusInScene.Remove(menu); } } } // Styles _headerStyle = new GUIStyle(GUI.skin.label) { fontStyle = FontStyle.Bold, clipping = TextClipping.Overflow }; _foldoutHeaderStyle = new GUIStyle(EditorStyles.foldout) { fontStyle = FontStyle.Bold }; _layoutOptions = new[] { GUILayout.MinWidth(300), GUILayout.MaxWidth(500) }; EditorGUILayout.Space(); EditorGUILayout.LabelField("Radial Menu Options", _headerStyle); // Check if there are current menus in scene. // If there are, display Build/Update option if (currentMenus.Length > 0) { _buildOption = (EBuildOptions)GUILayout.SelectionGrid((int)_buildOption, new[] { EBuildOptions.BuildNewMenu.ToString(), EBuildOptions.UpdateExistingMenu.ToString() }, 2, _layoutOptions); EditorGUILayout.Space(); } else { _buildOption = EBuildOptions.BuildNewMenu; _menuUpdate = null; _menusInScene = new List <RadialMenu>(); } if (_buildOption == EBuildOptions.UpdateExistingMenu) { if (_menusInScene != null) { _menuUpdateOptions = new string[_menusInScene.Count]; for (var i = 0; i < _menuUpdateOptions.Length; i++) { _menuUpdateOptions[i] = _menusInScene[i].gameObject.name; } } _menuUpdate = EditorGUILayout.ObjectField("Menu to Update", _menuUpdate, typeof(RadialMenu), true, _layoutOptions); if (_menuUpdate != null) { _menuUpdateOptionsIndex = ArrayUtility.IndexOf(_menuUpdateOptions, _menuUpdate.name); } if (_menuUpdate != null && GUILayout.Button("Load Menu", _layoutOptions)) { DisplayMenuToUpdate((RadialMenu)_menuUpdate); } else if (_menuUpdate == null && GUILayout.Button("Load Menu", _layoutOptions)) { Debug.LogError("VRMM: Please Select a Menu to Update."); } } GUILine(); //Loading content needed to make menu _buttonPrefabs = Resources.LoadAll <GameObject>("ButtonPrefabs/" + _buttonStyle); _radialMenuPrefab = Resources.Load <GameObject>("MenuPrefabs/RadialMenu"); _defaultClickSound = Resources.Load <AudioClip>("Sound/DefaultButtonPress"); _scrollPosition = GUILayout.BeginScrollView(_scrollPosition); // General Button Options EditorGUILayout.LabelField("General Button Options", _headerStyle); if (_buildOption == EBuildOptions.BuildNewMenu) { _menuName = EditorGUILayout.TextField("Menu Name", _menuName, _layoutOptions); } _buttonStyle = (EButtonStyles)EditorGUILayout.EnumPopup("Button Style", _buttonStyle, _layoutOptions); _numberOfButtons = EditorGUILayout.IntSlider("Number of Buttons", _numberOfButtons, 2, 8, _layoutOptions); GUILine(); // Label Options EditorGUILayout.LabelField("Label Options", _headerStyle); _labelDisplay = (ELabelDisplay)EditorGUILayout.EnumPopup("Button Label Mode", _labelDisplay, _layoutOptions); _labelFont = EditorGUILayout.ObjectField("Label Font", _labelFont, typeof(Font), false, _layoutOptions); GUILine(); //Color Options EditorGUILayout.LabelField("Color Options", _headerStyle); _buttonsMatch = EditorGUILayout.Toggle("All buttons same color", _buttonsMatch); if (_buttonsMatch) { _sharedButtonColor = EditorGUILayout.ColorField("Button Color", _sharedButtonColor, _layoutOptions); } _sharedHighlightColor = EditorGUILayout.ColorField("Button Hover Color", _sharedHighlightColor, _layoutOptions); // _buttonHighlightMat.color = _sharedHighlightColor; GUILine(); //Control Options EditorGUILayout.LabelField("Control Options", _headerStyle); _selectionButton = (ESelectionButton)EditorGUILayout.EnumPopup("Confirm Selection Button", _selectionButton, _layoutOptions); _menuToggle = EditorGUILayout.Toggle("Menu Visibility Toggle", _menuToggle); if (_menuToggle) { _menuToggleButton = (EToggleButton)EditorGUILayout.EnumPopup("Menu Toggle Button", _menuToggleButton, _layoutOptions); } GUILine(); //Misc Options EditorGUILayout.LabelField("Misc Options", _headerStyle); _handAttachPoint = EditorGUILayout.ObjectField("Menu Hand Attach Point", _handAttachPoint, typeof(GameObject), true, _layoutOptions); _playSoundOnClick = EditorGUILayout.Toggle("Play Sound on Click", _playSoundOnClick); if (_playSoundOnClick) { _onClickSound = EditorGUILayout.ObjectField("Sound on Click", _onClickSound, typeof(AudioClip), true, _layoutOptions); } _onClickSound = _defaultClickSound; _hapticHand = (EHapticHand)EditorGUILayout.EnumPopup("Haptic Controller", _hapticHand, _layoutOptions); if (_hapticHand != EHapticHand.NoHaptics) { _hapticIntensity = (EHapticIntensity)EditorGUILayout.EnumPopup("Haptic Intensity", _hapticIntensity, _layoutOptions); } GUILine(); //Individual Button Options _showButtonOptions = EditorGUILayout.Foldout(_showButtonOptions, "Individual Button Options", true, _foldoutHeaderStyle); if (_showButtonOptions) { for (var i = 0; i < _numberOfButtons; i++) { if (i == 0) { EditorGUILayout.Space(); } EditorGUILayout.LabelField("Button " + (i + 1), _headerStyle); _buttonLabels[i] = EditorGUILayout.TextField("Button Name", _buttonLabels[i], _layoutOptions); if (!_buttonsMatch) { _buttonColors[i] = EditorGUILayout.ColorField("Button Color", _buttonColors[i], _layoutOptions); } _buttonIcons[i] = EditorGUILayout.ObjectField("Button Icon", _buttonIcons[i], typeof(Sprite), false, _layoutOptions); GUILine(); } } GUILayout.EndScrollView(); //Create menu on press of 'Make Menu' button if ( _buttonStyle != EButtonStyles.ChooseStyle && _selectionButton.ToString() != _menuToggleButton.ToString() && _buildOption == EBuildOptions.BuildNewMenu && GUILayout.Button("Make Menu", _layoutOptions)) { try{ if (ValidateBuild(_menuName, _menusInScene, out _buttonMats, _numberOfButtons)) { MenuMaker.MakeMenu( _menuName, _radialMenuPrefab, _buttonHighlightMat, _buttonPrefabs[_numberOfButtons - 2], _buttonStyle, _buttonMats, _numberOfButtons, _labelDisplay, (Font)_labelFont, _hapticHand, _hapticIntensity, _selectionButton, _handAttachPoint, _buttonsMatch, _sharedButtonColor, _buttonColors, _buttonLabels, _buttonIcons, _playSoundOnClick, _onClickSound, _menuToggle, _menuToggleButton ); } } catch (Exception e) { Debug.LogError("VRMM: " + e.Message); } } else if ( _buildOption == EBuildOptions.UpdateExistingMenu && _selectionButton.ToString() != _menuToggleButton.ToString() && GUILayout.Button("Update Menu", _layoutOptions)) { try{ if (ValidateUpdate(out _buttonMats, _numberOfButtons, _menusInScene, _menuUpdateOptionsIndex)) { MenuMaker.UpdateMenu( _menusInScene[_menuUpdateOptionsIndex], _sharedHighlightColor, _buttonPrefabs[_numberOfButtons - 2], _buttonStyle, _buttonMats, _numberOfButtons, _labelDisplay, (Font)_labelFont, _hapticHand, _hapticIntensity, _selectionButton, _handAttachPoint, _buttonsMatch, _sharedButtonColor, _buttonColors, _buttonLabels, _buttonIcons, _playSoundOnClick, _onClickSound, _menuToggle, _menuToggleButton ); } } catch (Exception e) { Debug.LogError("VRMM: " + e.Message); } } else if ( _buttonStyle == EButtonStyles.ChooseStyle && _selectionButton.ToString() != _menuToggleButton.ToString() && _buildOption != EBuildOptions.UpdateExistingMenu && GUILayout.Button("Make Menu", _layoutOptions)) { Debug.LogWarning("VRMM: Please select a button style to create your menu!"); } else if ( _menuToggle && _selectionButton.ToString() == _menuToggleButton.ToString() && _buildOption != EBuildOptions.UpdateExistingMenu && GUILayout.Button("Make Menu", _layoutOptions)) { Debug.LogWarning("VRMM: Please choose different buttons for Confirm Selection and Menu Toggle"); } EditorGUILayout.Space(); }
public static void MakeMenu( string menuName, GameObject radialMenuPrefab, Material buttonHighlightMat, GameObject buttonPrefab, EButtonStyles buttonStyle, Material[] buttonMats, int numberOfButtons, ELabelDisplay labelDisplay, Font labelFont, EHapticHand hapticHand, EHapticIntensity hapticIntensity, ESelectionButton selectionButton, Object handAttachPoint, bool buttonsMatch, Color sharedButtonColor, Color[] buttonColors, string[] buttonLabels, Object[] buttonIcons, bool playSoundOnClick, Object onClickSound, bool toggleMenu, EToggleButton menuToggleButton ) { //Create new menu var radialMenuClone = Instantiate(radialMenuPrefab); radialMenuClone.name = menuName == "" ? "RadialMenu" : menuName; radialMenuClone.GetComponent <RadialMenu>().buttonStyle = buttonStyle; if (toggleMenu) { var menuToggle = radialMenuClone.GetComponent <MenuToggle>(); menuToggle.toggle = true; menuToggle.toggleButton = menuToggleButton; } //Add Unity Event list of proper length var buttonEvents = radialMenuClone.GetComponent <ButtonEvents>(); for (var i = 0; i < numberOfButtons; i++) { buttonEvents.eventList.Add(new UnityEvent()); } //Find Menu Cursor in instantiated menu and pass on needed variables var cursor = radialMenuClone.GetComponentInChildren <MenuCursor>(); cursor.labelDisplayOption = labelDisplay; //Check if play haptics cursor.playHaptics = hapticHand != EHapticHand.NoHaptics; cursor.hapticHandOption = hapticHand; cursor.hapticIntensityOption = hapticIntensity; cursor.highlightMat = buttonHighlightMat; cursor.selectionButton = selectionButton; //Attach menu to specified attach point var anchorAttach = radialMenuClone.GetComponent <AttachToAnchor>(); anchorAttach.attachPoint = handAttachPoint as GameObject; //Create buttons for menu for (var i = 0; i < numberOfButtons; i++) { //Create each button as a child of the button container using specific button model var buttonClone = Instantiate(buttonPrefab, radialMenuClone.GetComponentInChildren <RadialButtonContainer>().transform); var renderer = buttonClone.GetComponent <Renderer>(); renderer.material = buttonMats[i]; renderer.sharedMaterial.color = buttonsMatch ? sharedButtonColor : buttonColors[i]; //Handle button labels var buttonText = buttonClone.GetComponentInChildren <Text>(true); buttonText.text = buttonLabels[i]; if (labelFont == null) { var defaultFont = Resources.Load <Font>("Fonts/Rubik-Regular"); labelFont = defaultFont; } buttonText.font = labelFont; if (labelDisplay == ELabelDisplay.AlwaysShow) { buttonText.gameObject.SetActive(true); } //Handle button icons var buttonIcon = buttonClone.GetComponentInChildren <Image>(); if (buttonIcons[i] == null) { buttonIcon.color = Color.clear; } else { buttonIcon.sprite = buttonIcons[i] as Sprite; } //Rotate each button to proper spot around center and name GameObject appropriately buttonClone.transform.rotation = Quaternion.Euler(new Vector3(0, ((360 / numberOfButtons) * i) - ((360 / numberOfButtons) / 2) - 90, 0)); buttonClone.transform.position = Vector3.zero; buttonClone.name = buttonLabels[i] == null ? "Button " + (i + 1) : buttonLabels[i]; //Check button position and flip label/icon if on bottom half of menu if (buttonText.transform.position.z > 0 || (buttonText.transform.position.z == 0 && buttonText.transform.position.x > 0)) { buttonText.transform.Rotate(new Vector3(180, 180, 0)); } if (buttonIcon.transform.position.z > 0 || (buttonText.transform.position.z == 0 && buttonText.transform.position.x > 0)) { buttonIcon.transform.Rotate(new Vector3(180, 180, 0)); } //Make sure Audio exists or is disabled var audioSource = buttonClone.GetComponent <AudioSource>(); if (playSoundOnClick) { cursor.playSound = true; if (onClickSound != null) { audioSource.clip = onClickSound as AudioClip; } else { playSoundOnClick = false; } audioSource.playOnAwake = false; } else { audioSource.enabled = false; } } Debug.Log("Menu built. Find RadialMenu object in scene to add button events!"); }