当前位置 : 主页 > 网络编程 > c#编程 >

Unity实现领取奖励特效

来源:互联网 收集:自由互联 发布时间:2021-04-07
本文实例为大家分享了Unity实现领取奖励特效的具体代码,供大家参考,具体内容如下 一、效果演示 二、实现 导入Dotween插件 完整代码 using System;using UnityEngine;using DG.Tweening; /// summary

本文实例为大家分享了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();
  });
 }
}

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

网友评论