2016-05-25 6 views
1

私はUnity C#で単語ゲームを使いこなしていましたが、私が実装したい反チートメカニックに関しては停止しました。ユニティ入力フィールド時間の経過とともに以前に入力された文字をロックします

最初の文字が入力欄に入力されたら、私は2秒タイマーを起動します。 2秒後に、プレーヤーが別の文字を送信したり入力したりしないと、入力フィールドは以前に入力された文字を入力フィールドに固定し、後に入力された文字はその後に入力する必要があります。今、入力フィールドの値が変更されるたびに、私はOnInputValueChange()を実行している

currTime = 0; 
hasInput = false; 
lockedString = ""; 

void Update(){ 
    if(hasInput){ 
     currTime += Time.deltaTime * 1; 
     if(currTime >= 2){ 
      //Stores current string value of input field 
      lockedString = inputField.text; 
     } 
    } 
} 

void OnInputValueChange(){ 
    currTime = 0; 
    hasInput = true; 
    if(lockedString != ""){ 
    inputField.text = lockedString + inputField.text; 
    } 
} 

は、ここで私がこれまで持っているコードです。私はまた、タイマーが2秒間ヒットするとこれまでに入力された文字列を格納することができますが、入力フィールドがロックされた文字列を前面に「ロック」し、後ろに入力された文字を変更できるようにする方法はわかりませんそれ。コードinputField.text = lockedString + inputField.text;は、値が変更されるたびに入力フィールドに変数lockedStringを追加するだけです。

望ましい結果は、このような擬似コードのようになる:

//User types "bu" 
//2 second timer starts 
//During these 2 seconds, user can delete "bu" or continue typing 
//User deletes "bu" and types "ah" 
//Once the 2 second timer ends, whatever string is now in input is locked 
//"ah" is now locked at the front of the input field 
//After locking "ah", user cannot delete it anymore, but can continue typing 

私はこのような何かを達成する方法をへの任意の洞察力が最も参考になります。時間を割いてくれてありがとう、本当に感謝しています!

答えて

0

現在のところ、文字列を連結するだけです。あなたは、文字列が同じ文字で始まるかどうかを確認したい、とされていない場合、完全に入力が上書きされます:

void Update() { 
    if (hasInput && ((Time.time - inputTime) > 2f)) 
    { 
     //Stores current string value of input field 
     lockedString = inputField.text; 
     hasInput = false; 
    } 
} 

void OnInputValueChange() { 
    inputTime = Time.time; 
    hasInput = true; 
    if ((lockedString.Length > 0) && (inputField.text.IndexOf(lockedString) != 0)) { 
     // Replace invalid string 
     inputField.text = lockedString; 
     // Update cursor position 
     inputField.MoveTextEnd(false); 
    } 
} 

注:私は経過時間を測定する別の方法を実装しています。これを自分の方法で置き換えてください。

+0

助けてくれてありがとう、私は家に帰るとすぐにこれをテストします! – poopenheimer

+1

チャームのように働いた!ありがとう。私は別のフォローアップの質問をするかもしれませんが、今、入力フィールドはプレイヤーの入力をうまくロックしますが、文字を削除しようとすると、カーソルは文字列を変更することなく元に戻ります。カーソルが常にロックされた文字列の前にとどまるようにする方法はありますか? ありがとうございます、+1。 – poopenheimer

+0

atmを見て...私はたくさんの解決策を見ていません –

関連する問題