当前位置: 首页> 文旅> 美景 > 网站建设制作心得团队_学seo推广_推广平台有哪些渠道_沈阳网站建设

网站建设制作心得团队_学seo推广_推广平台有哪些渠道_沈阳网站建设

时间:2025/8/29 21:45:22来源:https://blog.csdn.net/alizhuana/article/details/142280706 浏览次数:0次
网站建设制作心得团队_学seo推广_推广平台有哪些渠道_沈阳网站建设

废话不多说,在Untiy中,如果想要对Button等组件进行扩展的话,那么不仅仅只需要将新增的属性设置为public或者增加SerializeField字段就行了的,同时需要对Inspector的GUI面板进行修改,以下直接展示代码:

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using UnityEngine.UI;
using UnityEditor;[CustomEditor(typeof(ButtonExtension))]
public class ButtonExtensionEditor : UnityEditor.UI.ButtonEditor    
{public override void OnInspectorGUI(){base.OnInspectorGUI();ButtonExtension myScript = (ButtonExtension)target;myScript.singleClickEnabled = GUILayout.Toggle(myScript.singleClickEnabled, "单击");//创建一个打开/关闭的开关按钮。myScript.doubleClickEnabled = GUILayout.Toggle(myScript.doubleClickEnabled, "双击");if (myScript.doubleClickEnabled){myScript.doubleClickTime = EditorGUILayout.FloatField("双击时间", myScript.doubleClickTime);}myScript.longPressEnabled = GUILayout.Toggle(myScript.longPressEnabled, "长按");if (myScript.longPressEnabled){myScript.minPressTime = EditorGUILayout.FloatField("最小按住时间", myScript.minPressTime);}base.OnInspectorGUI();}
}public class ButtonExtension : Button
{#region 单机双击相关[SerializeField]public bool singleClickEnabled = true;public bool doubleClickEnabled = false;public float doubleClickTime = 0.3f;private float lastClickTime = float.NegativeInfinity;private int clickCount = 0;[FormerlySerializedAs("onDoubleClick")][SerializeField]private ButtonClickedEvent doubleClickEvent = new ();public ButtonClickedEvent onDoubleClick{get => doubleClickEvent;set => doubleClickEvent = value;}public override void OnPointerClick(PointerEventData eventData){if(!IsActive() && !interactable) return;if (singleClickEnabled){UISystemProfilerApi.AddMarker("Button.onClick", this);onClick?.Invoke();Debug.LogError("单击");   }if (doubleClickEnabled){clickCount++;if (clickCount >= 2){if (Time.realtimeSinceStartup - lastClickTime < doubleClickTime){UISystemProfilerApi.AddMarker("Button.onDoubleClick", this);onDoubleClick?.Invoke();Debug.LogError("双击");lastClickTime = float.NegativeInfinity;clickCount = 0;   }else{clickCount = 1;lastClickTime = Time.unscaledTime;}}else{lastClickTime = Time.unscaledTime;}}}#endregion#region 长按相关public bool longPressEnabled = false;private float lastPressTime = 0;public float minPressTime = 0.5f;private bool isPressing = false;private bool hasInvokedLongPress = false;[FormerlySerializedAs("onLongPress")][SerializeField]private ButtonClickedEvent longPressEvent = new();public ButtonClickedEvent onLongPress{get => longPressEvent;set => longPressEvent = value;}public override void OnPointerDown(PointerEventData eventData){base.OnPointerDown(eventData);if (longPressEnabled){hasInvokedLongPress = false;isPressing = true;lastPressTime = Time.unscaledTime;   }}public override void OnPointerUp(PointerEventData eventData){base.OnPointerUp(eventData);isPressing = false;hasInvokedLongPress = false;}private void Update(){DealLongPress();}private void DealLongPress(){if(hasInvokedLongPress) return;if (isPressing){if (Time.unscaledTime - lastPressTime >= minPressTime){UISystemProfilerApi.AddMarker("Button.onLongPress", this);onLongPress?.Invoke();hasInvokedLongPress = true;Debug.LogError("执行长按事件");}}}#endregion
}

参考博客:Unity 编辑器-UGUI拓展Button,一个和原Button一样按钮⭐_unity editor button-CSDN博客

[Unity案例]Button的双击和长按_unity button 长按检测-CSDN博客

关键字:网站建设制作心得团队_学seo推广_推广平台有哪些渠道_沈阳网站建设

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: