c# - Unity Input Field Lock previously typed characters after a time -
i've been messing around word game in unity c# , have come standstill regarding anti-cheat mechanic want implement.
once first letter has been entered input field, start running 2 second timer. after 2 seconds, in player not submit or type letter, input field should lock typed letters in place on input field , letters typed after should typed after it.
here's code have far:
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; } }
right i'm running oninputvaluechange()
whenever input field's value changed. manage store string that's been entered far once timer hits 2 seconds, not know how make input field "locks" locked string front , allow changes letters typed behind it. code inputfield.text = lockedstring + inputfield.text;
adds lockedstring
variable input field every time value gets changed.
the desired outcome such in pseudo-code:
//user types "bu" //2 second timer starts //during these 2 seconds, user can delete "bu" or continue typing //user deletes "bu" , types "ah" //once 2 second timer ends, whatever string in input locked //"ah" locked @ front of input field //after locking "ah", user cannot delete anymore, can continue typing
any insight how might achieve helpful. taking time help, appreciate it!
currently, concatenating string. want check if string starts same characters, , if not, overwrite input:
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); } }
note: have implemented alternate method of measuring time passed. feel free replace own method.
Comments
Post a Comment