0

又是我 :D 我又遇到了下一个问题,但这一次我似乎无法解决它,而且也不应该是语法问题^^

好吧,现在我正在尝试为我们的 Classproject 为 UNity3D(在 C# 中)创建一个 Inputclass。我让输入本身工作,一切都很好。但是我必须这样做的原因,使控件在运行时可变,仍然是一个问题。我尝试了不同的方法来保存键(文本字段和编写键码 -> 我们的游戏设计师不喜欢它)。现在我试着用协程来做这件事,我最后一次尝试是将它转移到另一个函数并在鼠标释放后启动所有代码并且不应该做任何输入。无论如何它dsent工作。这是应该发挥魔力的代码:

    void PassKey(string wantedKey)
{
    if (Input.GetKey(KeyCode.Mouse0) == false)
    {
        Event uInput = Event.current;
        wantedKey = uInput.character.ToString();
        Debug.Log (wantedKey + uInput.character);
    }
}

这在这里被调用:

        if(GUI.Button(new Rect(0,0,qScreenWidthX / 2, qScreenHeightX / 5), rightText))
    {

        PassKey(right);
    }

(每个键都有这个按钮)。

你们知道如何在鼠标释放后等待下一个输入吗?

我不需要具体的代码,但也许可以提示我如何让它工作。

4

3 回答 3

0

根据上面OP的澄清评论,这里有一个片段应该适用于'a','b','c'等简单的键。

捕获击键的最简单方法是使用简单的标志来存储单击鼠标的时间,或者使用任何方法来指示要重新分配下一个键。在下面的示例中,当单击鼠标左键时,代码将观察Input.inputString是否为空。当Input.inputString包含任何内容时,ReassignKey()将使用第一个按键作为您要使用的按键分配。

请记住,inputString如果帧之间存在较大延迟,则可能包含多个 keyStroke。这就是为什么我只使用第一个字符。

public class KeyCapture : MonoBehaviour {
bool captureKey = false;
string assignedKey = "a";

// Update is called once per frame
void Update () {
    ReassignKey();

    // Mouse-click = wait for next key press
    if(Input.GetButtonDown("Fire1")){
        captureKey = true;
        Debug.Log("Waiting for next keystroke");
    }

    // Display message for currently assigned control.
    if(Input.GetKeyDown(assignedKey)){
        Debug.Log("Action mapped to key: " + assignedKey);
    }
}

void ReassignKey(){
    if(!captureKey)return;

    // ignore frames without keys
    if(Input.inputString.Length <= 0)return;

    // Input.inputString stores all keys pressed
    // since last frame but I only want to use a single
    // character.
    string originalKey = assignedKey;
    assignedKey = Input.inputString[0].ToString();
    captureKey = false;

    Debug.Log("Reassigned key '" + originalKey + "' to key '" + assignedKey + "'");
}
}
于 2013-06-26T13:24:05.200 回答
0

Thy @Jerdak and @user 2511414. I got the problems solution :D It was a bit like an epiphany this morning, the needed Code to return the Key that was hit is:

for(int i = 0; i < e; i++)
    {
        if(Input.GetKey((KeyCode)i))
        {
        return (KeyCode)i;
        }
            {

Im iterating through KeyCodes and check if any Key is pressed, and then Return the key that was actually pressed. THe problem with the other solutions was that my Lead hates multiple, avoidable If-parts.

于 2013-06-27T09:24:34.937 回答
0

如果我的问题是真的,您需要跟踪鼠标释放后按下的下一个键,这不是很难解决,就在任何鼠标释放后(在鼠标释放委托中)将布尔值(x)设置为 true,并且对于其他事件委托(任何导致进程取消的事件)将 value(x) 设置为 false,而不是在您的按键中,您会确定在它之前是否释放了鼠标,简单:

void mouseReleased(){// event when your mouse get released
   this.x=true;
}

void anyOtherEvent(){// for any other event that cancel the process, like wheel
   this.x=false;
}

void keyPressed(){//
   if(this.x){
     //ddo your operation
   }
   this.x=false;
}

希望我的问题是正确的。

于 2013-06-25T07:21:16.257 回答