【问题标题】:unity run a function as long a button is pressed in the inspector只要在检查器中按下按钮,统一运行功能
【发布时间】:2017-09-23 09:54:21
【问题描述】:

我是 Unity 的新手

使用 Unity Inspector,我设置了一个按钮,可以回调函数 (OnClick),它可以正常工作,但只有一次,要再次触发操作,我需要释放并再次单击按钮

只要按下按钮,我怎样才能使功能不断运行? (像机关枪)

public void MoveLeft ( )
{
    transform.RotateAround(Camera.main.transform.position, Vector3.up, -rotation / 4 * Time.deltaTime);
    infopanel.RotateAround(Camera.main.transform.position, Vector3.up, -rotation / 4 * Time.deltaTime);
}

问候...

【问题讨论】:

  • 我的回答解决了您的问题吗?

标签: c# function unity3d uibutton


【解决方案1】:

OnClick 不能这样做。使用OnPointerDownOnPointerUp。在这些函数中分别设置一个布尔变量为真/假,然后在Update函数中检查该布尔变量

附加到 UI Button 对象:

public class UIPresser : MonoBehaviour, IPointerDownHandler,
    IPointerUpHandler
{
    bool pressed = false;

    public void OnPointerDown(PointerEventData eventData)
    {
        pressed = true;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        pressed = false;
    }

    void Update()
    {
        if (pressed)
            MoveLeft();
    }

    public void MoveLeft()
    {
        transform.RotateAround(Camera.main.transform.position, Vector3.up, -rotation / 4 * Time.deltaTime);
        infopanel.RotateAround(Camera.main.transform.position, Vector3.up, -rotation / 4 * Time.deltaTime);
    }
}

你可以找到其他的事件函数here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多