Unity-uiinput Custom Limit input characters

Source: Internet
Author: User
Tags alphanumeric characters

The Uiinput component as shown in

The uiinput can be used to create an input box that comes with 6 restrictions, as shown in

The last type of Alphabet Int Chinese is my custom, which restricts the input of letters, numbers, Chinese characters, such as nicknames, and can use this setting.

Custom restriction input code to modify Uiinput

First, find the validation attribute

1  Public enumValidation2     {3 None,4 Integer,5 Float,6 Alphanumeric,7 Username,8 Name,9Alphabetintchinese,//CustomTen}

Add the name of our custom type in which I add the Alphabetintchinese
Find Validate this function

1 protected CharValidate (stringTextintPosCharch)2     {3         //Validation is disabled4         if(validation = = Validation.none | |!enabled)returnch;5 6         if(Validation = =Validation.integer)7         {8             //Integer Number Validation9             if(Ch >='0'&& CH <='9')returnch;Ten             if(ch = ='-'&& pos = =0&&!text. Contains ("-"))returnch; One         } A         Else if(Validation = =validation.float) -         { -             //floating-point number the             if(Ch >='0'&& CH <='9')returnch; -             if(ch = ='-'&& pos = =0&&!text. Contains ("-"))returnch; -             if(ch = ='.'&&!text. Contains ("."))returnch; -         } +         Else if(Validation = =validation.alphanumeric) -         { +             //All alphanumeric characters A             if(Ch >='A'&& CH <='Z')returnch; at             if(Ch >='a'&& CH <='Z')returnch; -             if(Ch >='0'&& CH <='9')returnch; -         } -         Else if(Validation = =validation.username) -         { -             //Lowercase and numbers in             if(Ch >='A'&& CH <='Z')return(Char) (CH-'A'+'a'); -             if(Ch >='a'&& CH <='Z')returnch; to             if(Ch >='0'&& CH <='9')returnch; +         } -         Else if(Validation = =validation.name) the         { *             CharLastchar = (text. Length >0) ? Text[mathf.clamp (POS,0, text. Length-1)] :' '; $             CharNextchar = (text. Length >0) ? Text[mathf.clamp (pos +1,0, text. Length-1)] :'\ n';Panax Notoginseng  -             if(Ch >='a'&& CH <='Z') the             { +                 //Space followed by a letter – make sure it ' s capitalized A                 if(Lastchar = =' ')return(Char) (CH-'a'+'A'); the                 returnch; +             } -             Else if(Ch >='A'&& CH <='Z') $             { $                 //uppercase letters is only allowed after spaces (and apostrophes) -                 if(Lastchar! =' '&& Lastchar! ='\ '')return(Char) (CH-'A'+'a'); -                 returnch; the             } -             Else if(ch = ='\ '')Wuyi             { the                 //Don ' t allow more than one apostrophe -                 if(Lastchar! =' '&& Lastchar! ='\ ''&& Nextchar! ='\ ''&&!text. Contains ("'"))returnch; Wu             } -             Else if(ch = =' ') About             { $                 //Don ' t allow more than one space in a row -                 if(Lastchar! =' '&& Lastchar! ='\ ''&& Nextchar! =' '&& Nextchar! ='\ '')returnch; -             } -         } A         //custom, allow only alphanumeric characters to be entered +         Else if(Validation = =Validation.alphabetintchinese) the         { -             if(ch>=0x4e00&& ch<=0x9fa5)returnCh//This is mainly the range of Chinese characters $             if(Ch >='A'&& CH <='Z')returnch;  the             if(Ch >='a'&& CH <='Z')returnch;  the             if(Ch >='0'&& CH <='9')returnch;  the         } the         return(Char)0; -}

At the end of the function, add the type we want.

It is also possible to see from this function what kind of restriction the original type is

After doing this, the Validate attribute in the Uiinput component selects our custom properties to

Unity-uiinput Custom Limit input characters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.