我有一个UI按钮。我想在用户按下按钮时显示文本,而在用户释放按钮时隐藏文本。
我该怎么做?
答案 0 :(得分:2)
您必须通过扩展Button类并覆盖方法OnPoiterDown和OnPointerUp来创建自己的自定义按钮。 将MyButton组件而不是Button附加到游戏对象上
IDC_APPSTARTING := 32650
~LButton::
changeCursor(IDC_APPSTARTING)
Return
~LButton Up::
changeCursor()
Return
changeCursor(cursor := 0) {
if (cursor) {
CursorHandle := DllCall("LoadCursor", Uint, 0, Int, cursor)
Cursors = 32512,32513,32514,32515,32516,32640,32641,32642,32643,32644,32645,32646,32648,32649,32650,32651
Loop, Parse, Cursors, `,
DllCall("SetSystemCursor", Uint, CursorHandle, Int, A_Loopfield )
} else {
DllCall("SystemParametersInfo", UInt, 0x57, UInt, 0, UInt, 0, UInt, 0 )
}
}
答案 1 :(得分:0)
this anwser基本上可以,但是有一个很大的缺点:因为Button
已经具有内置的EditorScript来覆盖默认的Inspector,所以Inspector中不能包含其他字段。
我会改为将其实现为完全附加的组件,以实现IPointerDownHandler和IPointerUpHandler(也许还IPointerExitHandler也可以在按住鼠标/指针的同时退出按钮时也进行重置)。
要在 期间执行某项操作,请按住Coroutine。
通常我会使用UnityEvents:
[RequireComponent(typeof(Button))]
public class PointerDownUpHandler : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{
public UnityEvent onPointerDown;
public UnityEvent onPointerUp;
// gets invoked every frame while pointer is down
public UnityEvent whilePointerPressed;
private Button _button;
private void Awake()
{
_button = GetComponent<Button>();
}
private IEnumerator WhilePressed()
{
// this looks strange but is okey in a Coroutine
// as long as you yield somewhere
while(true)
{
whilePointerPressed?.Invoke();
yield return null;
}
}
public void OnPointerDown(PointerEventData eventData)
{
// ignore if button not interactable
if(!_button.interactable) return;
// just to be sure kill all current routines
// (although there should be none)
StopAllCoroutines();
StartCoroutine(WhilePressed);
onPointerDown?.Invoke();
}
public void OnPointerUp(PointerEventData eventData)
{
StopAllCoroutines();
onPointerUp?.Invoke();
}
public void OnPointerExit(PointerEventData eventData)
{
StopAllCoroutines();
onPointerUp?.Invoke();
}
}
与您可以引用onPointerDown
的{{1}}事件一样,引用onPointerUp
,whilePointerPressed
和onClick
中的任何回调一样。