思路:
- 使用DOTween的DOPunchAnchorPos方法移動UI啥繁;
- 使用DOTween的DOFade方法實現(xiàn)被攻擊掉落生命值時的淡入淡出效果。
實現(xiàn)代碼:
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();
// 調(diào)用減小生命值動畫效果
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