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);
}
}
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
Store the correct menu name in a Global String variable, and enter the following at the top of the AnimateCombineCo function:
Replacing "Inventory name" with the name of your variable.
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?
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?