0

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

我的目标平台是 WebGL。我编写了用于桌面的键盘箭头按钮的控制和用于桌面和移动设备的 UI 箭头按钮的控制。

键盘控制在 PC 上运行良好,但 UI 箭头按钮在桌面和移动设备上都没有按预期工作。

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

按下时如何获取UI箭头按钮功能?

界面的图像在这里。 在此处输入图像描述

我的脚本在这里:

附着在汽车物体上的那个

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

0 回答 0