You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.5 KiB
84 lines
2.5 KiB
4 years ago
|
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 speed;
|
||
|
private float timmer = 0;
|
||
|
private float Swidth;
|
||
|
private float Sheight;
|
||
|
void Start()
|
||
|
{
|
||
|
AnchoredPosition = GetComponent<RectTransform>().anchoredPosition;
|
||
|
Swidth = Screen.width;
|
||
|
Sheight = Screen.height;
|
||
|
}
|
||
|
void Update()
|
||
|
{
|
||
|
if (CanMove)
|
||
|
{
|
||
|
timmer += Time.deltaTime;
|
||
|
if (AnchoredPosition != EndPositon)
|
||
|
{
|
||
|
GetComponent<RectTransform>().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<RectTransform>().anchoredPosition;
|
||
|
Swidth = Screen.width;
|
||
|
Sheight = Screen.height;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
public void AnchorMove(RectTransform rec, float time, MoveDir Dir)
|
||
|
{
|
||
|
alltime = time;
|
||
|
Dirc = Dir;
|
||
|
GetEndPos();
|
||
|
CanMove = true;
|
||
|
}
|
||
|
public void GetEndPos()
|
||
|
{
|
||
|
switch (Dirc)
|
||
|
{
|
||
|
case MoveDir.Up:
|
||
|
MoveDis = gameObject.GetComponent<RectTransform>().rect.height;
|
||
|
EndPositon = AnchoredPosition + Vector2.up * MoveDis;
|
||
|
break;
|
||
|
case MoveDir.Down:
|
||
|
MoveDis = gameObject.GetComponent<RectTransform>().rect.height;
|
||
|
EndPositon = AnchoredPosition + Vector2.down * MoveDis;
|
||
|
break;
|
||
|
case MoveDir.Left:
|
||
|
MoveDis = gameObject.GetComponent<RectTransform>().rect.width;
|
||
|
EndPositon = AnchoredPosition + Vector2.left * MoveDis;
|
||
|
break;
|
||
|
case MoveDir.Right:
|
||
|
MoveDis = gameObject.GetComponent<RectTransform>().rect.width;
|
||
|
EndPositon = AnchoredPosition + Vector2.right * MoveDis;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|