思路:
- 使用DOTween的DOPunchAnchorPos方法移动UI;
- 使用DOTween的DOFade方法实现被攻击掉落生命值时的淡入淡出效果。
实现代码:
BattleController.cs
/// <summary> 攻击 </summary>
void Attack(RectTransform attacker, Vector2 directionAndstrength, Text suffererLossHPText, Text sufferCurrentHPText, int lossHP, ref int suffererCurrentHP)
{
if (!m_isBattleOver)
{
// 攻击动画
attacker.DOPunchAnchorPos(directionAndstrength, 0.2f, 0, 0f, false);
// 受攻击一方生命值减小,减小值为攻击方的攻击力
suffererCurrentHP -= lossHP;
m_battleView.SetCurrentHP(sufferCurrentHPText, suffererCurrentHP);
// Debug.Log("currentHP:" + suffererCurrentHP);
// 受攻击一方显示生命值减小的值
suffererLossHPText.text = "-" + lossHP.ToString();
// 调用减小生命值动画效果
m_battleView.LossHPAnimation(suffererLossHPText);
}
}
RectTransform (Unity UI 4.6)
DOPunchAnchorPos(Vector2 punch, float duration, int vibrato, float elasticity, boolsnapping)
Punches the target's anchoredPosition with the given values.
punch: punch The direction and strength of the punch (added to the RectTransform's current position).
duration: The duration of the tween.
vibrato: Indicates how much will the punch vibrate.
elasticity: Represents how much (0 to 1) the vector will go beyond the starting position when bouncing backwards. 1 creates a full oscillation between the punch direction and the opposite direction, while 0 oscillates only between the punch and the start position.
**snapping: ** If TRUE the tween will smoothly snap all values to integers.
BattleView.cs
/// <summary> 被攻击时掉落生命值动画效果 </summary>
public void LossHPAnimation(Text lossHPText)
{
// FadeIn
lossHPText.DOFade(1f, 0f);
// FadeOut
lossHPText.DOFade(0f, 1.5f);
}
Text (Unity UI 4.6)
DOFade(float to, float duration)
Fades the target's alpha to the given value.
Unity5.4, C#
Github
网友评论