unity 只要在检查器中按下按钮就可以运行一个函数

Chi*_*001 0 c# function uibutton unity-game-engine

我是 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);
}
Run Code Online (Sandbox Code Playgroud)

问候...

Pro*_*mer 5

这事他们OnClick做不到 使用OnPointerDownOnPointerUp. 分别在这些函数中将布尔变量设置为 true/false,然后检查函数中的布尔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);
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以在这里找到其他事件函数。