Unity Button趣味扩展

演示效果

demo

Hierarchy界面节点关系

代码部分

注意:UIButtonExtend脚本挂载到了Button对象上面,例如Button_Play

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.UIElements;
using Button = UnityEngine.UI.Button;

public class UIButtonExtend : Selectable
{
    public Button.ButtonClickedEvent OnClick;
    private GameObject sign;
    
    // Start is called before the first frame update
    protected override void Start()
    {
        base.Start();
        sign = transform.Find("Sign").gameObject;
    }

    public override void OnPointerUp(PointerEventData eventData)
    {
        base.OnPointerUp(eventData);
        //立刻清理按钮状态(SelectionState:Normal,Highlighted,Pressed,Selected,Disabled)
        InstantClearState();   
        //parm_1:切换到Normal状态  parm_2:非立刻执行(意思是执行完该执行的即过完fade duration时间后执行当前段)
        DoStateTransition(SelectionState.Normal, false);
    }

    public override void OnPointerEnter(PointerEventData eventData)
    {
        base.OnPointerEnter(eventData);
        //执行进入动画
        if(sign != null) sign.SetActive(true);
    }

    public override void OnPointerExit(PointerEventData eventData)
    {
        base.OnPointerExit(eventData);
        if(sign != null) sign.SetActive(false);
    }
}

posted @ 2022-03-24 23:34  yassine  阅读(161)  评论(0编辑  收藏  举报