using DG.Tweening;  
 | 
using System.Collections.Generic;  
 | 
using UnityEngine;  
 | 
  
 | 
//获得货币动画  
 | 
public class MoneyMoveByPath : MonoBehaviour  
 | 
{  
 | 
    [Header("第一段动画移动时间")]  
 | 
    public float duration1 = 1f;      // 动画持续时间  
 | 
    [Header("随机初始左右间距")]  
 | 
    public float spacing = 50f;         // 设置图片之间的间隔  
 | 
    [Header("X参考偏移值")]  
 | 
    public float XPath = 50f;         
 | 
    [Header("Y参考偏移值")]  
 | 
    public float YPath = 100f;  
 | 
    [Header("Y参考偏移值")]  
 | 
    public AnimationCurve heightCurve; // 用于定义路径高度的曲线  
 | 
    [Header("第二段动画飘到目的地")]  
 | 
    public float duration2 = 0.4f;     // 动画持续时间  
 | 
    public Transform targetPosition;  
 | 
    List<ImageEx> imgMoneys = new List<ImageEx>();  
 | 
      
 | 
    /// <summary>  
 | 
    /// 播放掉落动画  
 | 
    /// </summary>  
 | 
    /// <param name="targetPosition">目的地坐标</param>  
 | 
    /// <param name="moneyType">货币类型</param>  
 | 
    /// <param name="num">掉几个货币</param>  
 | 
    public void PlayAnimation(int moneyType, int num)  
 | 
    {  
 | 
        int createImgCnt = 0;  
 | 
        if (imgMoneys.IsNullOrEmpty())  
 | 
        {  
 | 
            createImgCnt = num;  
 | 
        }  
 | 
        else  
 | 
        {  
 | 
            createImgCnt = Mathf.Max(0, num - imgMoneys.Count);  
 | 
        }  
 | 
        if (createImgCnt > 0)  
 | 
        {  
 | 
            for (int i = 0; i < createImgCnt; i++)  
 | 
            {  
 | 
                var imgMoney = new GameObject("moneyIcon" + i);  
 | 
                imgMoney.AddMissingComponent<ImageEx>();  
 | 
                //挂父节点  
 | 
                imgMoney.transform.SetParentEx(this.transform, Vector3.zero, Quaternion.identity, Vector3.one);  
 | 
                var moneyImg = imgMoney.GetComponent<ImageEx>();  
 | 
                moneyImg.SetSprite(StringUtility.Contact("Money_Type_", moneyType));  
 | 
                moneyImg.raycastTarget = false;  
 | 
                moneyImg.GetComponent<RectTransform>().sizeDelta = new Vector2(40, 40);  
 | 
                imgMoneys.Add(moneyImg);  
 | 
            }  
 | 
        }  
 | 
  
 | 
        //通过 heightCurve 生成路径,时间为x 总时间为1,y为高度,偏移量为100  
 | 
        int pointCount = 30;  
 | 
        Vector3[] points = new Vector3[pointCount];  
 | 
        for (int i = 0; i < pointCount; i++)  
 | 
        {  
 | 
            float x = i / (pointCount - 1f);  
 | 
            float y = heightCurve.Evaluate(x) * YPath;  
 | 
            points[i] = new Vector3(x * XPath, y, 0);  
 | 
  
 | 
        }  
 | 
  
 | 
  
 | 
          
 | 
  
 | 
        for (int i = 0; i < imgMoneys.Count; i++)  
 | 
        {  
 | 
            var moneyImg = imgMoneys[i];  
 | 
              
 | 
            if (num <= i)  
 | 
            {  
 | 
                moneyImg.transform.localScale = Vector3.zero;  
 | 
            }  
 | 
            else  
 | 
            {  
 | 
                moneyImg.transform.localScale = Vector3.one;  
 | 
                int randDir = Random.Range(0, 2) == 0 ? 1 : -1;  
 | 
                float offsetX = Random.Range(0, spacing);  
 | 
  
 | 
                // x y 随机下差异  
 | 
                float randX = Random.Range(0.2f, 1f);  
 | 
                float randY = Random.Range(0.6f, 1f);  
 | 
                Vector3[] points2 = new Vector3[pointCount];  
 | 
                for (int j = 0; j < points.Length; j++)  
 | 
                {  
 | 
                    points2[j] = new Vector3((points[j].x + offsetX)* randDir * randX, points[j].y * randY, points[j].z);  
 | 
                }  
 | 
                moneyImg.transform.localPosition = points2[0];  
 | 
  
 | 
  
 | 
                moneyImg.transform.DOLocalPath(points2, duration1, PathType.CatmullRom).SetEase(Ease.InOutSine).OnComplete(() =>  
 | 
                {  
 | 
                    if (targetPosition != null)  
 | 
                    {  
 | 
                        //移动到目标位置  
 | 
                        moneyImg.transform.DOMove(targetPosition.position, duration2).OnComplete(() =>  
 | 
                        {  
 | 
                            moneyImg.transform.localScale = Vector3.zero;  
 | 
                        });  
 | 
                    }  
 | 
                    else  
 | 
                    {  
 | 
                        moneyImg.transform.localScale = Vector3.zero;  
 | 
                    }  
 | 
                });  
 | 
  
 | 
  
 | 
            }  
 | 
        }  
 | 
    }  
 | 
} 
 |