如何通过单击按钮调用更新功能的代码

时间:2019-01-03 11:21:49

标签: function unity3d long-press

如何使用Update方法在public函数内部调用代码?

我需要存档的是,使用另一个函数调用Update

以便Update使用其他方法触发。

还有一件事,只有长按按钮时代码才应运行。

非常感谢四位帮助

using UnityEngine;
using System.Collections;

public class CarController : MonoBehaviour
{

    public float speed = 1500f;
    public float rotationSpeed = 15f;

    public WheelJoint2D backWheel;
    public WheelJoint2D frontWheel;

    public Rigidbody2D rb;

    private float movement = 0f;
    private float rotation = 0f;



    void Update()
    {

        rotation = Input.GetAxisRaw("Horizontal");
        movement = -Input.GetAxisRaw("Vertical") * speed;

    }

    void FixedUpdate()
    {
        if (movement == 0f)
        {
            backWheel.useMotor = false;
            frontWheel.useMotor = false;
        }
        else
        {

            backWheel.useMotor = true;
            frontWheel.useMotor = true;

            JointMotor2D motor = new JointMotor2D { motorSpeed = movement, maxMotorTorque = 10000 };
            backWheel.motor = motor;
            frontWheel.motor = motor;


        }

        rb.AddTorque(-rotation * rotationSpeed * Time.fixedDeltaTime);
    }

    //public void Rotate()
    //{
    //    rotate = true;
    //    print("aa");
    //}
    //public void Move()
    //{
    //    rotation = Input.GetAxisRaw("Horizontal");
    //    movement = -Input.GetAxisRaw("Vertical") * speed;
    //}
}

2 个答案:

答案 0 :(得分:1)

实际上是两个问题。

1。长按按钮

Unity UI.Button本身并没有长按的方法,但是您可以使用IPointerXHandler界面自行实现:

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

// RequireComponent makes sure there is Button on the GameObject
[RequireComponent(typeof(Button))]
public class LongPressButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{
    private void Awake()
    {
        ResetButton();
    }

    // set the long press duration in the editor (in seconds)
    public float LongPressDuration = 0.5f;

    // Here you reference method just like in onClick

    public UnityEvent onLongPress;

    private float _timer;
    private bool _isPressed;
    private bool _pressInvoked;

    private void Update()
    {
        // prevent multiple calls if button stays pressed
        if (_pressInvoked) return;

        // if button is not pressed do nothing
        if (!_isPressed) return;

        // reduce the timer by the time passed since last frame
        _timer -= Time.deltaTime;

        // if timeout not reached do nothing
        if (!(_timer <= 0)) return;

        // Invoke the onLongPress event -> call all referenced callback methods
        onLongPress.Invoke();
        _pressInvoked = true;
    }

    // reset all flags and timer
    private void ResetButton()
    {
        _isPressed = false;
        _timer = LongPressDuration;
        _pressInvoked = false;
    }


    /* IPointer Callbacks */    

    // enable the timer
    public void OnPointerDown(PointerEventData eventData)
    {
        _isPressed = true;
    }

    // reset if button is released before timeout
    public void OnPointerUp(PointerEventData eventData)
    {
        ResetButton()
    }

    // reset if cursor leaves button before timeout
    public void OnPointerExit(PointerEventData eventData)
    {
        ResetButton();
    }
}

此脚本必须放在Button组件旁边。

您没有在Button的引用中引用回调方法  onClick,但在此LongPressButton的{​​{1}}

也不要忘记也在检查器中调整onLongPress

示例

enter image description here

2。呼叫LongPressDuration的{​​{1}}

我不知道您为什么要这么做(我想您已禁用该组件,但仍然想调用CarController

解决方案A

为了能够在Inspector中引用该方法,有几个选项:

  1. 只需使您的Update方法Update

    Update
  2. 用另一种public方法包装public void Update() { rotation = Input.GetAxisRaw("Horizontal"); movement = -Input.GetAxisRaw("Vertical") * speed; } 的内容,并改用该方法:

    Update
  3. 相反(您如何请求)-从另一个public方法调用private void Update() { DoUpdateStuff(); } public void DoUpdateStuff() { rotation = Input.GetAxisRaw("Horizontal"); movement = -Input.GetAxisRaw("Vertical") * speed; }

    Update

因此,剩下的全部都是在public的{​​{1}}事件中引用private void Update() { rotation = Input.GetAxisRaw("Horizontal"); movement = -Input.GetAxisRaw("Vertical") * speed; } public void DoUpdateStuff() { Update(); } 的{​​{1}}或CarController方法。


解决方案B

或者,您可以在运行时直接添加该回调,而无需引用任何内容,也无需使用回调方法Update,因此可以直接使用DoUpdateStuff,而无需使用包装方法。

缺点:对于这种方法,您必须以某种方式在LongPressButton脚本中获取对该onLongPress的引用

public

答案 1 :(得分:0)

感谢您的描述性答复。我所做的就是将脚本更改为following并添加了Event Trigger组件。然后相应地调用公共函数。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CarMve : MonoBehaviour
{

    bool move = false;
    bool moveR = false;
    public Rigidbody2D rb;

    public float speed = 20f;
    public float rotationSpeed = 2f;

    private void FixedUpdate()
    {
        if (move == true)
        {
            rb.AddForce(transform.right * speed * Time.fixedDeltaTime * 100f, ForceMode2D.Force);
        }
        if (moveR == true)
        {
            rb.AddForce(transform.right *- speed * Time.fixedDeltaTime * 100f, ForceMode2D.Force);
        }
    }

    /* Will be used on the UI Button */
    public void MoveCar(bool _move)
    {
        move = _move;
    }
    public void MoveCarR(bool _moveR)
    {
        moveR = _moveR;
    }

}

enter image description here