Unity实现领取奖励特效

编辑: admin 分类: c#语言 发布时间: 2022-01-18 来源:互联网

本文实例为大家分享了Unity实现领取奖励特效的具体代码,供大家参考,具体内容如下

一、效果演示

二、实现

导入Dotween插件

完整代码

using System;
using UnityEngine;
using DG.Tweening;
 
/// <summary>
/// 特效管理
/// </summary>
public class EffectManager : MonoBehaviour
{
 //单例
 private static EffectManager _instance;
 public static EffectManager Instance
 {
  get
  {
  【文章出处http://www.nextecloud.cn/kt.html欢迎转载】 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();
  });
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持海外IDC网。