Search Unity

Better understanding UICharsInfo.cursorPos

Discussion in 'UGUI & TextMesh Pro' started by Breyer, Aug 22, 2014.

  1. Breyer

    Breyer

    Joined:
    Nov 10, 2012
    Posts:
    412
    I try implement button richText with support fire UnityEvent. But i need convert position first and last selected char to world position or screen pos depend on which is easier and flexible (so i could generate 1-3 rect - depend on case - to check if mouse click selected text)

    Return to topic: cursorPos is position related to pivot? that i saw in debug but im not sure. If yes I should write

    Code (CSharp):
    1. float worldPosX=rect.x+rect.width*pivot.x+UICharInfo.cursorPos.x; //same for y axis
    ?

    Optimization related point: simple case would be create 3xboxCollider2D (or 1x polygonal but i think procedural generate is harder in that case and no performance gain?) based on worldPos vertices and simple parent collider to text component and dont worry much on rotation and animation. But i would like to remove colliders because this relatively slow and, maybe, cannot automaticaly adjust when resize layout (must be regenerated via code in that case?). I would like to simple store 3x Rects and simple check .Contains(mousePosInWorldPosOrScreenPos); but how support rotation in that case?
     
    Last edited: Aug 22, 2014
  2. Breyer

    Breyer

    Joined:
    Nov 10, 2012
    Posts:
    412
    oh i think i found better idea: create TextButton component which inherit from Button, override IsRaycastLocationValid then generate rects on start and reuse them in validation. If valid true then insert html code to string in selected char position with proper color. If something critical change then regenerate rects. Detect this shouldnt be very hard since unity provide a lot of event.

    However i still need be sure what UICharInfo.cursorPos mean.
     
  3. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    The cursor position is the location of where the '|' character should be when editing the text (think input field). It's location is relative to the Rect it is inside of.
     
  4. Breyer

    Breyer

    Joined:
    Nov 10, 2012
    Posts:
    412
    Then this info is unreliable right? Im not concerned on input field, only text component but i guess in that case cursorpos is taken from input field in inspector. There exist any way to convert char position to world pos or screen pos?

    Btw is there screen to gui point method for ugui? I searched in doc but only i found is old guiutility

    Edit: i found recttransformutility.screentolocalpointinrect (since i have screen point in raycast validation) i suppose will be enough but still need find local position of char in generated text

    Edit2: i found verts array which may be solution but i must know how find correct vertex for given char index and what positiin is - worldPos or localPos?
     
    Last edited: Aug 23, 2014
  5. Breyer

    Breyer

    Joined:
    Nov 10, 2012
    Posts:
    412
    okay now i fully understand how work verts thanks to debugging (position is related to pivot and local) so it will be definitely my solution.

    @Tim C

    In the meantime i found that .z component never changes in vert position - so i think Vector3 is unnecessary and might mess a bit code in validation due to fact that screen pos is Vector2. There is reason for Vector3? I hope so that i dont sound as design nazi ^^
     
  6. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    As in the vertex position returned from the text generator? Well they may not always return a 0 z position, and then you need to convert them into a vector 3 to put them into the vertex array anyway.
     
  7. Breyer

    Breyer

    Joined:
    Nov 10, 2012
    Posts:
    412
    Yes i have in mind Text.cachedTextGenerator.verts []. Interesting, you say that .z component in vertex pos could be non-zero? Could you describe possible situation? I scaled text, rotated ,move in z axis and no change. Only one thing i can imagine and not tested yet is text over another text and .z mean 'piority' in visibility
     
  8. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    I mean in a future version, not currently :) We may add text generation effects (shadow ect) to the c++ side for performance and then some verts would be z offset.
     
  9. Breyer

    Breyer

    Joined:
    Nov 10, 2012
    Posts:
    412
    Oh then ok that makes sense i hope only that these additional vertex from effect dont mess list of vertex or we will have to filter via linq (or via loop) for be sure that we could easily grab correct vertexes for given char index. (actually number of vertexes is always 4*number of chars in text right?)

    Thanks so much for clarification. End of story ;)