using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public enum MoveDir { Up, Down, Left, Right } public class UIMove : MonoBehaviour { private Vector2 AnchoredPosition; private Vector2 EndPositon; private bool CanMove = false; private float alltime; private MoveDir Dirc; private float MoveDis; private float timmer = 0; private float Swidth; private float Sheight; public void SetPos() { AnchoredPosition = GetComponent().anchoredPosition; Swidth = Screen.width; Sheight = Screen.height; } void Update() { if (CanMove) { timmer += Time.deltaTime; if (AnchoredPosition != EndPositon) { GetComponent().anchoredPosition = Vector2.Lerp(AnchoredPosition, EndPositon, timmer / alltime); } if (timmer >= alltime) { AnchoredPosition = EndPositon; CanMove = false; timmer = 0; } } else { if (Swidth != Screen.width || Sheight != Screen.height) { AnchoredPosition = GetComponent().anchoredPosition; Swidth = Screen.width; Sheight = Screen.height; } } } public void AnchorMove(RectTransform rec, float time, MoveDir Dir, float Offset) { alltime = time; Dirc = Dir; GetEndPos(Offset); CanMove = true; } public void GetEndPos(float Offset) { switch (Dirc) { case MoveDir.Up: MoveDis = gameObject.GetComponent().rect.height; EndPositon = AnchoredPosition + Vector2.up * (MoveDis - Offset); break; case MoveDir.Down: MoveDis = gameObject.GetComponent().rect.height; EndPositon = AnchoredPosition + Vector2.down * (MoveDis - Offset); break; case MoveDir.Left: MoveDis = gameObject.GetComponent().rect.width; EndPositon = AnchoredPosition + Vector2.left * (MoveDis - Offset); break; case MoveDir.Right: MoveDis = gameObject.GetComponent().rect.width; EndPositon = AnchoredPosition + Vector2.right * (MoveDis - Offset); break; } } }