This script allows you to offset element slots (e.g. a DialogList or InventoryBox) with the mousewheel. To use it:
- Create a new C# script named ScrollWheelMenuList.cs, and paste in the code below
- For each element you wish to enable scrolling for, place an instance of the script in the scene. For Unity UI-linked Menus, the script can be attached to the prefab's root Canvas.
- Configure the Inspector values. You will need to ensure that the Input Name field matches an Input defined in Unity's Input Manager.
ScrollWheelMenuList.cs:
using UnityEngine;
using AC;
public class ScrollWheelMenuList : MonoBehaviour
{
[SerializeField] private string inputName = "Mouse ScrollWheel";
[SerializeField] private float inputThreshold = 0.05f;
[SerializeField] private string menuName = "Conversation";
[SerializeField] private string elementName = "DialogueList";
private bool allowScrollWheel;
private bool menuIsOn;
private bool hasCanvas;
private void OnEnable ()
{
EventManager.OnMenuTurnOn += OnMenuTurnOn;
EventManager.OnMenuTurnOff += OnMenuTurnOff;
hasCanvas = (GetComponent<Canvas> () != null);
}
private void OnDisable ()
{
EventManager.OnMenuTurnOn -= OnMenuTurnOn;
EventManager.OnMenuTurnOff -= OnMenuTurnOff;
}
private void Update ()
{
if (menuIsOn || hasCanvas)
{
float scrollInput = Input.GetAxisRaw (inputName);
if (scrollInput > inputThreshold)
{
if (allowScrollWheel)
{
PlayerMenus.GetElementWithName (menuName, elementName).Shift (AC_ShiftInventory.ShiftPrevious, 1);
allowScrollWheel = false;
}
}
else if (scrollInput < -inputThreshold)
{
if (allowScrollWheel)
{
PlayerMenus.GetElementWithName (menuName, elementName).Shift (AC_ShiftInventory.ShiftNext, 1);
allowScrollWheel = false;
}
}
else
{
allowScrollWheel = true;
}
}
}
private void OnMenuTurnOn (Menu menu, bool isInstant)
{
if (menu.title == menuName)
{
menuIsOn = true;
}
}
private void OnMenuTurnOff (Menu menu, bool isInstant)
{
if (menu.title == menuName)
{
menuIsOn = false;
}
}
}