游戏中,金币爆炸,然后沿着一定曲线飞往目标地方,形成回收效果,这种需求很常见,并且回收的效果也多种多样。
这里做一个比较简单的效果,但是着重点会放在两种路径方式下的回收效果功能。
具体说就是:
(1)金币出发地点UI坐标,目标地点UI坐标。
(2)金币出发地点场景中某个世界坐标位置,目标地点UI坐标。
这种情况经常有,比如场景中的人物的某个UI元素飞向界面上的某个UI,又比如场景中的某个物体产生某各种资源或者金币飞向界面上的UI。
效果图:
2.gif
源码地址:https://github.com/linguoyuan/GameDisplay_GoldFly
脚本构成:
CoinEffectMgr.cs :金币回收调用管理类,那个世界做的物体或者UI需要用这个效果,挂上这个脚本即可。
UICurrencyCollect.cs :金币具体飞行类。
功能分析:
-
1、金币的转动,用Sprite动画去做。
-
2、金币的路径动画,用DoTween动画去做,并开放一部分参数出来可以控制。
比如:
num :产生金币的数量
time :金币飞行的总时间
TargetPos:目标地点
面板参数:
image.png -
3、在世界坐标转UI坐标的时候,先把出发地点的世界坐标转为ViewPort坐标 ,然后再把ViewPort坐标转为UGUI坐标。
因为ViewPort的尺寸大小是0~1,然后用ViewPort的坐标值乘以画布的尺寸大小就刚好得到世界坐标在UGUI坐标系中的位置了。
CoinEffectMgr.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CoinEffectMgr : MonoBehaviour
{
[Header("金币预制体")]
public GameObject Coin;
[Header("金币数量")]
public int num;
[Header("最终目的地")]
public Transform targetPos;
[Header("金币飞行速度")]
public float time = 1;
public void StartEffect()
{
if (transform.GetComponent<RectTransform>() != null)
{
for (int i = 0; i < num; i++)
{
GameObject go = Instantiate(Coin, Vector3.zero, Quaternion.identity) as GameObject;
//UICurrencyCollect go = GameObject.Instantiate(Coin, transform.localPosition, Quaternion.identity, transform);
go.transform.SetParent(transform);
go.transform.localPosition = new Vector3(0, 0, 0);
//go.InitAnimation();
UICurrencyCollect cc = go.GetComponent<UICurrencyCollect>();
if (cc != null)
{
cc.SetFlyParam(time);
cc.InitAnimation(targetPos);
}
}
}
else
{
GameObject go = GameObject.Find("Canvas");
if (go == null)
{
go = new GameObject("Canvas").AddComponent<Canvas>().gameObject;
}
GameObject CoinParent = GameObject.Find("CoinParent");
if (CoinParent == null)
{
CoinParent = new GameObject("CoinParent");
CoinParent.AddComponent<RectTransform>();
CoinParent.transform.SetAsLastSibling();
}
else
{
for (int i = 0; i < CoinParent.transform.childCount; i++)
{
Destroy(CoinParent.transform.GetChild(i).gameObject);
}
}
RectTransform rect = go.GetComponent<RectTransform>();
CoinParent.transform.SetParent(go.transform);
Vector3 ObjWorldPos = transform.position;
Vector3 uiObjectPos = WorldToUGUIPosition(rect, Camera.main, ObjWorldPos);
//CoinParent.transform.localPosition = uiObjectPos;
CoinParent.transform.GetComponent<RectTransform>().anchoredPosition = uiObjectPos;
for (int i = 0; i < num; i++)
{
GameObject coinGo = Instantiate(Coin, Vector3.zero, Quaternion.identity) as GameObject;
coinGo.transform.SetParent(CoinParent.transform);
coinGo.transform.localPosition = new Vector3(0, 0, 0);
//go.InitAnimation();
UICurrencyCollect cc = coinGo.GetComponent<UICurrencyCollect>();
if (cc != null)
{
cc.SetFlyParam(time);
cc.InitAnimation(targetPos);
}
}
}
}
public static Vector2 WorldToUGUIPosition(RectTransform canvasRectTransform, Camera camera, Vector3 worldPosition)
{
//世界坐标-》ViewPort坐标
Vector2 viewPos = camera.WorldToViewportPoint(worldPosition);
//ViewPort坐标-〉UGUI坐标
//return new Vector2(canvasRectTransform.rect.width * viewPos.x, canvasRectTransform.rect.height * viewPos.y);
//因为默认的CoinParent锚点是中间位置。使用上方的代码需要设置为左下角锚点
return new Vector2(canvasRectTransform.rect.width * viewPos.x - canvasRectTransform.rect.width * 0.5f, canvasRectTransform.rect.height * viewPos.y - canvasRectTransform.rect.height * 0.5f);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StartEffect();
}
}
}
UICurrencyCollect.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class UICurrencyCollect : MonoBehaviour
{
private float t1 = 0.6f;//第一段飞行时间
private float t2 = 0.2f;//第二段飞行时间
private Transform targetPos;
private Vector3 _startPos1;
private Vector3 _endPos;
private Vector3[] _v_middle = new Vector3[] { new Vector3(0, 0, 0) };
private Vector3[] _v_end = new Vector3[] { new Vector3(0, 0, 0) };
private CanvasGroup cg;
private void Start()
{
cg = GetComponent<CanvasGroup>();
//InitAnimation(transform.gameObject);
}
public void InitAnimation(Transform tartgePosFinal)
{
_startPos1 = transform.position;
_endPos = tartgePosFinal.position;
//Debug.Log("tartgePosFinal.position :" + tartgePosFinal.position);
this.transform.SetAsLastSibling();
transform.parent.transform.SetAsLastSibling();
//transform.parent.parent.transform.SetAsLastSibling();
float random_x, random_y;
random_x = Random.Range(-200, 250);
//Debug.Log("random_x = " + random_x);
random_y = Random.Range(50, 250);
_v_middle[0] = _startPos1 + new Vector3(random_x, random_y, 0);
_v_end[0] = _endPos;
Sequence _seq = DOTween.Sequence();
_seq.Append(transform.DOPath(_v_middle, t1, PathType.CatmullRom).SetEase(Ease.Linear));
_seq.AppendInterval(0.1f);
_seq.Append(transform.DOPath(_v_end, t2, PathType.CatmullRom).SetEase(Ease.Linear));
_seq.Join(DOTween.To(() => cg.alpha, x => cg.alpha = x, 0, 0.5f));
_seq.AppendCallback(delegate { GameObject.Destroy(this.gameObject); });
}
public void SetFlyParam(float mTime)
{
t1 = mTime * 0.6f;
t2 = mTime * 0.2f;
}
}
网友评论