add project files
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using OHM.UnityToolkit;
|
||||
using OHM.UnityToolkit.Pooling;
|
||||
|
||||
namespace OHM.UnityToolkit.UI
|
||||
{
|
||||
public class UIMoveToTarget : MonoBehaviour, PooledObjectListener
|
||||
{
|
||||
[SerializeField]
|
||||
private RectTransform rectTransform;
|
||||
|
||||
[SerializeField]
|
||||
public RectTransform target;
|
||||
|
||||
[SerializeField]
|
||||
public float initialDelay;
|
||||
|
||||
[SerializeField]
|
||||
public float animDuration;
|
||||
|
||||
[SerializeField]
|
||||
public AnimationCurve xCurve;
|
||||
|
||||
[SerializeField]
|
||||
public AnimationCurve yCurve;
|
||||
|
||||
[SerializeField]
|
||||
public bool autoStart = true;
|
||||
[SerializeField]
|
||||
public bool destroyOnEnd;
|
||||
|
||||
public UnityEvent onMoveDone = new UnityEvent();
|
||||
private void Start()
|
||||
{
|
||||
ComponentsUtils.GetRequiredComponent(this, ref rectTransform);
|
||||
if (autoStart)
|
||||
{
|
||||
StartCoroutine(MoveAnim());
|
||||
}
|
||||
}
|
||||
|
||||
public void StartMove()
|
||||
{
|
||||
StartCoroutine(MoveAnim());
|
||||
}
|
||||
|
||||
private IEnumerator MoveAnim()
|
||||
{
|
||||
yield return new WaitForEndOfFrame();
|
||||
yield return new WaitForSeconds(initialDelay);
|
||||
Vector2 from = rectTransform.anchoredPosition;
|
||||
var delta = (Vector2)target.localPosition - from;
|
||||
for (float t = animDuration; t > 0f; t -= Time.deltaTime)
|
||||
{
|
||||
float ratio = 1f - t / animDuration;
|
||||
rectTransform.anchoredPosition = new Vector2(
|
||||
from.x + xCurve.Evaluate(ratio) * delta.x,
|
||||
from.y + yCurve.Evaluate(ratio) * delta.y);
|
||||
yield return new WaitForEndOfFrame();
|
||||
}
|
||||
onMoveDone.Invoke();
|
||||
if (destroyOnEnd)
|
||||
{
|
||||
PoolsManager.ReleaseObject(base.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnEnable()
|
||||
{
|
||||
StartCoroutine(MoveAnim());
|
||||
}
|
||||
|
||||
public void OnRelease()
|
||||
{
|
||||
}
|
||||
|
||||
public void OnReset()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user