我刚进入游戏模式,我已经撞墙了。 我实际上是一名C ++程序员,但我目前正在进行的修改要求我使用C#,这不应该是一个太大的问题,但我相当肯定我错过了一些关键C#概念。
我试图绑定"删除"按钮到使用" GetAsyncKeyState"递增变量的函数。我已经尝试过这个函数的每个变体,输入它等等,但没有任何工作。以下是整个功能,以及我在下面尝试过哪些变体的例子。
功能:
private void incModJump()
{
if (Convert.ToBoolean(Movement.GetAsyncKeyState(127) & 32768))
{
Thread.Sleep(150);
this.modJump += 1f;
this.modWallJump += 1f;
}
}
"变体" if"声明我已经试过了。
(这些都是使用和不使用" Convert.ToBoolean"进行测试,都不起作用。)
if (GetAsyncKeyState(127) > 0)
if (GetAsyncKeyState(127) & 0x8000)
if (GetAsyncKeyState(127) & 0x8000 == 0x8000)
if (GetAsyncKeyState(127) & 32768)
if (GetAsyncKeyState(127) & -32768)
上述所有内容似乎都不起作用,我对如何实现此功能感到不知所措,我也使用此功能查看了示例C#代码,所以我对此表示怀疑' sa语法错误(编译器也会警告我。)
如上所述,我是C ++程序员,所以这可能是因为我对C#不太熟悉,因此我将包括我尝试修改的整个课程,以防万一问题在于别处。 我在这里触及的唯一代码包括更多"使用"标头和添加if语句中的变量以及函数。 (请忽略关于令牌的注释部分,它们是由反编译器引起的。)
using System;
using System.Runtime.InteropServices;
using System.Threading;
using UnityEngine;
// Token: 0x0200007B RID: 123
public class Movement : MonoBehaviour
{
// Token: 0x06000282 RID: 642
private void Start()
{
this.fighting = base.GetComponent<Fighting>();
this.standing = base.GetComponent<Standing>();
this.info = base.GetComponent<CharacterInformation>();
this.controller = base.GetComponent<Controller>();
this.grabHandler = base.GetComponent<GrabHandler>();
this.au = base.GetComponentInChildren<AudioSource>();
BodyPart[] componentsInChildren = base.GetComponentsInChildren<BodyPart>();
this.rigidbodies = new Rigidbody[componentsInChildren.Length];
for (int i = 0; i < this.rigidbodies.Length; i++)
{
this.rigidbodies[i] = componentsInChildren[i].GetComponent<Rigidbody>();
}
this.screenshake = ScreenshakeHandler.Instance;
this.rightHand = base.GetComponentInChildren<RightHand>().GetComponent<Rigidbody>();
this.leftHand = base.GetComponentInChildren<LeftHand>().GetComponent<Rigidbody>();
}
// Token: 0x06000283 RID: 643
private void FixedUpdate()
{
this.flyVelocity *= 0.95f;
if (this.controller.canFly)
{
this.MoveFly(this.flyVelocity);
this.MoveFly(Vector3.up * 0.37f);
this.leftHand.AddForce(Vector3.down * 2000f * Time.fixedDeltaTime + Vector3.forward * 2000f * Time.fixedDeltaTime, ForceMode.Acceleration);
this.rightHand.AddForce(Vector3.down * 2000f * Time.fixedDeltaTime + Vector3.forward * -2000f * Time.fixedDeltaTime, ForceMode.Acceleration);
}
}
// Token: 0x06000284 RID: 644
private void MoveFly(Vector3 direction)
{
if (this.info.sinceFallen < 0f)
{
return;
}
Rigidbody[] array = this.rigidbodies;
for (int i = 0; i < array.Length; i++)
{
array[i].AddForce(direction * this.forceMultiplier * this.fighting.movementMultiplier * Time.deltaTime, ForceMode.Acceleration);
}
foreach (RigidbodyMovement rigidbodyMovement in this.rigsToMove)
{
rigidbodyMovement.rigidbody.AddForce(direction * rigidbodyMovement.forceMultiplier * this.fighting.movementMultiplier * Time.deltaTime, ForceMode.Acceleration);
}
}
// Token: 0x06000285 RID: 645
public void Fly(Vector3 direction)
{
this.flyVelocity += direction * Time.deltaTime * 10f;
}
// Token: 0x06000286 RID: 646
public void MoveRight()
{
if (this.info.sinceFallen < 0f)
{
return;
}
float num = 1f;
if (!this.controller.isAI)
{
num = Mathf.Abs((!this.controller.HasControl) ? ((this.standing.LeftStickYValue >= -0.5f) ? 0.6f : 0f) : this.controller.PlayerActions.Movement.X);
}
if (this.grabHandler.isHoldingSomething)
{
num *= 0.1f;
}
Rigidbody[] array = this.rigidbodies;
for (int i = 0; i < array.Length; i++)
{
array[i].AddForce(-Vector3.forward * this.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
}
foreach (RigidbodyMovement rigidbodyMovement in this.rigsToMove)
{
rigidbodyMovement.rigidbody.AddForce(-Vector3.forward * rigidbodyMovement.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
}
}
// Token: 0x06000287 RID: 647
public void Move(float direction)
{
if (this.info.sinceFallen < 0f)
{
return;
}
float num = 1f;
if (!this.controller.isAI)
{
num = Mathf.Abs(this.controller.PlayerActions.Movement.X);
}
if (this.grabHandler.isHoldingSomething)
{
num *= 0.1f;
}
Rigidbody[] array = this.rigidbodies;
for (int i = 0; i < array.Length; i++)
{
array[i].AddForce(direction * Vector3.forward * this.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
}
foreach (RigidbodyMovement rigidbodyMovement in this.rigsToMove)
{
rigidbodyMovement.rigidbody.AddForce(direction * Vector3.forward * rigidbodyMovement.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
}
}
// Token: 0x06000288 RID: 648
public void MoveLeft()
{
if (this.info.sinceFallen < 0f)
{
return;
}
float num = 1f;
if (!this.controller.isAI)
{
num = Mathf.Abs((!this.controller.HasControl) ? ((this.standing.LeftStickYValue >= -0.5f) ? 0.6f : 0f) : this.controller.PlayerActions.Movement.X);
}
if (this.grabHandler.isHoldingSomething)
{
num *= 0.1f;
}
Rigidbody[] array = this.rigidbodies;
for (int i = 0; i < array.Length; i++)
{
array[i].AddForce(Vector3.forward * this.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
}
foreach (RigidbodyMovement rigidbodyMovement in this.rigsToMove)
{
rigidbodyMovement.rigidbody.AddForce(Vector3.forward * rigidbodyMovement.forceMultiplier * this.fighting.movementMultiplier * num * Time.deltaTime, ForceMode.Acceleration);
}
}
// Token: 0x06000289 RID: 649
public bool Jump(bool force = false, bool forceWallJump = false)
{
bool result = this.DoJump(force, forceWallJump);
this.au.PlayOneShot(this.jumpClips[UnityEngine.Random.Range(0, this.jumpClips.Length)]);
return result;
}
// Token: 0x0600028A RID: 650
private bool DoJump(bool force = false, bool forceWallJump = false)
{
bool result = false;
this.standing.gravity = this.jumpTime * 0.5f;
float d = 0.3f;
foreach (Rigidbody rigidbody in this.rigidbodies)
{
rigidbody.velocity = new Vector3(rigidbody.velocity.x, 0f, rigidbody.velocity.z);
if (!force)
{
if (this.info.wallNormal != Vector3.zero)
{
rigidbody.AddForce(this.info.wallNormal * this.jumpForceMultiplier * this.modWallJump, ForceMode.VelocityChange);
rigidbody.AddForce(Vector3.up * this.jumpForceMultiplier * this.modWallJump, ForceMode.VelocityChange);
result = true;
}
else
{
rigidbody.AddForce(Vector3.up * this.jumpForceMultiplier * this.modJump, ForceMode.VelocityChange);
result = false;
}
}
else if (forceWallJump)
{
rigidbody.AddForce(this.info.wallNormal * d * this.jumpForceMultiplier * 0.75f, ForceMode.VelocityChange);
rigidbody.AddForce(Vector3.up * d * this.jumpForceMultiplier * 0.85f, ForceMode.VelocityChange);
}
else
{
rigidbody.AddForce(Vector3.up * d * this.jumpForceMultiplier, ForceMode.VelocityChange);
}
}
this.screenshake.AddShake(Vector3.up * 0.01f);
return result;
}
// Token: 0x060014F1 RID: 5361
private void incModJump()
{
if (Convert.ToBoolean(Movement.GetAsyncKeyState(127) & 32768))
{
Thread.Sleep(150);
this.modJump += 1f;
this.modWallJump += 1f;
}
}
// Token: 0x06001526 RID: 5414
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(int nVirtKey);
// Token: 0x040002CB RID: 715
public RigidbodyMovement[] rigsToMove;
// Token: 0x040002CC RID: 716
public float forceMultiplier;
// Token: 0x040002CD RID: 717
public float jumpForceMultiplier;
// Token: 0x040002CE RID: 718
public float jumpTime = 0.5f;
// Token: 0x040002CF RID: 719
private Standing standing;
// Token: 0x040002D0 RID: 720
private CharacterInformation info;
// Token: 0x040002D1 RID: 721
private Controller controller;
// Token: 0x040002D2 RID: 722
private GrabHandler grabHandler;
// Token: 0x040002D3 RID: 723
private Fighting fighting;
// Token: 0x040002D4 RID: 724
private Rigidbody[] rigidbodies;
// Token: 0x040002D5 RID: 725
private ScreenshakeHandler screenshake;
// Token: 0x040002D6 RID: 726
private AudioSource au;
// Token: 0x040002D7 RID: 727
public AudioClip[] jumpClips;
// Token: 0x040002D8 RID: 728
public Vector3 flyVelocity = Vector3.zero;
// Token: 0x040002D9 RID: 729
private Rigidbody leftHand;
// Token: 0x040002DA RID: 730
private Rigidbody rightHand;
// Token: 0x04001332 RID: 4914
private float modJump = 3f;
// Token: 0x04001333 RID: 4915
private float modWallJump = 3.75f;
}
如果这根本不可能,还有其他方法可以实现这个功能吗?
答案 0 :(得分:2)
这是一个简单的示例,说明如何在C#中使用GetASyncKeyState()来检查是否按下了一个键。
[DllImport("user32.dll")]
static extern short GetAsyncKeyState(Int32 vKey);
int VK_DELETE = 0x2E;
while (true)
{
short keyStatus = GetAsyncKeyState(VK_DELETE);
if ((keyStatus & 1) == 1)
{
//if you land here, the key was pressed
}
}
我们正在检查是否设置了最低有效位,请在MSDN上的备注中阅读更多内容。这确保了每次按键操作仅执行一次,而不是每次轮询键状态时执行一次。就像其他人在评论中说的那样,GetASyncKeyState有助于快速验证概念,但不适用于专业用途。原因之一是因为它在所有应用程序中都不安全,其他应用程序可能会设置GetASyncKeyState()的结果,并且还会影响您的程序。