//--------------------------------------------------------
|
// [Author]: 第二世界
|
// [ Date ]: Thursday, September 07, 2017
|
//--------------------------------------------------------
|
using UnityEngine;
|
using System.Collections;
|
using UnityEngine.UI;
|
using System;
|
|
public class ScreenMoveTo : MonoBehaviour
|
{
|
|
[SerializeField]
|
LerpType m_LerpType = LerpType.Linear;
|
|
[SerializeField]
|
Vector2 m_Destination;
|
public Vector2 destination {
|
get { return m_Destination; }
|
set { m_Destination = value; }
|
}
|
|
[SerializeField]
|
float m_Duration = 0.5f;
|
public float duration {
|
get { return m_Duration; }
|
set { m_Duration = value; }
|
}
|
|
[SerializeField]
|
bool m_IsLocal = true;
|
public bool isLocal {
|
get { return m_IsLocal; }
|
}
|
|
bool disableOnEnd = true;
|
float endTime = 0f;
|
|
Vector3 startPosition = Vector3.zero;
|
Vector3 startLocalPosition = Vector3.zero;
|
Vector3 refPosition = Vector3.zero;
|
|
bool end = false;
|
Action endCallBack = null;
|
|
public void Begin(bool _deActiveOnEnd = true)
|
{
|
endTime = Time.time + duration;
|
end = false;
|
disableOnEnd = _deActiveOnEnd;
|
startPosition = this.transform.position;
|
startLocalPosition = this.transform.localPosition;
|
refPosition = Vector3.zero;
|
endCallBack = null;
|
|
this.enabled = true;
|
if (!this.gameObject.activeInHierarchy)
|
{
|
this.SetActive(true);
|
}
|
}
|
|
public void Begin(Action _callBack, bool _disableOnEnd = true)
|
{
|
endTime = Time.time + duration;
|
end = false;
|
disableOnEnd = _disableOnEnd;
|
startPosition = this.transform.position;
|
startLocalPosition = this.transform.localPosition;
|
refPosition = Vector3.zero;
|
endCallBack = _callBack;
|
|
this.enabled = true;
|
if (!this.gameObject.activeInHierarchy)
|
{
|
this.SetActive(true);
|
}
|
}
|
|
private void LateUpdate()
|
{
|
switch (m_LerpType)
|
{
|
case LerpType.Linear:
|
if (Time.time < endTime)
|
{
|
var t = Mathf.Clamp01(1f - (endTime - Time.time) / m_Duration);
|
if (isLocal)
|
{
|
this.transform.localPosition = Vector3.Lerp(startLocalPosition, destination, t);
|
}
|
else
|
{
|
this.transform.position = Vector3.Lerp(startPosition, destination, t);
|
}
|
}
|
else
|
{
|
end = true;
|
if (isLocal)
|
{
|
this.transform.localPosition = destination;
|
}
|
else
|
{
|
this.transform.position = destination;
|
}
|
}
|
break;
|
case LerpType.Smooth:
|
|
if (isLocal)
|
{
|
if (Vector3.Distance(this.transform.localPosition, new Vector3(destination.x, destination.y)) > 1)
|
{
|
this.transform.localPosition = Vector3.SmoothDamp(this.transform.localPosition, destination, ref refPosition, duration);
|
}
|
else
|
{
|
this.transform.localPosition = destination;
|
end = true;
|
}
|
}
|
else
|
{
|
if (Vector3.Distance(this.transform.position, new Vector3(destination.x, destination.y)) > 1)
|
{
|
this.transform.position = Vector3.SmoothDamp(this.transform.position, destination, ref refPosition, duration);
|
}
|
else
|
{
|
this.transform.position = destination;
|
end = true;
|
}
|
}
|
break;
|
}
|
|
if (end)
|
{
|
if (endCallBack != null)
|
{
|
endCallBack();
|
endCallBack = null;
|
}
|
}
|
|
if (end && disableOnEnd)
|
{
|
this.enabled = false;
|
}
|
|
}
|
|
|
public enum LerpType
|
{
|
Linear,
|
Smooth,
|
}
|
|
}
|