Unity text implements mouse cursor

Source: Internet
Author: User

Because of the project requirements, the cursor of the mouse needs to be implemented on text, and the cursor moves with the click position. Implementation method:

1) Create a new cursor prefab (simply add a vertical bar spirte in the image) to increase the script control flicker.

The script is as follows:

    Public floatCycletime=0.2f;  PublicVector2 size =NewVector2 (2, -);  PublicColor Oricolor =NewColor (1,1,1,1);  PublicColor Newcolor =NewColor (1,1,1,0); //Use this for initialization    voidStart () {Startcoroutine (Shine ()); }        Private voidShineaction (BOOLiscolor) {gameobject.getcomponent<RectTransform> (). sizedelta=size; Gameobject.getcomponent<Image> (). color = IsColor?Oricolor:newcolor; } IEnumerator Shine () { while(true) {shineaction (false); yield return Newwaitforseconds (cycletime); Shineaction (true); yield return Newwaitforseconds (cycletime); }     }

2) Get the text where each character is located (width in the code is the length of each character,characterinfo.advance is the length of a single character)

          PublicText Textcomp; Font Font= Textcomp.font;//resources.load<font> ("FZCQJW");        intFontSize =textcomp.fontsize; stringText =Textcomp.text; Font.        Requestcharactersintexture (text, FontSize, fontstyle.normal);        Characterinfo Characterinfo; floatwidth =0f;  for(inti =0; I < text. Length; i++) {font. Getcharacterinfo (Text[i], outCharacterinfo, FontSize); //width+=characterinfo.width; unity5.x hints that this method will be discarded in the futurewidth + =characterinfo.advance; }

3) Get the mouse position (get the mouse position in the global coordinate system, need to pass transform. Inversetransformpoint (eventdata.position) into the text local coordinate system data), inherit the interface Ipointerdownhandler implement its Onpointerdown method, You can get the current click position by EventData. Position

     Public void Onpointerdown (pointereventdata eventData)    {    }

The complete code is as follows:

 Public classrowtoggleevent:monobehaviour,ipointerdownhandler{ PublicText Textcomp;  Publicgameobject cursor; PrivateGameobject Newcursor =NULL;  Public voidOnpointerdown (Pointereventdata eventData) {if(Transitdata.cursor! =NULL) Destroy (transitdata.cursor); Vector2 Pointerdownpos=transform.        Inversetransformpoint (eventdata.position);        Debug.Log (Pointerdownpos); Transitdata.cursor=instantiate (cursor, gameobject.transform); floatNewcursorxpos = Getcursonxpos (pointerdownpos.x- -); Vector2 Newcursorpos=NewVector2 (newcursorxpos+ -,-gameobject.getcomponent<recttransform> (). SIZEDELTA.Y/2); TransitData.cursor.GetComponent<RectTransform> (). Anchoredposition =Newcursorpos;    Debug.Log (Newcursorpos); }    Private floatGetcursonxpos (floatPointerdownxpos) {        floatXPos =0; Font Font= Textcomp.font;//resources.load<font> ("FZCQJW");        intFontSize =textcomp.fontsize; stringText =Textcomp.text; Font.        Requestcharactersintexture (text, FontSize, fontstyle.normal);        Characterinfo Characterinfo; floatwidth =0f;
for(inti =0; I < text. Length; i++) {font. Getcharacterinfo (Text[i], outCharacterinfo, FontSize); width + =characterinfo.advance;
if(Pointerdownxpos <=width) {XPos=width; Break; } Else { if(i = = text.) Length-1) XPos =width; } } returnXPos; }

Unity text implements mouse cursor

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.