unity run a function as long a button is pressed in the inspector

后端 未结 1 1758
清酒与你
清酒与你 2021-01-28 19:18

I\'m a newbie in Unity

Using Unity Inspector i have setup a button that makes a callback to a function (OnClick), it works fine but only once, to fire the action again i

相关标签:
1条回答
  • 2021-01-28 20:08

    The OnClick can't do this. Use OnPointerDown and OnPointerUp. Set a boolean variable to true/false in these function respectively then check that boolean variable in the Update function

    Attach to the UI Button object:

    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);
        }
    }
    

    You can find other event functions here.

    0 讨论(0)
提交回复
热议问题