Forum rules - please read before posting.

move the sprite to menu

Hello. I have a question ... I need to move the sprite from the scene to some menu option (Unity menu system). Can this be done?

Comments

  • In what context? Do you mean you want to move something fixed from the scene file to your interface in general, or that you want the user to be able to move an item in the scene into your inventrory menu?

    Unity UI can render sprites with its own Image component, which doesn't strictly need to rely on AC. The Image can be part of a UI Button that is linked to AC's Menu Manager as a Button element, if you want to make it clickable.

    If you're trying to have an item be moved to your inventory menu, you can define that item in the Inventory Manager and add it to your Inventory menu using the "Inventory: Add or remove" Action.

  • I put it a little wrong. I need to move the object not INSIDE the interface, but to some object of the interface. Do it not with your hands, but automatically.

    This is a task of such a plan: after changing the variable, the player receives a coin. This coin is an animated sprite, it can be anywhere on the stage. But after the animation is complete, it (at this moment you can replace it with a simple sprite without animation) flies into the indicator, which is part of the menu.

    I cannot do this inside the menu itself, as there are no such complicated options out of the box. But I need the coin to fly to my menu option. At any resolution and screen format.

  • Moving a scene sprite to the position of a UI RectTransform isn't strictly involving AC, unless you're looking to refer to the RectTransform by way of a linked Menu element.

    An object in the scene can be moved to a RectTransform by a custom script. Try the following, SpriteToUIMover.cs, attached to your scene sprite:

    using UnityEngine;
    using System.Collections;
    
    public class SpriteToUIMover : MonoBehaviour
    {
    
        public Vector3 offset;
        public RectTransform toRectTransform;
        public float moveDuration = 3f;
    
        public void MoveToUI ()
        {
            StopAllCoroutines ();
            StartCoroutine (MoveToUICo ());
        }
    
        private IEnumerator MoveToUICo ()
        {
            Vector3 fromPosition = transform.position;
    
            Vector2 toPositionScreen = RectTransformToScreenSpace (toRectTransform).center;
            Vector3 toPosition = Camera.main.ScreenToWorldPoint (new Vector3 (toPositionScreen.x, toPositionScreen.y, transform.position.z - Camera.main.transform.position.z));
    
            float timer = moveDuration;
            while (timer > 0f)
            {
                float p = 1f - (timer / moveDuration);
                transform.position = Vector3.Lerp (fromPosition, toPosition + offset, p);
    
                timer -= Time.deltaTime;
                yield return null;
            }
        }
    
        private Rect RectTransformToScreenSpace (RectTransform transform)
        {
            Vector2 size = Vector2.Scale(transform.rect.size, transform.lossyScale);
            return new Rect((Vector2)transform.position - (size * 0.5f), size);
        }
    
    }
    

    As a test, create a new UI Button or Image in the scene (no need to link it to AC) and assign it in the component's "To Rect Transform" Inspector field.

    To have it move, use either the Object: Call event or Object: Send message Action to trigger the component's MoveToUI function. You should find that the sprite then moves to the UI.

    If successful, we can then see about adapting it to work by referring to an AC menu element.

  • Working. However, I am using the Unity menu version. So it looks like we have what we need.
    https://gyazo.com/8e3ac87b63c39c0e670f5957ff31ad27

  • It remains to figure out how to send an object to a specific item of the Unity menu and I will get what I need :)

  • edited December 2021

    This updated script will let you enter the name of the menu and element you want to get the RectTransform for:

    using UnityEngine;
    using System.Collections;
    
    public class SpriteToUIMover : MonoBehaviour
    {
    
        public string menuName;
        public string elementName;
        public int elementSlot;
    
        public Vector3 offset;
        public float moveDuration = 3f;
    
        public void MoveToUI ()
        {
            StopAllCoroutines ();
            StartCoroutine (MoveToUICo ());
        }
    
        private IEnumerator MoveToUICo ()
        {
            Vector3 fromPosition = transform.position;
    
            AC.MenuElement element = AC.PlayerMenus.GetElementWithName (menuName, elementName);
            RectTransform toRectTransform = element.GetRectTransform (elementSlot);
    
            Vector2 toPositionScreen = RectTransformToScreenSpace (toRectTransform).center;
            Vector3 toPosition = Camera.main.ScreenToWorldPoint (new Vector3 (toPositionScreen.x, toPositionScreen.y, transform.position.z - Camera.main.transform.position.z));
    
            float timer = moveDuration;
            while (timer > 0f)
            {
                float p = 1f - (timer / moveDuration);
                transform.position = Vector3.Lerp (fromPosition, toPosition + offset, p);
    
                timer -= Time.deltaTime;
                yield return null;
            }
        }
    
        private Rect RectTransformToScreenSpace (RectTransform transform)
        {
            Vector2 size = Vector2.Scale(transform.rect.size, transform.lossyScale);
            return new Rect((Vector2)transform.position - (size * 0.5f), size);
        }
    
    }
    
  • This time it didn't work out so easily. The script gives an error:
    https://gyazo.com/2f4febb23bade75b8b0edfadfed42d06

  • Are you using custom assembly definition files?

    I've tweaked the above script - give it another try.

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.