技能是一個Button按鈕,需要滿足以下要求:
一.技能的冷卻
1.當滑鼠放上與移開時,自适應縮放大小,添加UI Button Scale即可
2.播放聲音
3.技能的冷卻效果:1.顯示遮罩2.冷卻時不能再進行點選
1.在Skill下添加一個Mask遮罩,選擇 該技能的Sprite,将顔色ColorI Tint更改為灰色
技能有冷卻,普通攻擊不需要冷卻,是以判斷是否有子物體Mask即可判斷是否是技能,冷卻效果通過Mask.fillAmount來實作
2.不能被點選:移除碰撞效果,即collider設定為false
二.技能的施放
1.在技能被按下的時候,找到player,進行player動作的播放
技能
public PosType posType = PosType.Basic;
private PlayerAnimation playerAnim;
//冷卻技能
public float coldTime = 4;
private float coldTimer = 0;//技能冷卻的剩餘時間
private UISprite maskSprite = null;
private UIButton btn;
void Start()
{
//這樣子會優化查找,隻需查找一次player即可
playerAnim = TranscriptManager._instance.player.GetComponent<PlayerAnimation>();
if (transform.Find("Mask"))
{
maskSprite = transform.Find("Mask").GetComponent<UISprite>();
}
btn = this.GetComponent<UIButton>();
}
void Update()
{
//技能的冷卻,普通攻擊不需要冷卻
if (maskSprite == null) return;
if (coldTimer > 0)
{
coldTimer -= Time.deltaTime;
maskSprite.fillAmount = coldTimer / coldTime;//更新mask的fillAmount
if (coldTimer <= 0)
{
Enable();
}
}
else//冷卻完成
{
maskSprite.fillAmount = 0;//取消mask
}
}
void OnPress(bool isPress)
{
playerAnim.OnAttackButtonClick(isPress, posType);
if (isPress && maskSprite != null)//被按下,普通技不能被冷卻
{
coldTimer = coldTime;
Disable();
}
}
void Disable()//技能按鈕禁止被點選
{
GetComponent<Collider>().enabled = false;
btn.SetState(UIButtonColor.State.Normal, true);
}
void Enable()//技能按鈕允許被點選
{
GetComponent<Collider>().enabled = true;
}
人物動作
private Animator anim;
void Awake()
{
anim = this.GetComponent<Animator>();
}
public void OnAttackButtonClick(bool isPress,PosType posType)
{
if (posType == PosType.Basic)
{
if (isPress)
{
anim.SetTrigger("Attack");
}
}
else
{
anim.SetBool("Skill" + (int)posType, isPress);//按下就觸發
}
}