如何模拟按键单击按键 - Unity

时间:2018-02-22 14:42:14

标签: c# unity3d

我对Unity中的脚本非常陌生,我试图创建一个按钮,一旦点击它就需要模拟' F' F' F'按下按键(拿起物品)

以下是我现有的代码,在写这篇文章之前,我已经查看了整个团结论坛,但找不到任何有用的代码。

代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
public class button : MonoBehaviour {

    public void ButtonToClick(int clickToButton)
    {
        SendKeys.Send("F");
    }
} 

2 个答案:

答案 0 :(得分:0)

从此question

  1. http://inputsimulator.codeplex.com

  2. 上下载zip文件
  3. 使用Unity项目中的脚本(C#)将其解压缩到Assets目录

  4. 重新加载MonoDevelop(如果是开放的)

  5. 在顶部的脚本中写:使用WindowsInput;

  6. 在课堂上您可以使用它,例如:InputSimulator.SimulateKeyPress(VirtualKeyCode.RIGHT); //模拟右箭头按

答案 1 :(得分:0)

我认为模拟按键不是正确的方法。

相反,您应该在单击按钮时调用PowerMockito.mockStatic(ClassUnderTest.class);函数,方法与按下PickUp键时调用Pickup的方式相同。

F