Unity实现领取奖励特效

   日期:2020-09-06     浏览:88    评论:0    
核心提示:一:效果演示二:实现——导入Dotween插件——完整代码using System;using UnityEngine;using DG.Tweening;public class EffectManager : MonoBehaviour{ private static EffectManager _instance; public static EffectManager Instance { get { .

一:效果演示

二:实现

——导入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();
                            }
                        );
                    }
                );
        }
    }
}

 

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服