一:效果演示
二:实现
——导入Dotween插件
——完整代码
using System;
using UnityEngine;
using DG.Tweening;
public class EffectManager : MonoBehaviour
{
private static EffectManager _instance;
public static EffectManager Instance
{
get
{
if (_instance == null)
{
GameObject go = new GameObject(typeof(EffectManager).ToString());
DontDestroyOnLoad(go);
_instance = go.AddComponent<EffectManager>();
}
return _instance;
}
}
/// <summary>
/// 显示奖励特效
/// </summary>
/// <param name="_startPos">实例化出来的位置</param>
/// <param name="_targetPos">目标位置</param>
/// <param name="_parent">父物体</param>
/// <param name="prefabName">预制体名称</param>
/// <param name="_num">实例化的数量</param>
/// <param name="_callback">回调</param>
public void ShowAwardEffect(Vector3 _startPos, Vector3 _targetPos, Transform _parent, string prefabName, int _num = 10, Action _callback = null)
{
//渲染UI的相机名称和画布名称
Camera uiCamera = GameObject.Find("UICamera").GetComponent<Camera>();
Canvas uiCanvas = GameObject.Find("UICanvas").GetComponent<Canvas>();
if (uiCamera == null || uiCanvas == null)
{
return;
}
for (int i = 0; i < _num; i++)
{
GameObject prefab = Instantiate(Resources.Load<GameObject>(prefabName));//根据项目路径进行修改
prefab.transform.SetParent(_parent, false);
float randomTime = UnityEngine.Random.Range(0.4f, 0.6f);
prefab.transform.position = _startPos;
prefab.transform.DOMove(_startPos + UnityEngine.Random.insideUnitSphere, UnityEngine.Random.Range(0.4f, 0.6f)).SetEase(Ease.OutSine).OnComplete
(
() =>
{
prefab.transform.DOMove(_targetPos, UnityEngine.Random.Range(0.8f, 1.2f)).SetEase(Ease.InOutSine).OnComplete
(
() =>
{
Destroy(prefab);
_callback?.Invoke();
}
);
}
);
}
}
}