我对 Unity 中的脚本编写还很陌生,我正在尝试创建一个按钮,点击后需要模拟按下“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");
}
}
最佳答案
我认为模拟按键不是正确的方法。
相反,您应该在单击按钮时调用 PickUp
函数,就像在按下 F
键时调用 Pickup
一样。
// Drag & Drop the object holding the script to the `OnClick` listener of your button
// Then, simply select the `Pickup` function
public void Pickup()
{
// code ....
}
private void Update()
{
if( Input.GetKeyDown( KeyCode.F ) )
Pickup() ;
}
关于c# - 如何在按钮单击时模拟按键 - Unity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48930193/