Adventure Creator Wikia
Advertisement

This script animates the return of Inventoryi tems to their original slot in the Inventory menu after de-selected them.

To use it:

  1. Using Unity UI for your Inventory menu, set the Canvas' Render Mode to Screen Space - Camera.
  2. Set the Plane Distance to 1. For this field to show, place the UI prefab in the scene, and assign the MainCamera. Apply changes and remove from the scene afterwards.
  3. Create a new C# script named ItemSlotFollowCursor and copy/paste the code below
  4. For each item slot in the Inventory UI prefab, move the Image component to a child object, and attach the new Item Slot Follow Cursor component to it - assigning the Canvas and its slot index number (start from zero) in its Inspector.

ItemSlotFollowCursor.cs:

using UnityEngine;
using AC;

public class ItemSlotFollowCursor : MonoBehaviour
{

    public Canvas canvas;
    public int slotIndex;
    private GameObject buttonOb;
    private RectTransform rectTransform;
    private const float ReturnSpeed = 5f;
    private InvInstance recordedInvInstance;

    void Awake ()
    {
        rectTransform = GetComponent<RectTransform> ();
        buttonOb = transform.parent.gameObject;
    }

    void Update ()
    {
        MenuInventoryBox inventoryBox = (MenuInventoryBox) KickStarter.playerMenus.GetMenuWithCanvas (canvas).GetElementWithGameObject (buttonOb);
        InvInstance invInstance = inventoryBox.GetInstance (slotIndex);
        if (!InvInstance.IsValid (invInstance)) return;

        bool isSelected = invInstance == KickStarter.runtimeInventory.SelectedInstance;
        if (isSelected)
        {
            rectTransform.localPosition = new Vector2 (0f, -10f);
            RectTransformUtility.ScreenPointToLocalPointInRectangle (canvas.transform as RectTransform, Input.mousePosition, canvas.worldCamera, out Vector2 movePos);
            rectTransform.position = canvas.transform.TransformPoint (movePos);
            recordedInvInstance = invInstance;
        }
        else
        {
            if (invInstance == recordedInvInstance)
            {
                rectTransform.localPosition = Vector2.Lerp (rectTransform.localPosition, Vector2.zero, Time.deltaTime * ReturnSpeed);
            }
            else
            {
                rectTransform.localPosition = Vector2.zero;
            }
        }
    }

}
Advertisement