/// <summary> /// Creates a "selector" -- left/right arrow buttons with the currently-selected option displayed in between. /// Returns the new selected object based on player input. /// </summary> public static int Selector(GUIContent leftArrow, GUIContent rightArrow, GUIContent[] options, int currentOption, SelectorLayout layout, bool wrapAroundBeginning, bool wrapAroundEnd, bool scaleToScreen, GUIStyle arrowStyle = null, GUIStyle selectionStyle = null) { if (scaleToScreen) { layout = new SelectorLayout(layout); layout.CenterLeftPos = new Vector2(layout.CenterLeftPos.x * Screen.width, layout.CenterLeftPos.y * Screen.height); layout.SpaceBetweenElements *= Screen.width; layout.ArrowWidth *= Screen.width; layout.ArrowHeight *= Screen.height; layout.SelectedWidth *= Screen.width; layout.SelectedHeight *= Screen.height; } //The button/label areas. Rect leftArea = new Rect(layout.CenterLeftPos.x, layout.CenterLeftPos.y - (0.5f * layout.ArrowHeight), layout.ArrowWidth, layout.ArrowHeight); Rect centerArea = new Rect(leftArea.xMax + layout.SpaceBetweenElements, layout.CenterLeftPos.y - (0.5f * layout.SelectedHeight), layout.SelectedWidth, layout.SelectedHeight); Rect rightArea = new Rect(centerArea.xMax + layout.SpaceBetweenElements, leftArea.yMin, layout.ArrowWidth, layout.ArrowHeight); //Draw the current option. if (selectionStyle == null) { GUI.Box(centerArea, options[currentOption]); } else { GUI.Box(centerArea, options[currentOption], selectionStyle); } //Left arrow button. if ((arrowStyle == null && GUI.Button(leftArea, leftArrow)) || (arrowStyle != null && GUI.Button(leftArea, leftArrow, arrowStyle))) { currentOption -= 1; if (currentOption < 0) { if (wrapAroundBeginning) { currentOption += options.Length; } else { currentOption = 0; } } } //Right arrow button. if ((arrowStyle == null && GUI.Button(rightArea, rightArrow)) || (arrowStyle != null && GUI.Button(rightArea, rightArrow, arrowStyle))) { currentOption += 1; if (currentOption >= options.Length) { if (wrapAroundEnd) { currentOption -= options.Length; } else { currentOption = options.Length - 1; } } } return(currentOption); }
public SelectorLayout(SelectorLayout copy) : this(copy.CenterLeftPos, copy.SpaceBetweenElements, copy.ArrowWidth, copy.ArrowHeight, copy.SelectedWidth, copy.SelectedHeight) { }