大连中石油电子沙盘
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.
 
 
 
 

82 lines
2.5 KiB

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<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, float Offset)
{
alltime = time;
Dirc = Dir;
GetEndPos(Offset);
CanMove = true;
}
public void GetEndPos(float Offset)
{
switch (Dirc)
{
case MoveDir.Up:
MoveDis = gameObject.GetComponent<RectTransform>().rect.height;
EndPositon = AnchoredPosition + Vector2.up * (MoveDis - Offset);
break;
case MoveDir.Down:
MoveDis = gameObject.GetComponent<RectTransform>().rect.height;
EndPositon = AnchoredPosition + Vector2.down * (MoveDis - Offset);
break;
case MoveDir.Left:
MoveDis = gameObject.GetComponent<RectTransform>().rect.width;
EndPositon = AnchoredPosition + Vector2.left * (MoveDis - Offset);
break;
case MoveDir.Right:
MoveDis = gameObject.GetComponent<RectTransform>().rect.width;
EndPositon = AnchoredPosition + Vector2.right * (MoveDis - Offset);
break;
}
}
}