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