ButtonPlus
Coding Style wiki
ButtonPlus 是基於繼承 Unity UGUI 的 Button 進行擴展的按鈕組件,完整保留 Button 原有功能 (onClick、interactable、Transition、Navigation 等),並擴展支持各 Long Click (長按) 行為模式 (Once、Continuous、PressedAndReleased) 與 Scale (按壓縮放) 過渡效果;可透過 GameObject -> UI 選單直接建立,或以 Add Component 掛載 (選單路徑 OxGKit -> ButtonSystem -> Button -> ButtonPlus)。
| 命名空間 | OxGKit.ButtonSystem |
| 類型 | public class ButtonPlus : UnityEngine.UI.Button |
| 原始碼 | ButtonPlus.cs |
using OxGKit.ButtonSystem;
快速上手
using OxGKit.ButtonSystem;
using UnityEngine;
public class ControlPanel : MonoBehaviour
{
public ButtonPlus button;
private void Awake()
{
// 一般點擊 (繼承自 Button)
this.button.onClick.AddListener(() => Debug.Log("Click"));
// 切換長按模式並訂閱長按事件
this.button.extdLongClick = ButtonPlus.ExtdLongClick.PressedAndReleased;
this.button.triggerTime = 1f;
this.button.onLongClickPressed.AddListener(() => Debug.Log("Long Click Pressed"));
this.button.onLongClickReleased.AddListener(() => Debug.Log("Long Click Released"));
}
}
通用規則
建立方式
- Hierarchy 選單:GameObject -> UI -> Button Plus - TextMeshPro 或 GameObject -> UI -> Legacy -> Button Plus (自動附帶 Image 與對應的文字標籤)。
- Add Component:選單路徑 OxGKit -> ButtonSystem -> Button -> ButtonPlus;欲取代原有 Button 時,移除 Button 元件後改掛 ButtonPlus 並重新指定事件。
- 程式動態掛載:
gameObject.AddComponent<ButtonPlus>(),另可匯入 ButtonPlus Demo Sample 參考完整範例 (詳見模塊介紹)。
注意 ButtonPlus 覆寫的 Awake 僅記錄縮放原始尺寸,未呼叫 base.Awake();以程式動態掛載且需要 UGUI Transition 視覺效果時,請自行指定 targetGraphic。
長按模式
| 模式 | 行為 |
|---|---|
None | 無長按行為 (等同一般 Button)。 |
Once | 按住達 triggerTime 後,觸發一次 onLongClickPressed。 |
Continuous | 按住達 triggerTime 後首次觸發 onLongClickPressed,並於按住期間每 intervalTime 連續觸發。 |
PressedAndReleased | 按住達 triggerTime 觸發 onLongClickPressed;放開 (Pointer Up) 或指標移出按鈕 (Pointer Exit) 時觸發 onLongClickReleased。 |
注意 長按與點擊互斥:任一長按模式下,長按觸發後該次放開不會再觸發 onClick (內部會暫時替換 onClick 事件避免誤觸發),一次按壓要嘛是點擊、要嘛是長按。
注意 PressedAndReleased 的 onLongClickReleased 於指標移出按鈕時也會觸發,回呼需容許「未回到按鈕上就釋放」的情況。
計時規則
- ignoreTimeScale (預設
true) 決定 Continuous 連發計時採用Time.unscaledDeltaTime或Time.deltaTime累加。 - triggerTime 的觸發延遲是透過
MonoBehaviour.Invoke排程,受Time.timeScale影響 (與 ignoreTimeScale 無關)。
重要 遊戲暫停 (Time.timeScale = 0) 時,長按的初始觸發 (triggerTime) 不會發生;ignoreTimeScale 僅保證已觸發後的 Continuous 連發間隔