Question by
SaurabhStudio · Mar 09, 2017 at 11:18 AM ·
gui
Move UI element from top to bottom of the screen
I wont to move Ui Button from top of the canvas screen to bottom of the canvas screen. After moving outside of canvas(Bottom canvas) it should replace to top of the canvas screen.
Comment
Best Answer
Answer by CoryButler · Mar 09, 2017 at 02:16 PM
Hello, @SaurabhStudio,
Attach this script to the UI Button you want to move. It assumes your Canvas is named "Canvas".
using UnityEngine;
public class MoveButton : MonoBehaviour {
RectTransform canvas;
RectTransform button;
Vector3 startingPosition;
public float speed;
void Start()
{
button = gameObject.GetComponent<RectTransform>();
canvas = GameObject.Find("Canvas").GetComponent<RectTransform>();
startingPosition = transform.position;
speed = -10f;
}
void Update () {
transform.Translate(0f, speed, 0f);
if (button.position.y < -button.rect.height)
transform.position = new Vector3(startingPosition.x, canvas.rect.height + button.rect.height, startingPosition.z);
}
}
Answer by Roixo · Apr 30, 2018 at 05:50 PM
Hi,
You can move any UI element using script with a coroutine and a MoveTowards method or Lerp, without the disadvantages of using Animator.
I explain how exactly make that in this tutorial: