Forum rules - please read before posting.

Adapt AnimateCursor script

Hi, how would I adapt this script of my menu name has changed from 'Inventory' to either 'InventoryFullscreen', 'InventoryBottomBar' and 'InventoryTopbar' (as the player can choose which inventory they want'

using UnityEngine;
using System.Collections;
using AC;

public class AnimateCursor : MonoBehaviour
{

    public Vector2 offset;
    public RectTransform cursorRectTransform;
    public int itemToMoveFrom;
    public int itemToMoveTo;
    public Canvas canvas;
    public float moveDuration = 3f;

    public string menuName = "Inventory";
    public string inventoryElementName = "InventoryBox";


    public void AnimateCombine ()
    {
        StopAllCoroutines ();
        StartCoroutine (AnimateCombineCo ());
    }


    private IEnumerator AnimateCombineCo ()
    {
        MenuInventoryBox inventoryBox = PlayerMenus.GetElementWithName (menuName, inventoryElementName) as MenuInventoryBox;

        int fromSlot = inventoryBox.GetItemSlot (itemToMoveFrom);
        RectTransform fromRectTransform = inventoryBox.uiSlots[fromSlot].GetRectTransform ();

        Sprite originalSprite = inventoryBox.uiSlots[fromSlot].uiButton.image.sprite;
        inventoryBox.uiSlots[fromSlot].uiButton.image.sprite = null;

        int toSlot = inventoryBox.GetItemSlot (itemToMoveTo);
        RectTransform toRectTransform = inventoryBox.uiSlots[toSlot].GetRectTransform ();

        Vector2 fromPosition = fromRectTransform.position / canvas.transform.localScale.x;
        Vector2 toPosition = toRectTransform.position / canvas.transform.localScale.x;

        Vector2 parentOffset = canvas.GetComponent<RectTransform>().sizeDelta / 2f;
        fromPosition -= parentOffset;
        toPosition -= parentOffset;

        fromPosition += offset;
        toPosition += offset;

        Vector2 originalPosition = cursorRectTransform.anchoredPosition;

        float timer = moveDuration;
        while (timer > 0f)
        {
            float p = 1f - (timer / moveDuration);
            cursorRectTransform.anchoredPosition = Vector2.Lerp (fromPosition, toPosition, p);

            timer -= Time.deltaTime;
            yield return null;
        }

        inventoryBox.uiSlots[fromSlot].uiButton.image.sprite = originalSprite;
        cursorRectTransform.anchoredPosition = originalPosition;
    }


    private Rect RectTransformToScreenSpace (RectTransform transform)
    {
        Vector2 size = Vector2.Scale(transform.rect.size, transform.lossyScale);
        return new Rect((Vector2)transform.position - (size * 0.5f), size);
    }

}

Comments

  • Store the correct menu name in a Global String variable, and enter the following at the top of the AnimateCombineCo function:

    inventoryElementName = GlobalVariables.GetVariable ("Inventory name").TextValue;
    

    Replacing "Inventory name" with the name of your variable.

  • Could I instead use 0,1,2 in an integer variable as I have three different inventorys
  • edited February 14

    if I used your addition above, what would I add it in Menu Name in the Inspector script field?

    To elaborate further, I have three menus, each with an inventory UI canvasm with names 'InventoryFullscreen', InventoryScrollBottom' and 'InventoryScrollTop'. The player chooses which one they want via a button choice, each button turns on an integer in variable 'InvenoryChoice' - 0,1 or 2. I need the above script to recognise which one is chosen.

    i think the easiest option is to have 3 different AnimateCursor script that is attached to each inventory. What would I change in the above if the Inventory is called 'InventoryFullscreen' eg?

  • i think the easiest option is to have 3 different AnimateCursor script that is attached to each inventory. What would I change in the above if the Inventory is called 'InventoryFullscreen' eg?

    That'd be the "Menu Name" field in its Inspector.

  • I am not seeing the object animate now. Am getting this error, could this be the reason?

    NullReferenceException: Object reference not set to an instance of an object
    AnimateCursor.AnimateCombine () (at Assets/Sleepytime Village/Scripts/AnimateCursor.cs:21)
    UnityEngine.GameObject:SendMessage(String, SendMessageOptions)
    AC.ActionSendMessage:Run() (at Assets/AdventureCreator/Scripts/Actions/ActionSendMessage.cs:96)
    AC.<RunAction>d__46:MoveNext() (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:467)
    UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
    AC.ActionList:ProcessAction(Int32) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:408)
    AC.ActionList:ProcessActionEnd(ActionEnd, Int32, Boolean) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:616)
    AC.ActionList:EndAction(Action) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:575)
    AC.<RunAction>d__46:MoveNext() (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:539)
    UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
    AC.ActionList:ProcessAction(Int32) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:408)
    AC.ActionList:ProcessActionEnd(ActionEnd, Int32, Boolean) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:616)
    AC.ActionList:EndAction(Action) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:575)
    AC.<RunAction>d__46:MoveNext() (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:539)
    UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
    AC.ActionList:ProcessAction(Int32) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:408)
    AC.ActionList:ProcessActionEnd(ActionEnd, Int32, Boolean) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:616)
    AC.ActionList:EndActionParallel(Action) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:701)
    AC.<RunAction>d__46:MoveNext() (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:535)
    UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
    AC.ActionList:ProcessAction(Int32) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:408)
    AC.ActionList:ProcessActionEnd(ActionEnd, Int32, Boolean) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:616)
    AC.ActionList:EndAction(Action) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:575)
    AC.<RunAction>d__46:MoveNext() (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:539)
    UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) (at /Users/bokken/build/output/unity/unity/Runtime/Export/Scripting/Coroutines.cs:17)
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.