Unity自定义Button

Posted Xiaohei_wh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity自定义Button相关的知识,希望对你有一定的参考价值。

哈喽,艾瑞巴蒂,你们好嘛,2022年到来了,小黑在这里祝愿大家在新的一年如猛虎一头,在工作中用虎的霸气吓退困难,在生活中用虎的“傻不拉几”温暖身边人。新年快乐~

好了,新的一年,新的开始,新的水博客~今天给大家带来,Unity中的自定义按钮:CustomButton。因为好多时候吧,UGUI中的Button并不能满足我们的需求,那怎么办?自己写一个!绝对不向困难妥协,但可以向困难拖鞋,熏死困难~!

该代码中包含以下几个事件:其中按着事件,与其他事件共存双击事件会先触发单击

  • onClick                         单击
  • onDoubleClick             双击
  • onLongPress               长按
  • onKeepPress               按着

 

 大家可以根据自己的需求来使用,也可以进行拓展,可能小黑的代码水平不高,但是希望能帮助到大家,如果你们拓展了告诉小黑一下,小黑会加到博客中来,并且标明是谁提供。

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using UnityEngine.UI;

/*
 * Introduction:Custom UGUI`s button
 * Creator:Xiaohei Wang
 */
[RequireComponent(typeof(Image))]
[RequireComponent(typeof(CanvasRenderer))]
public class CustomButton : Selectable, IPointerClickHandler, ISubmitHandler

    protected CustomButton()  

    [Serializable]
    public class ButtonClickedEvent : UnityEvent  

    [FormerlySerializedAs("onClick")]
    [SerializeField]
    private ButtonClickedEvent m_OnClick = new ButtonClickedEvent(); 

    [FormerlySerializedAs("onLongPress")]
    [SerializeField]
    private ButtonClickedEvent m_onLongPress = new ButtonClickedEvent();

    [FormerlySerializedAs("onDoubleClick")]
    [SerializeField]
    private ButtonClickedEvent m_onDoubleClick = new ButtonClickedEvent();

    [FormerlySerializedAs("onKeepPress")]
    [SerializeField]
    private ButtonClickedEvent m_onKeepPress = new ButtonClickedEvent();

    public ButtonClickedEvent onClick
    
        get  return m_OnClick; 
    
    public ButtonClickedEvent onDoubleClick
    
        get  return m_onDoubleClick; 
    
    public ButtonClickedEvent onLongPress
    
        get  return m_onLongPress; 
    
    public ButtonClickedEvent onKeepPress
    
        get  return m_onKeepPress; 
    

    private bool m_isPress = false;
    private bool m_longPress = false;
    private bool m_isKeepPress = false;
    private DateTime m_currentStartTime;


    private void Press()
    
        if (!IsActive() || !IsInteractable())
            return;

        UISystemProfilerApi.AddMarker("Button.onClick", this);
        m_OnClick.Invoke();
    

    private void Update()
    
        CheckForLongPress();

        if (m_isKeepPress) onKeepPress?.Invoke();
    

    private void CheckForLongPress()
    
        if (m_isPress && !m_longPress)
        
            if ((DateTime.Now - m_currentStartTime).TotalMilliseconds >= 600)
            
                m_isPress = false;
                m_longPress = true;
                m_onLongPress?.Invoke();
            
        
    

    public override void OnPointerDown(PointerEventData eventData)
    
        m_isPress = true;
        m_longPress = false;
        m_isKeepPress = true;
        m_currentStartTime = DateTime.Now;

        base.OnPointerDown(eventData);
    

    public override void OnPointerUp(PointerEventData eventData)
    
        m_isPress = false;
        m_isKeepPress = false;

        base.OnPointerUp(eventData);
    

    public override void OnPointerExit(PointerEventData eventData)
    
        m_isPress = false;
        m_isKeepPress = false;

        base.OnPointerExit(eventData);
    

    public virtual void OnPointerClick(PointerEventData eventData)
    
        if (!m_longPress)
        
            if (eventData.clickCount == 2)
                m_onDoubleClick?.Invoke();
            else if (eventData.clickCount == 1)
                onClick?.Invoke();
        
    

    public virtual void OnSubmit(BaseEventData eventData)
    
        Press();

        if (!IsActive() || !IsInteractable())
            return;

        DoStateTransition(SelectionState.Pressed, false);
        StartCoroutine(OnFinishSubmit());
    

    private IEnumerator OnFinishSubmit()
    
        var fadeTime = colors.fadeDuration;
        var elapsedTime = 0f;

        while (elapsedTime < fadeTime)
        
            elapsedTime += Time.unscaledDeltaTime;
            yield return null;
        

        DoStateTransition(currentSelectionState, false);
    

希望大家:点赞,留言,关注咯~    

😘😘😘😘

唠家常

  • 小黑的今日分享结束啦,小伙伴们你们get到了么,你们有没有更好的办法呢,可以评论区留言分享,也可以加小黑的QQ:841298494,大家一起进步

今日无推荐

以上是关于Unity自定义Button的主要内容,如果未能解决你的问题,请参考以下文章

GUIStyle.hover 状态是不是适用于 Unity 编辑器标签(或除 Button 之外的其他控件)?

自定义button

将按钮的 alphaHitTestMinimumTreshold 设置为不调用 Button.OnPointerDown

unity 如何自定义字体

WPF自定义Button控件

html,网页自定义button外形