有条件的统一UI按钮的问题
尝试用按钮替换键
void SlashAttack ()
{
// We look for the current state looking at the information of the animator
AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);
bool loading = stateInfo.IsName("Player_Slash");
// Ataque a distancia
if (Input.GetKeyDown("p") || Input.GetKeyDown(KeyCode.Z))
{
anim.SetTrigger("loading");
aura.AuraStart();
}
else if (Input.GetKeyUp("p") || Input.GetKeyUp(KeyCode.Z))
{
anim.SetTrigger("attacking");
if (aura.IsLoaded())
{
// Para que se mueva desde el principio tenemos que asignar un
// valor inicial al movX o movY en el edtitor distinto a cero
float angle = Mathf.Atan2(anim.GetFloat("movY"), anim.GetFloat("movX")) * Mathf.Rad2Deg;
GameObject slashObj = Instantiate(slashPrefab, transform.position, Quaternion.AngleAxis(angle, Vector3.forward));
Slash slash = slashObj.GetComponent<Slash>();
slash.mov.x = anim.GetFloat("movX");
slash.mov.y = anim.GetFloat("movY");
}
aura.AuraStop();
StartCoroutine(EnableMovementAfter(0.4f));
}
// Prevenimos el movimiento mientras cargamos
if (loading)
{
movePrevent = true;
}
}
快速反应:你不能因为UI.Button
没有像button down
或button up
这样的回调
如果您真的不想更改当前的代码结构,可以使用IPointerDownHandler和IPointerUpHandler编写自己的组件,例如:喜欢
using UnityEngine;
using UnityEngine.EventSystems;
public class Example : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public bool isDown;
public bool isUp;
//Detect current clicks on the GameObject (the one with the script attached)
public void OnPointerDown(PointerEventData pointerEventData)
{
isDown = true;
}
//Detect if clicks are no longer registering
public void OnPointerUp(PointerEventData pointerEventData)
{
isUp = true;
}
// reset both in LateUpdate
private void LateUpdate()
{
isDown = false;
isUp = false;
}
}
将其连接到您的按钮。
比你的代码引用依据组件
public Example PButton;
void SlashAttack ()
{
// ...
// Ataque a distancia
if (Input.GetKeyDown("p") || Input.GetKeyDown(KeyCode.Z) || PButton.isDown)
{
// ...
}
else if (Input.GetKeyUp("p") || Input.GetKeyUp(KeyCode.Z) || PButton.isUp)
{
// ...
}
// ...
}
注意
确保场景中存在
EventSystem
以允许指针检测。对于非UI GameObjects上的指针检测,请确保将PhysicsRaycaster
附加到Camera。
首先将单独的功能分解为单独的方法,例如:
public void OnKeyDown()
{
anim.SetTrigger("loading");
aura.AuraStart();
}
public void OnKeyUp()
{
anim.SetTrigger("attacking");
if (aura.IsLoaded())
{
// Para que se mueva desde el principio tenemos que asignar un
// valor inicial al movX o movY en el edtitor distinto a cero
float angle = Mathf.Atan2(anim.GetFloat("movY"), anim.GetFloat("movX")) * Mathf.Rad2Deg;
GameObject slashObj = Instantiate(slashPrefab, transform.position, Quaternion.AngleAxis(angle, Vector3.forward));
Slash slash = slashObj.GetComponent<Slash>();
slash.mov.x = anim.GetFloat("movX");
slash.mov.y = anim.GetFloat("movY");
}
aura.AuraStop();
StartCoroutine(EnableMovementAfter(0.4f));
}
void SlashAttack ()
{
// We look for the current state looking at the information of the animator
AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);
bool loading = stateInfo.IsName("Player_Slash");
// Ataque a distancia
if (Input.GetKeyDown("p") || Input.GetKeyDown(KeyCode.Z))
{
OnKeyDown();
}
else if (Input.GetKeyUp("p") || Input.GetKeyUp(KeyCode.Z))
{
OnKeyUp();
}
// Prevenimos el movimiento mientras cargamos
if (loading)
{
movePrevent = true;
}
}
当我们将方法公之于众时,您现在可以将它们链接到UI。由于标准统一按钮仅支持单击,并且您的代码依赖于按钮释放,因此您可以使用EventTrigger组件来显示要链接到的事件