这篇文章主要为大家详细介绍了Unity实现领取奖励特效,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Unity实现领取奖励特效的具体代码,供大家参考,具体内容如下
一、效果演示
二、实现
导入Dotween插件
完整代码
using System;
using UnityEngine;
using DG.Tweening;
/// <summary>
/// 特效管理
/// </summary>
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="siblingIndex">子层级下的索引</param>
/// <param name="num">实例化的数量</param>
/// <param name="onFinish">回调</param>
public void ShowAwardEffect(Vector3 startPos, Vector3 targetPos, Transform parent, string prefabName, int siblingIndex = -1, int num = 10, Action onFinish = null)
{
//渲染UI的相机名称和画布名称
Camera uiCamera = GameObject.Find("UICamera").GetComponent<Camera>();
Canvas uiCanvas = GameObject.Find("Canvas").GetComponent<Canvas>();
if (uiCamera == null || uiCanvas == null)
{
return;
}
var seq = DOTween.Sequence();
for (int i = 0; i < num; i++)
{
GameObject prefab = Instantiate(Resources.Load<GameObject>(prefabName));//根据项目路径进行修改
prefab.transform.SetParent(parent, false);
if (siblingIndex != -1)
{
prefab.transform.SetSiblingIndex(siblingIndex);
}
float randomTime = UnityEngine.Random.Range(0.4f, 0.6f);
prefab.transform.position = startPos;
seq.Insert(0, prefab.transform.DOMove(startPos + UnityEngine.Random.insideUnitSphere, randomTime)).SetEase(Ease.OutSine);
seq.Insert(randomTime, prefab.transform.DOMove(targetPos, UnityEngine.Random.Range(0.8f, 1.2f)).SetEase(Ease.InOutSine).OnComplete
(() =>
{
Destroy(prefab);
}));
}
seq.SetUpdate(true);
seq.AppendCallback(() =>
{
onFinish?.Invoke();
});
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得得之家。
沃梦达教程
本文标题为:Unity实现领取奖励特效
基础教程推荐
猜你喜欢
- winform把Office转成PDF文件 2023-06-14
- C# windows语音识别与朗读实例 2023-04-27
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- unity实现动态排行榜 2023-04-27
- 一个读写csv文件的C#类 2022-11-06
- C#类和结构详解 2023-05-30
- C#控制台实现飞行棋小游戏 2023-04-22
- C# 调用WebService的方法 2023-03-09
- C# List实现行转列的通用方案 2022-11-02
- ZooKeeper的安装及部署教程 2023-01-22