我正在尝试扩展跨平台输入,以便使移动按钮左右移动角色。我做的是我创建了两个按钮和一个附加到每个名为MobileMovementButtons.cs的脚本。我运行程序,角色向左移动很好但不对......我认为它与我的条件语句有关..
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityEngine.EventSystems;
namespace CrossPlatformInput {
public class MobileMovementButton : MonoBehaviour, IPointerUpHandler, IPointerDownHandler{
public enum Direction {
Left,
Right
}
public Direction buttonDirection = Direction.Left;
CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis;
private bool rightPressed = false;
private bool leftPressed = false;
void Awake() {
CrossPlatformInputManager.UnRegisterVirtualAxis ("Horizontal");
}
void OnEnable() {
m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis("Horizontal" );
CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
}
void Update() {
if (rightPressed) {
m_HorizontalVirtualAxis.Update (1);
}
if (leftPressed) {
m_HorizontalVirtualAxis.Update (-1);
}
if (!leftPressed && !rightPressed){
m_HorizontalVirtualAxis.Update (0);
}
}
public void OnPointerDown(PointerEventData eD) {
if (this.buttonDirection == Direction.Left) {
leftPressed = true;
}
if (this.buttonDirection == Direction.Right) {
rightPressed = true;
}
}
public void OnPointerUp(PointerEventData eD) {
if (this.buttonDirection == Direction.Left) {
leftPressed = false;
}
if (this.buttonDirection == Direction.Right) {
rightPressed = false;
}
}
}
}
以下是按钮: