using System.Collections;
|
using System.Collections.Generic;
|
using System.Globalization;
|
using Unity.VisualScripting;
|
using UnityEngine;
|
|
public class UIAnimation : MonoBehaviour, IUIShow
|
{
|
public Vector2 Direction;
|
public float Speed = 500f;
|
private RectTransform myRect;
|
private bool run;
|
private bool isHide;
|
private Vector2 defaultPos;
|
private Vector2 dir;
|
|
void Start()
|
{
|
myRect = GetComponent<RectTransform>();
|
run = false;
|
|
defaultPos = transform.position;
|
|
|
if (SceneTypeManager.Inst.IsVRMode)
|
{
|
Hide();
|
}
|
}
|
|
float GetTargetPos(float dir, float pos, float sizeDelta)
|
{
|
float targetPos = 0;
|
if (dir > 0)
|
{
|
targetPos = pos - sizeDelta / 2;
|
}
|
else if (dir < 0)
|
{
|
targetPos = pos + sizeDelta / 2;
|
}
|
|
return targetPos;
|
}
|
|
bool IsStop(float dir, float pos, float oldPos)
|
{
|
bool stop = false;
|
if (dir > 0)
|
{
|
if (pos >= oldPos)
|
{
|
stop = true;
|
}
|
}
|
else if (dir < 0)
|
{
|
if (pos <= oldPos)
|
{
|
stop = true;
|
}
|
}
|
else
|
{
|
stop = true;
|
}
|
return stop;
|
}
|
|
void Update()
|
{
|
if (run)
|
{
|
if (!isHide)
|
{
|
dir = Direction;
|
myRect.anchoredPosition += dir * Speed * Time.deltaTime;
|
|
float targetPosX = GetTargetPos(Direction.x, transform.position.x, myRect.sizeDelta.x);
|
float targetPosY = GetTargetPos(Direction.y, transform.position.y, myRect.sizeDelta.y);
|
if (targetPosX < 0 || targetPosY < 0 || targetPosX > Screen.width || targetPosY > Screen.height)
|
{
|
run = false;
|
isHide = true;
|
}
|
}
|
else
|
{
|
dir = Direction * -1;
|
myRect.anchoredPosition += dir * Speed * Time.deltaTime;
|
|
bool stopX = IsStop(dir.x, transform.position.x, defaultPos.x);
|
bool stopY = IsStop(dir.y, transform.position.y, defaultPos.y);
|
|
if (stopX && stopY)
|
{
|
run = false;
|
transform.position = defaultPos;
|
isHide = false;
|
}
|
}
|
}
|
}
|
|
public void Show()
|
{
|
if (run) {
|
run = false;
|
isHide = true;
|
}
|
|
if (isHide)
|
{
|
run = true;
|
}
|
}
|
|
public void Hide()
|
{
|
if (run) {
|
run = false;
|
isHide = false;
|
}
|
|
if (!isHide)
|
{
|
run = true;
|
}
|
}
|
|
public void Stop()
|
{
|
if (run)
|
{
|
run = false;
|
}
|
}
|
}
|