This script allows you to display a list of Hotspot interactions that can be scrolled through if more are available than can be shown at once.
To use it, do the following:
- Set your Interaction method to Choose Hotspot Then Interaction
- Create a new Unity UI Canvas with a series of Buttons (e.g. 3) that each represent a clickable Interaction, as well as two separate Buttons for scrolling up and down the list.
- Attach the script below as a component to the root of the Canvas, and assign the exposed fields in its Inspector.
- Create an OnClick event for the "Scroll Up" Button, that runs the script's OnClickInteractionUp method.
- Create an OnClick event for the "Scroll Down" Button, that runs the script's OnClickInteractionDown method.
- For each Interaction Button, create an OnClick event that runs the script's OnClickInteractionMethod. Each should pass an integer according to its order in the list (first passes 0, second passes 1, etc)
- Create a new Menu in the Menu Manager and set its Appear type to On Interaction. No Elements need to be defined.
InteractionListUI.cs:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using AC; public class InteractionListUI : MonoBehaviour { #region Variables private List<AC.Button> interactions; private int interactionOffset; private Menu menu; [SerializeField] private Canvas canvas; [SerializeField] private UnityEngine.UI.Button[] interactionButtons; [SerializeField] private UnityEngine.UI.Button interactionUpButton; [SerializeField] private UnityEngine.UI.Button interactionDownButton; #endregion #region UnityStandards private void OnEnable () { if (Menu == null) { return; } if (interactions == null) { interactions = new List<AC.Button>(); } interactions.Clear (); foreach (AC.Button useButton in Menu.GetTargetHotspot ().useButtons) { if (!useButton.isDisabled) { interactions.Add (useButton); } } interactionOffset = 0; RebuildInteractionLabels (); } #endregion #region PublicFunctions public void OnClickInteraction (int index) { int iconID = interactions [index + interactionOffset].iconID; Menu.GetTargetHotspot ().RunUseInteraction (iconID); } public void OnClickInteractionUp () { interactionOffset --; RebuildInteractionLabels (); } public void OnClickInteractionDown () { interactionOffset ++; RebuildInteractionLabels (); } #endregion #region PrivateFunctions private void RebuildInteractionLabels () { if (interactions.Count < 3) { interactionOffset = 0; } else if (interactionOffset < 0) { interactionOffset = 0; } else if (interactionOffset > interactions.Count - 3) { interactionOffset = interactions.Count - 3; } for (int i=0; i<interactionButtons.Length; i++) { int index = i + interactionOffset; if (index < interactions.Count) { interactionButtons[i].GetComponentInChildren <Text>().text = KickStarter.cursorManager.GetLabelFromID (interactions [index].iconID, Options.GetLanguage ()); interactionButtons[i].gameObject.SetActive (true); } else { interactionButtons[i].gameObject.SetActive (false); } } interactionUpButton.gameObject.SetActive (interactionOffset > 0); interactionDownButton.gameObject.SetActive (interactionOffset < interactions.Count - 3); if (interactions.Count > 0) { KickStarter.playerMenus.SelectUIElement (interactionButtons[0].gameObject); } } #endregion #region GetSet private Menu Menu { get { if (menu == null) { menu = KickStarter.playerMenus.GetMenuWithCanvas (canvas); } return menu; } } #endregion }