技能是一个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);//按下就触发
}
}