FANDOM


This script allow for inventory shift left/right buttons to work while the mouse is drag-and-dropping inventory items.  This only works with Unity UI-based Inventory Menus.  To use it, attach the script file to your UI Button(s) and update the prefab.


/*
 *
 *    Adventure Creator
 *    by Chris Burton, 2013-2017
 *    
 *    "DragDropInventoryButton.cs"
 * 
 *    This script can let Unity UI Buttons be invoked when the cursor is held over it, with a drag-and-drop inventory item selected, after a set time.
 *    It is useful for shift left/right buttons in your Unity UI, so that you can scroll inventory items when one is selected.
 *    'Drag and drop inventory?' must be checked in the Settings Manager.
 *    To use it, attach it to your Unity UI Button.
 * 
 */

using System;
using UnityEngine;
using UnityEngine.EventSystems;
using AC;

public class DragDropInventoryButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{

    [SerializeField] private UnityEngine.UI.Button uiButton;
    [SerializeField] private float totalHoldTime = 0.4f;

    private bool isOver = false;
    private float currentHold;


    private void Awake ()
    {
        if (uiButton == null)
        {
            uiButton = GetComponent <UnityEngine.UI.Button>();
        }
    }


    public void OnPointerEnter (PointerEventData eventData)
    {
        if (!isOver && uiButton != null && AC.KickStarter.playerInput.GetDragState () == DragState.Inventory)
        {
            StartWait ();
        }
    }


    public void OnPointerExit (PointerEventData eventData)
    {
        isOver = false;
    }


    private void Update ()
    {
        if (isOver)
        {
            if (KickStarter.playerInput.GetMouseState () != MouseState.HeldDown)
            {
                isOver = false;
            }

            if (currentHold > 0f)
            {
                currentHold -= Time.deltaTime;

                if (currentHold <= 0f)
                {
                    uiButton.onClick.Invoke ();
                    StartWait ();
                }
            }
        }
    }


    private void StartWait ()
    {
        isOver = true;
        currentHold = totalHoldTime;
    }

}

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.