Unity 2018:UI按钮OnPointerDown无法按预期运行

时间:2018-11-30 04:30:23

标签: c# unity3d

我正在测试UI箭头按钮的功能,该功能应该在3D场景中的x和z位置平移汽车对象。

我的目标平台是WebGL。我同时为台式机和移动设备的键盘箭头按钮控件和UI箭头按钮控件进行了编码。

键盘控制在PC上工作正常,但UI箭头按钮在台式机和移动设备上均无法正常工作。

我记录了OnPointerDown()函数,该函数位于UI箭头按钮附带的脚本中,并且当按下UI按钮时,日志没有显示。

按下后如何获得UI箭头按钮功能?

接口的图像在这里。 enter image description here

我的脚本在这里:

附着在汽车上的物体

using UnityEngine;

public class CarPlayerControl : MonoBehaviour {

public float speed = 40;
private bool touchCtrl = false;
private float xInput = 0f;
private float zInput = 0f;

void Awake(){
    //if (Input.touchSupported && Application.platform 
         //     != RuntimePlatform.WebGLPlayer){
        //touchCtrl = true;
        //}
        }

void FixedUpdate(){

    xInput = 0f;
    zInput = 0f;

    if (touchCtrl)
    {
        //if (Input.GetButton("ButtonUp"))
        //{
        //    xInput = 1.0f;
        //    Debug.Log("button up " + xInput);
        //}
        //else if (Input.GetButton("ButtonDown"))
        //{
        //    xInput = -1.0f;
        //    Debug.Log("button down " + xInput);
        //}
        //else if (Input.GetButton("ButtonLeft"))
        //{
        //    zInput = 1.0f;
        //    Debug.Log("button left " + zInput);
        //}
        //else if (Input.GetButton("ButtonRight"))
        //{
        //    zInput = -1.0f;
        //    Debug.Log("button right " + zInput);
        //}
    }
    else //mouse
    {
        xInput = Input.GetAxis("Horizontal");
        zInput = Input.GetAxis("Vertical");
    }

    Move(xInput, zInput);
}

public void Move(float xInput, float zInput)
{
    float xMove = xInput * speed * Time.deltaTime;
    float zMove = zInput * speed * Time.deltaTime;

    float x = KeepXWithinRange(xMove);
    float y = transform.position.y;
    float z = KeepZWithinRange(zMove);

    transform.position = new Vector3(x, y, z);
}

private float KeepXWithinRange(float xMove){
    float x = transform.position.x + xMove;
    return Mathf.Clamp(x, 0, 900);
}

private float KeepZWithinRange(float zMove)
{
    float z = transform.position.z + zMove;
    return Mathf.Clamp(z, -470, 500);
}
}

和附加到UI向上箭头按钮的那个

using UnityEngine;

public class ButtonUpControl : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public GameObject car;
private CarPlayerControl carPlayerControl;
private bool mouseDown;
private float xInput;

void Awake()
{
    carPlayerControl = car.GetComponent<CarPlayerControl>();
    mouseDown = false;
}
void Start()
{

}

void FixedUpdate()
{
    if (mouseDown) {
        xInput = 1.0f;
    }
    else {
        xInput = 0f; 
    }

    carPlayerControl.Move(xInput, 0f);
}

public void OnPointerDown(PointerEventData eventData)
{
    mouseDown = true;
    Debug.Log("on ptr down btn up ");
}

public void OnPointerUp(PointerEventData eventData)
{
    mouseDown = false;
    Debug.Log("on ptr up btn up ");
}
}

0 个答案:

没有答案